Skip to content

Instantly share code, notes, and snippets.

@itugs
Last active May 29, 2023 01:52
Show Gist options
  • Select an option

  • Save itugs/76b05401dd662412990ce1e8b846e235 to your computer and use it in GitHub Desktop.

Select an option

Save itugs/76b05401dd662412990ce1e8b846e235 to your computer and use it in GitHub Desktop.
HackerCards for Leanne
public class Solution {
static int[] hackerCards(int[] collection, int d) {
List<Integer> result = new ArrayList<>();
int s, e;
for(int idx = 0; idx <= collection.length; idx++) {
if (idx == 0) s = 1; else s = collection[idx-1] + 1;
if (idx != collection.length) e = collection[idx]; else e = Integer.MAX_VALUE;
if (d < s) break;
for (int k = s; k < e; k++) {
if (k <= d) {
result.add(k);
d -= k;
} else {
break;
}
}
}
return result.stream().mapToInt(i->i).toArray();
}
}
@vinayak-tyagi
Copy link
Copy Markdown

Can you please explain the solution ?

@jeevikakabialn
Copy link
Copy Markdown

can you give explaination for this?

@Matt-J-Stewart
Copy link
Copy Markdown

I too would like an explanation of the code. I recently had to do this problem and would like to compare my answer to yours!

@ramyasaimullapudi
Copy link
Copy Markdown

Will the input Collections come in ascending order?

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