TITLE: Given an array A of size n with elements from 1 to k and another Array B of size k with elements 1 to n. Show that they have a subarray of same sum.
QUESTION [9 upvotes]: Given an array A of size n with elements from 1 to k and another Array B of size k with elements 1 to n.  Show that they have a subarray of the same sum where n,k >= 1.
So far I have done this much analysis:
Define d = P1- P2 where P1 and P2 are prefixes of array A and B. For some subarrays in A and B to have common sum d should be same for at least 2 pairs(P1, P2) and possible pairs are (n+1)(k+1) and d can have values from -nk to nk i.e. 2nk + 1 differences.
How should I proceed further? or is there any another way(Induction or something different) of proving the same?

REPLY [4 votes]: Assume w.l.o.g. that $\sum_{i=1}^n A_i\leq \sum_{j=1}^k B_j$. Then for every prefix $P_A$ of $A$, there is a prefix $P_B$ of $B$ such that $0\leq S(P_A) - S(P_B) < n$, where by $S(P_A)$ and $S(P_B)$ I mean sums of those prefices. Since there are $n+1$ prefices of $A$, for two of them, those differences must be the same, and it gives us subarrays of equal sums.