Skip to content

Pairs with array by @AlexU238#2

Open
AlexU238 wants to merge 2 commits into
oleksandr-kazimirov:masterfrom
TrainingInc:master
Open

Pairs with array by @AlexU238#2
AlexU238 wants to merge 2 commits into
oleksandr-kazimirov:masterfrom
TrainingInc:master

Conversation

@AlexU238
Copy link
Copy Markdown

No description provided.

Comment thread DuosForSums.java Outdated
public static void main(String[] args) {
int[] initArr = {1, 3, 5, -2, 6, 0, -3, -1, 7, 2};
int sumNum = 5;
sort(initArr);
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want to have one method to do all work

Comment thread DuosForSums.java Outdated
array[ind2] = tmp;
}

private static void sort(int[] array) {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not necessary to implement custom sorting, use Collections.sort(..) where complexity is nlog(n)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Meanwhile sorting here is good optimization!

Comment thread DuosForSums.java
int first = 0;
int last = arr.length - 1;
while (first < last) {
int sumTmp = arr[first] + arr[last];
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sumTmp the local variable is redundant

Comment thread DuosForSums.java Outdated
i++;
pairs = new int[i][2];

for (int g = 0; g < tmp.length; ++g) {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need these second pair of loops?

Comment thread DuosForSums.java
public static void main(String[] args) {
int[] initArr = {1, 3, 5, -2, 6, 0, -3, -1, 7, 2};
int sumNum = 5;
group(initArr, sumNum);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if initArr = {5, 5, 5, 5, 5, 5, 5, 5, 5, 5}; sumNum = 10;? Will it show all pairs (45)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only unique pairs [[5, 5], [5, 5], [5, 5], [5, 5], [5, 5]]

Copy link
Copy Markdown

@malvitvik malvitvik Jul 19, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How many unique pairs has my example?) BTW unique - when a[i] is included only once in resulted array, right?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unique means, if a number is already used then it is out and not used anymore

Comment thread DuosForSums.java

}

private static void group(int[] arr, int x) {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants