A Non-empty zero-indexed array A consisting of N integers is given. A pair of integers (P, Q) is called K-complementary in Array Aif0≤p, Q < N and a[p] + a[q] =K. For example, consider array A such that:a[0] = 1 a[1] = 8 a[2]=-3a[3] = 0 A[4] = 1 a[5]= 3a[6] = 2 A[7] = 4 a[8]= 5The following pairs is6-complementary in array A: (0,8), (1,6), (4,8), (5,5), (6,1), (8,0), (8,4). For instance, the pair (4,8) is 6-complementary because a[4] + a[8] = 1 + 5 = 6. Write a function:classSolution { Public intSolution (intKint[] A); }that, given an integer K and a non-empty zero-indexed array A consisting of N integers, returns the number of Kcomplementary pairs in array A. For example, given K= 6and array A such that:a[0] = 1 a[1] = 8 a[2]=-3. a[3] = 0 A[4] = 1 a[5]= 3. a[6] = 2 A[7] = 4 a[8]= 5The function shouldreturn7, as explained above. Assume That:n is an integer within the range [1..50,000]; K is an integer within the range [−2,147,483,648..2,147,483,647];each element of array A is an integer within the range [−2,147,483,648..2,147,483,647]. Complexity:expected Worst- CaseTime complexity is O (nlog (N)); expected worst- CaseSpace complexity is O (N), beyond input storage (not counting the storage required forinput arguments). Elements of input arrays can be modified.
Much like the 2sum pinch algorithm, it needs a sort. It is not difficult, but the tricky is to allow the same array elements to make a pair of themselves, as in the example above [5, 5]. And the array itself allows elements of equal value to exist, when calculating the pair, the pair is counted as different pairs, such as the array is [3,3],k=6, when the pair has [0, 0], [0, 1], [1, 0], [1, 1]4.
This case made the problem a lot more difficult. My counterpart is to record the number of occurrences of each element with HashMap, and record the number of viable pair with a variable res. Like the clamping method, a left and right two pointer L, R respectively go in the middle, if the left and next elements and add up equals K:
1. If L = = r, res = res + 1;
2. Else, if a[l] = = A[r], res = res + MATH.POW (map.get (A[l]), 2); Plus A[l] The number of squares of occurrences, such as [3, 3] This example, plus 2^2 ==4, again such as [3, 3, 3], plus 9
3. else, i.e. a[l]! = A[r], res = res + 2 * map.get (A[l]) * Map.get (A[r]); For example [1, 1, 5], plus 4;[1, 1, 5, 5], plus 8;[-2, 8], plus 2
And then the pointer how to jump after the left and right, my approach is to jump the number of occurrences so many times, so that the number of these occurrences are not repeated processing
1 Public intKcomplementary (int[] A,intK) {2 if(a==NULL|| a.length==0)return0;3 intres = 0;4 intL = 0;5 intr = A.length-1;6Hashmap<integer, integer> map =NewHashmap<integer, integer>();7 for(inti=0; i<a.length; i++) {8 if(Map.containskey (A[i])) {9Map.put (A[i], Map.get (A[i]) +1);Ten } One Else { AMap.put (A[i], 1); - } - } the while(L <=r) { - if(A[l] + a[r] = =K) { - if(L = r) Res + = 1; - Else if(A[l] = =A[r]) { +Res + = Math.pow (Map.get (A[l]), 2); - } + Else { ARes + = 2 * Map.get (A[l]) *Map.get (A[r]); at } -L = l +Map.get (A[l]); -R = R-Map.get (A[r]); - } - Else if(A[l] + A[r] <K) { -L = l +Map.get (A[l]); in } - Else { toR = R-Map.get (A[r]); + } - } the returnRes; *}
Twitter OA prepare:k-complementary Pair