Pairs with array by @AlexU238#2
Conversation
| public static void main(String[] args) { | ||
| int[] initArr = {1, 3, 5, -2, 6, 0, -3, -1, 7, 2}; | ||
| int sumNum = 5; | ||
| sort(initArr); |
There was a problem hiding this comment.
We want to have one method to do all work
| array[ind2] = tmp; | ||
| } | ||
|
|
||
| private static void sort(int[] array) { |
There was a problem hiding this comment.
It is not necessary to implement custom sorting, use Collections.sort(..) where complexity is nlog(n)
There was a problem hiding this comment.
Meanwhile sorting here is good optimization!
| int first = 0; | ||
| int last = arr.length - 1; | ||
| while (first < last) { | ||
| int sumTmp = arr[first] + arr[last]; |
There was a problem hiding this comment.
sumTmp the local variable is redundant
| i++; | ||
| pairs = new int[i][2]; | ||
|
|
||
| for (int g = 0; g < tmp.length; ++g) { |
There was a problem hiding this comment.
Why do we need these second pair of loops?
| public static void main(String[] args) { | ||
| int[] initArr = {1, 3, 5, -2, 6, 0, -3, -1, 7, 2}; | ||
| int sumNum = 5; | ||
| group(initArr, sumNum); |
There was a problem hiding this comment.
What if initArr = {5, 5, 5, 5, 5, 5, 5, 5, 5, 5}; sumNum = 10;? Will it show all pairs (45)
There was a problem hiding this comment.
Only unique pairs [[5, 5], [5, 5], [5, 5], [5, 5], [5, 5]]
There was a problem hiding this comment.
How many unique pairs has my example?) BTW unique - when a[i] is included only once in resulted array, right?
There was a problem hiding this comment.
unique means, if a number is already used then it is out and not used anymore
|
|
||
| } | ||
|
|
||
| private static void group(int[] arr, int x) { |
There was a problem hiding this comment.
a little bit hard to understand but this is good enough for the current level. please take into account array could be created in this way new int[]{5,5}
No description provided.