We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I'm not sure if it was just me, but my problem was with the way the question was phrased?
"Given an array of integers, find and print the maximum number of integers you can select from the array such that the absolute difference between any two of the chosen integers is <= 1."
So, if our array was 4, 6, 5, 3, 3, 1
I assumed from the question that we would check every integer for the absolute difference. For example:
Check 4 - 6, 4 - 5, 4 - 3, 4 - 3, 4 - 1. Then:
Check 6 - 5, 6 - 3, 6 - 3, 6 - 1. So on and so forth.
I realised at the end I interpreted the question incorrectly, but is it just me who misread the question?
i think the question asks that a number be chosen from the array
such that the difference between any 2 chosen numbers is less
than 2.
for example,
s = [4,6,7,4,6,4,6]
here, if 6 is chosen, then 7 is next number to choose.then, the next number n sould be such that |7-n| <2 and |6-n|<2
Given that you can just select one pair of integers that are a distance of one apart (say i and i+1), what is the size of the biggest multiset you can create that contains only elements equal to i and i+1.
seems like this is the correct interpretation, judging by correct code samples in this discussion... But can anyone explain how can anyone arrive to THIS interpretaion from the original question:
"find the longest subarray where the absolute difference between any two elements is less than or equal to 1" ???
I know the "correct" solution, and I've spent half an hour reading word-by-word these two question variations again and again, and they totally don't match.
ClaudiaC's variation suggests that we fill a frequency array, and then compare element pairs, picking up the Max.
For example, for the 1-2-2-3-1-2 array, the frequency array is 0-2-3-1-0-0, which means the longest subarray is two "1s" and three "2s": 1-1-2-2-2.
However, the original question asks us a completely different thing: find a longest sub-sequence where ALL elements have either equal values, or differ by "1". So in the original 1-2-2-3-1-2 array there are 1-2-2, 2-2-3, and 1-2 sequencies that match this condition.
You are ABSOLUTELY correct. The correct solution to the problem, as it is worded today (on 7-17-22), does not yield the results shown in example 0 in the problem description on hacker rank, and it does not yield results which are accepted by the code checker when one submits code.
The example problem and the code checker are looking for the maximmum number of elements meeting the criteria, IRRESPECTIVE of whether or NOT they are contiguously located in the input array, but the current wording requires contiguous elements.
Apparently the wording for this problem has changed, so as to require a very different solution then the earlier wording. Unfortunately explanation zero, given with problem, has not changed. Nor has the code checking data set used to verify the submitted code.
The current wording of the problem on the hackerrank site is the following:
"Given an array of integers, find the longest subarray where the absolute difference between any two elements is less than or equal to . "
(Note that a subarray is unambiguously a contguous subset of the input array.)
An earlier wording of the hackerrank problem as directly quoted in pokedev8's comment above is the following:
"Given an array of integers, find and print the maximum number of integers you can select from the array such that the absolute difference between any two of the chosen integers is <= 1."
That wording did not explicitly specify a subarray.
Many commenters felt the earlier wording was ambiguous. So someone apparently eliminated the ambiguity, but failed to check that the examples and code checking data were consistent with the new wording. Hugely annoying
nope i read it correctly first time, its "any pair" not just the number next door. so for 4,3,3,2,2,1 your best choice is {3,3,2,2} since 4 is more then 1 away from 2 and 1. this subset choice would be the same completely ignoring the order the numbers appear. so 3,1,2,4,2,3 is essentially the same problem as above
No need of creating an array and sorting it. Store the largest value in a variable instead and keep changing the value of that variable if larger value comes up.
Guys, don't be worried, the interpretation mentioned above is perfectly alright, what i beleive you are missing out on are the numbers with -ve differences which become postive when considered as absolute differences.
Check my code and I hope you'll understand.
Hello Raghav,
I understand what you are thinking but mathematically absolute difference between 2 numbers x and y is given by |x-y| which is
the difference between the two on number line which is always positive by definition, in simple words x-y if x>y and y-x if y>x, but while coding we only calculate x-y which will give -ve difference if y>x and hence absolute difference =|x-y| instead of x-y, I believe these cases are the ones which are causing you trouble. For more information on Absolute difference you can consult the Wikipedia link: Absolute Difference and other resources on web.
Hello Udit, we had the taken the new array anf then we are increasing the count at that index wherever our condition is satisfied. and then we are with new arrayy b at the end of loop.then we are finding the max out of this.
you just have to find out the multiset which has the maximum number of elements that satisfies the condition given in the question. Now see if you pick up elements like ,say, { 4,4,5,5,6} , so it is not a valid multiset because if you'd find out the difference between 6 and 4 , it will be 2 which is again greater than 1. SO now analyse through number thoery that no three consecutive elements can have the difference equal to one among the one except they are two subsequent numbers . Let's analyse this from the above example . {4,4,5,5} can be a valid multiset ,{5,5,6} can be a valid one because in each multiset there are two subsequent numbers . In first , it is 4 and 5. In second 5 and 6. So you just have to find out the frequency of the TWO consecutive numbers .
Take a static array say ar and start find the frequency of every element in the array (original ). Now you have the array ar which contains frequency of every element in the original array . You just have to find out the maximum frequency of the consequent elements .
All the makes sense but you lost me here:
"SO now analyse through number thoery that no three consecutive elements can have the difference equal to one among the one except they are two subsequent numbers"
@CS234_1716410234 thank you so much for your explanation, I wasn't able to crack this problem, but now did it, it's not an optimised code at all, it may have huge time complexity but it's working for the time being :
static int maxLength=0;
static boolean status = false;
static List array = new ArrayList<>();
static List array1 = new ArrayList<>();
sum function only takes two arguments , first one is iterable object like list, tuple,... second one is starting point which is optional . we can't directly give the n number of integers or floating values . it will throw an error like int|| float object is not iterable. are you get it?
Your code will give wrong answer for the sequence 1,1,1,2,2,2,3,4,5,5,5,5,6,7. Because 'pos' will written the position of 5. Final sequence will be either 4,5,5,5,5 or 5,5,5,5,6 according to your code as frequency of 5 is maximum whereas 1,1,1,2,2,2 is the actual answer
Step 1:
We have given 2<= n <= 100. So take an array of size 100.
The frequency of each integer of array a is stored in array arr
Step 2:
Now the array arr is sequence of integer containg the freqency of it.
So, the maximum subarray size will be the sum of frequeny of neighbouring integer.
The question is phrased correctly, what we all are missing out is that in the final set every pair of numbers should be at a difference of '1' form each other, so in the first example {1,2,2,3,1,2}, the final set cannot include 3, as the difference between 3 and 1 will be 2. So we have to check that every pair in the final answer should have a difference of only 1 with every other element.
Please stop complaining about the unclarity of exercises. In my developer's life, I constantly stomp on unclear specifications, which most of the time requires reading hundreds/thousands lines of existing code, gathering info from diverse documentations and getting, if needed, clarification from users or business analysts. It's just part of it. You can't escape it. Get used to it.
By problem statement itself, it's very clear that we need to worry about numbers that are adjacent or equal(i.e. absolute difference<=1). freq[] has got frequency of occurance of each element of input array. We just need to return which two adjacent elements appear highest in array.(sum of their frequencies)
public static int pickingNumbers(List grades) {
int[] count = new int[100];
Arrays.fill(count, 0);
for(int i = 0; i < grades.size(); i++)
count[grades.get(i)]++;
int max = 0;
for(int i = 0; i < 100-1; i++)
if(max < count[i] + count[i+1])
max = count[i] + count[i+1];
return max;
}
Here is the equivelent JavaScript code with comments:
functionpickingNumbers(a){returna.reduce((count,val)=>{// count occurance of each numbercount[val]++returncount;},newArray(100).fill(0)).reduce((max,val,index,count)=>{// max number of integers such that the absolute // difference between any two is 1if(max<val+count[index+1]){max=val+count[index+1];}returnmax;},0);}
Fill an array with zeros
Increment the count for each number found
Find the maximum pair of numbers
It should be noted that you could end up with a subset containing all the same integer.
Here is a solution with O(1) space complexity and (n*logn) Time complexity
pickingNumbers(a) {
a = a.sort();
let currLength = 1;
let maxLength = currLength;
let firstElement = a[0];
for (let i = 0; i < a.length - 1; i++) {
if (Math.abs(a[i] - a[i+1]) <= 1 && Math.abs(firstElement - a[i+1]) <= 1) {
currLength += 1;
}
else {
if (maxLength < currLength) maxLength = currLength;
currLength = 1;
firstElement = a[i+1];
}
// special case when maxLength needs to be set to currLength when the last element checked is part of the longest boundary and logic above never enters the else statement
if (i + 1 == a.length - 1 && maxLength < currLength) maxLength = currLength;
}
return maxLength;
fn picking_numbers(mut a: Vec) -> u8 {
let mut ctr: u8 = 0;
let mut val: u8 = 255;
let mut max: u8 = 0;
a.sort_unstable();
for v in a {
if v > val + 1 {
ctr = 0;
val = v;
}
ctr += 1;
if ctr > max {
max = ctr;
}
}
max
}
approach is really good ,but why every freq. array element is incremented without condition while u assigned all elements zero first but now every element is equal to one.
i did the same thing only to realise later this method will only give result for a single case
1,3,2,2
for 1 its 3
for 3 its 3
for 2 its 4
the answer would 4
which is wrong
I understood it the same way you did, but that's part of the trick ;). You're already solving it in your head before you completely assimilate the question.
The trick is to make sure the delta between any two randomly selected numbers from the final set must be <= 1.
yes , the question is to find the number itself and one related to it (+1 OR -1,but not all),because when you use abs, there is a element which will be 2 between the -1 one and the +1 one, the question, however ,is to find out an list in which the difference between two arbitrary elements is no more than 1
But it's clearly written: "...between any two of the chosen integers...". So in principle you can consider any of the 2^n possible subsequences, and if this subsequence S (= "the chosen integers") satisfies the condition (it can be written: max(S) - min(S) <= 1), then you call it "good". In the end you return the largest length of all "good" subsequences. Obviously, there is a more efficient way to do it.
Ok! I did the same mistake and implemented the same algorithm as you did. But you see, the problem is, from the given subset, if we take a nested loop for i and j
if ith value is 5:
jth value is 4: we get absolute difference as 1 ( increasing count by 1)
jth value is 6: we get absolute difference as 1 (increasing count by 1)
but the absolute difference between 4 and 6 ( since both are a part of our subarray, with atleast the wrong assumption that we made), is 2. which is wrong.
Same here, maybe it's hard to phrase it, but the first example is more harmful than helpful.
My story is that I spent quite a long time on solvnig it and didn't understand, what is wrong, but then I understood what was meant and wrote it whole under 1 minute, not kidding :(
Can anybody please help me out in finding solution for this question? (This question has no relevance with picking numbers. I misunderstood the question as formatted below.)
Given an array of integers, find and print the maximum number of integers you can select from the array such that they are consecutive numbers.
I agree. They should have said non-contiguous subarray, instead of just subarray. It took me a while to figure out a solution for a contiguous one. Then I realized that they are asking to solve a simpler problem.
This question is so so incorrectly phrased.I had started thinking of sliding window logic, and somehow came to the discussion section.Thanks for saving our time!
yaan for me the same problem existed . Well there is a mistake from hackerrank it is not making its question clear to us ,actually we have to find the longest subsequence of specified type ,not subarray
so you can run the same program with a minor change ,just sort the array.
Picking Numbers
You are viewing a single comment's thread. Return to all comments →
I'm not sure if it was just me, but my problem was with the way the question was phrased?
"Given an array of integers, find and print the maximum number of integers you can select from the array such that the absolute difference between any two of the chosen integers is <= 1."
So, if our array was 4, 6, 5, 3, 3, 1
I assumed from the question that we would check every integer for the absolute difference. For example:
Check 4 - 6, 4 - 5, 4 - 3, 4 - 3, 4 - 1. Then: Check 6 - 5, 6 - 3, 6 - 3, 6 - 1. So on and so forth.
I realised at the end I interpreted the question incorrectly, but is it just me who misread the question?
yes, same here.
I used collections counter to solve it easily.
Full python solution can be found here. Hackerrank - Picking Numbers Solution
here is problem solution in java python c++ c and javascript programming.
HackerRank Picking Numbers solution
Here is my O(n) c++ solution, you can find the explanation here : https://youtu.be/0zvqEO1gDRw
yes
i think the question asks that a number be chosen from the array such that the difference between any 2 chosen numbers is less than 2. for example, s = [4,6,7,4,6,4,6] here, if 6 is chosen, then 7 is next number to choose.then, the next number n sould be such that |7-n| <2 and |6-n|<2
same thing happened with me bro..
same and i still dont know what the correct iterpretation is :p
same here
I to felt same brother
I too felt same.
Given that you can just select one pair of integers that are a distance of one apart (say i and i+1), what is the size of the biggest multiset you can create that contains only elements equal to i and i+1.
thanks buddy
seems like this is the correct interpretation, judging by correct code samples in this discussion... But can anyone explain how can anyone arrive to THIS interpretaion from the original question:
"find the longest subarray where the absolute difference between any two elements is less than or equal to 1" ???
I know the "correct" solution, and I've spent half an hour reading word-by-word these two question variations again and again, and they totally don't match.
ClaudiaC's variation suggests that we fill a frequency array, and then compare element pairs, picking up the Max.
For example, for the 1-2-2-3-1-2 array, the frequency array is 0-2-3-1-0-0, which means the longest subarray is two "1s" and three "2s": 1-1-2-2-2.
However, the original question asks us a completely different thing: find a longest sub-sequence where ALL elements have either equal values, or differ by "1". So in the original 1-2-2-3-1-2 array there are 1-2-2, 2-2-3, and 1-2 sequencies that match this condition.
Absolutely disgusting challenge
xtrashlove:
You are ABSOLUTELY correct. The correct solution to the problem, as it is worded today (on 7-17-22), does not yield the results shown in example 0 in the problem description on hacker rank, and it does not yield results which are accepted by the code checker when one submits code.
The example problem and the code checker are looking for the maximmum number of elements meeting the criteria, IRRESPECTIVE of whether or NOT they are contiguously located in the input array, but the current wording requires contiguous elements.
Apparently the wording for this problem has changed, so as to require a very different solution then the earlier wording. Unfortunately explanation zero, given with problem, has not changed. Nor has the code checking data set used to verify the submitted code.
The current wording of the problem on the hackerrank site is the following:
"Given an array of integers, find the longest subarray where the absolute difference between any two elements is less than or equal to . "
(Note that a subarray is unambiguously a contguous subset of the input array.)
An earlier wording of the hackerrank problem as directly quoted in pokedev8's comment above is the following:
"Given an array of integers, find and print the maximum number of integers you can select from the array such that the absolute difference between any two of the chosen integers is <= 1."
That wording did not explicitly specify a subarray.
Many commenters felt the earlier wording was ambiguous. So someone apparently eliminated the ambiguity, but failed to check that the examples and code checking data were consistent with the new wording. Hugely annoying
oh :/, it was so simple, why did they write it so badly 0.0
sort(a.begin(),a.end()); int c=1;int s=a[0];int m=0; for(int i=1;im) m=c; } else { c=1; s=a[i]; } } return m;
same .... I don't get what the question wants me to do.
nope i read it correctly first time, its "any pair" not just the number next door. so for 4,3,3,2,2,1 your best choice is {3,3,2,2} since 4 is more then 1 away from 2 and 1. this subset choice would be the same completely ignoring the order the numbers appear. so 3,1,2,4,2,3 is essentially the same problem as above
Thank you! Some relief :)
Here is the solution https://www.hackerrank.com/challenges/picking-numbers/forum/comments/454763
explain your code why you used Arrays.sort() at last
i did so to find the largest element in the array created after calculating the desired no of elements for each element of the given array.
No need of creating an array and sorting it. Store the largest value in a variable instead and keep changing the value of that variable if larger value comes up.
me too
Here is the solution https://www.hackerrank.com/challenges/picking-numbers/forum/comments/454763
me tooo
Here is the solution https://www.hackerrank.com/challenges/picking-numbers/forum/comments/454763
same here. I misinterpreted it in the beginning
same.
Same here!
me too!
Guys, don't be worried, the interpretation mentioned above is perfectly alright, what i beleive you are missing out on are the numbers with -ve differences which become postive when considered as absolute differences. Check my code and I hope you'll understand.
But the question states that it needs only the absolute differences <=1, so why would i even care for the -ve differences converted to +ve.
Hello Raghav, I understand what you are thinking but mathematically absolute difference between 2 numbers x and y is given by |x-y| which is the difference between the two on number line which is always positive by definition, in simple words x-y if x>y and y-x if y>x, but while coding we only calculate x-y which will give -ve difference if y>x and hence absolute difference =|x-y| instead of x-y, I believe these cases are the ones which are causing you trouble. For more information on Absolute difference you can consult the Wikipedia link: Absolute Difference and other resources on web.
so many loops, the complaxity for this basic code is very high
it O(N^2) because only 2 nested loops
b[k]++ b[k+1]++ //can you explain please.
Hello Udit, we had the taken the new array anf then we are increasing the count at that index wherever our condition is satisfied. and then we are with new arrayy b at the end of loop.then we are finding the max out of this.
What array b stores? Can you please explain
// Complete the pickingNumbers function below.
This can be done in linear time.
consider sol={1,4,4,1,1,6,1) , according to your logic maxi=6+1=7, but that's not true maxi will be 4+4=8
I am not getting your point. Have you understood the question
LOGIC ALERT :-
you just have to find out the multiset which has the maximum number of elements that satisfies the condition given in the question. Now see if you pick up elements like ,say, { 4,4,5,5,6} , so it is not a valid multiset because if you'd find out the difference between 6 and 4 , it will be 2 which is again greater than 1. SO now analyse through number thoery that no three consecutive elements can have the difference equal to one among the one except they are two subsequent numbers . Let's analyse this from the above example . {4,4,5,5} can be a valid multiset ,{5,5,6} can be a valid one because in each multiset there are two subsequent numbers . In first , it is 4 and 5. In second 5 and 6. So you just have to find out the frequency of the TWO consecutive numbers . Take a static array say ar and start find the frequency of every element in the array (original ). Now you have the array ar which contains frequency of every element in the original array . You just have to find out the maximum frequency of the consequent elements .
CODE in C . Linear approach . int pickingNumbers(int a_count, int* a) { int i,j,count=0,max=0,sum; static int ar[101]; for(i=0;i
i cannot see your code , can you post the code again?
All the makes sense but you lost me here: "SO now analyse through number thoery that no three consecutive elements can have the difference equal to one among the one except they are two subsequent numbers"
Thanks.Your logic is really helpful.
@CS234_1716410234 thank you so much for your explanation, I wasn't able to crack this problem, but now did it, it's not an optimised code at all, it may have huge time complexity but it's working for the time being :
static int maxLength=0; static boolean status = false; static List array = new ArrayList<>(); static List array1 = new ArrayList<>();
A simple tweak to above solution. Passed all the test cases! My code:
https://www.hackerrank.com/challenges/picking-numbers/submissions/code/95999127
actually to sort things you need at least n*log n so it is the lower bound of complexity.
there is no need to sort the array.only the largest element in the array is to be found
What's wrong with something simple (Python) like:
return max([sum((a.count(i), a.count(i+1))) for i in set(a)])
Hi, If you could please tell which concept it is of Python? I am new to Python .
yeah seriously that was my first idea
just remove sum function and make the comma plus . It will work.
sum function only takes two arguments , first one is iterable object like list, tuple,... second one is starting point which is optional . we can't directly give the n number of integers or floating values . it will throw an error like int|| float object is not iterable. are you get it?
Best solution that I have found....Python is super cooool
thank you so much
Your code will give wrong answer for the sequence 1,1,1,2,2,2,3,4,5,5,5,5,6,7. Because 'pos' will written the position of 5. Final sequence will be either 4,5,5,5,5 or 5,5,5,5,6 according to your code as frequency of 5 is maximum whereas 1,1,1,2,2,2 is the actual answer
No it gives correct answer of 6
it'll take O(nlogn) for sorting purpose how come it is linear?
**public static int pickingNumbers(List a) { // Write your code here int[] arr = new int[100]; for(int temp : a) { arr[temp]++; }
could you please explain the basic logic of this code.
Step 1: We have given 2<= n <= 100. So take an array of size 100.
The frequency of each integer of array a is stored in array arr
Step 2: Now the array arr is sequence of integer containg the freqency of it. So, the maximum subarray size will be the sum of frequeny of neighbouring integer.
Wow very good thinking
awesome man,nice approach
tl;dr How about some comments in your code??? Or variables that are more than one letter?
you can ask where ever you need an explanation
when we use complexity of n^3,they are giving error with message,"exceeded compile time".
why int k = 2*i ?
complexity of ur code is too high its O(n^2), isn't there any other way to reduce the complexity?
could have lower time complexity
thank u
We have to find absolute difference which will be either 0 or 1
same here
Same issue. I think the question is phrased wrongly.
The question is phrased correctly, what we all are missing out is that in the final set every pair of numbers should be at a difference of '1' form each other, so in the first example {1,2,2,3,1,2}, the final set cannot include 3, as the difference between 3 and 1 will be 2. So we have to check that every pair in the final answer should have a difference of only 1 with every other element.
Yeah even i did the same thing.
Yes, same here.
haha lol same here.
Thanks! Without this discussion thread, I would not have known what was even being asked! :-)
yes same here
same
same here
same here!
yeah same here
yes.....same here
Please stop complaining about the unclarity of exercises. In my developer's life, I constantly stomp on unclear specifications, which most of the time requires reading hundreds/thousands lines of existing code, gathering info from diverse documentations and getting, if needed, clarification from users or business analysts. It's just part of it. You can't escape it. Get used to it.
I don't think the question is phrased in confusing way. Here is what I thought..
I was here to check whether anyone has done it in O(1) space. I guess, no one:)
can you explain your logic? im still not clear how the max of sum of frequencies gets the answer!
By problem statement itself, it's very clear that we need to worry about numbers that are adjacent or equal(i.e. absolute difference<=1). freq[] has got frequency of occurance of each element of input array. We just need to return which two adjacent elements appear highest in array.(sum of their frequencies)
Brillient
thanks boiiii
thanks to your explanation, danglingP0inter ;))
public static int pickingNumbers(List grades) { int[] count = new int[100]; Arrays.fill(count, 0); for(int i = 0; i < grades.size(); i++) count[grades.get(i)]++; int max = 0; for(int i = 0; i < 100-1; i++) if(max < count[i] + count[i+1]) max = count[i] + count[i+1]; return max;
}
Just Brilliant .
Very good. I am supposed to be used to frequency approach. You've enlightened me.
Here is the equivelent JavaScript code with comments:
It should be noted that you could end up with a subset containing all the same integer.
Nice!! Nested Reduce for the win. I will study this!!
I also used reduce but sligthly another way ))
Why shouldn't the last for loop start at i=1 instead of i=2?
Here is a solution with O(1) space complexity and (n*logn) Time complexity
}
Same approach, cleaner code:
Hey, This solution satisfies all the test cases but consider this case
7
1 1 2 2 3 3 3
The output will be 4 instead it should be 5 as the array will be [2.2,3,3,3]
Please let me know if I am wrong.
approach is really good ,but why every freq. array element is incremented without condition while u assigned all elements zero first but now every element is equal to one.
GENIUS
But how do you know that the max number in the list will be 2 digits long only??
Fine coding skills! thanks.
thank u so much Excellent logic !
If you notice the key word "any" in the description, you will not be confused. For example, 6-1=5, which breaks the requirement. My code in C.
int pickingNumbers(int a_size, int* a) { int n=a_size; int i; int j; int np; int nn; int max=0;
}
int main() { int n; scanf("%i", &n); int *a = malloc(sizeof(int) * n); for (int a_i = 0; a_i < n; a_i++) { scanf("%i",&a[a_i]); } int result = pickingNumbers(n, a); printf("%d\n", result); return 0; }
your code is very helpful as well as efficient. it only needs o(1) space. thank u
No, Bro the code complexity will be o(n^2). See carefully.
its taking O(n*n) time. while it can be solved in O(nlgn) and O(n) too. Try it.
i did the same thing only to realise later this method will only give result for a single case 1,3,2,2 for 1 its 3 for 3 its 3 for 2 its 4 the answer would 4 which is wrong
i used that algorithm and it worked fine. i don't think anything is wrong with it
i haven't tried the algorithm yet i just read the question and tried on my own first
on the same page
I'm not even going to try. Doesn't make sense. They should re-word it.
nice trick
Same here, though I never had this issue before...
Here is the solution https://www.hackerrank.com/challenges/picking-numbers/forum/comments/454763
i understood by reading examples
but the difference between 4 and 5 also 1 ryt?
Same here :(
same here!!!!
Select is the keyword here i guess. Just a tricky sentence.
Yes, in problem of 4, 5, 6, 3, 3, 1 answer should be 4. for {4, 5, 3, 3} but Hackerrank expected 3.
but difference of 3 and 5 is 2 so it doesnt fulfill the condition.
Same here!
I understood it the same way you did, but that's part of the trick ;). You're already solving it in your head before you completely assimilate the question.
The trick is to make sure the delta between any two randomly selected numbers from the final set must be <= 1.
yes , the question is to find the number itself and one related to it (+1 OR -1,but not all),because when you use abs, there is a element which will be 2 between the -1 one and the +1 one, the question, however ,is to find out an list in which the difference between two arbitrary elements is no more than 1
But it's clearly written: "...between any two of the chosen integers...". So in principle you can consider any of the 2^n possible subsequences, and if this subsequence S (= "the chosen integers") satisfies the condition (it can be written: max(S) - min(S) <= 1), then you call it "good". In the end you return the largest length of all "good" subsequences. Obviously, there is a more efficient way to do it.
me too
Yeah, correct!
Exact same here.
I think question is wrong because if we see the example 4,6,5,3,3,1 then we will see that 4,4-5,4-3,4-3 total 4 will be the highest.
same it bro
This will take care of all the cases: def pickingNumbers(a): c=0 s=set(a) for i in s: p=a.count(i)+a.count(i+1) if c < p: c=p return c
Same here Brother
same shit happened with me
def pickingNumbers(a): return max(len([x for x in a if 0 <= u - x <= 1]) for u in set(a))
Ok! I did the same mistake and implemented the same algorithm as you did. But you see, the problem is, from the given subset, if we take a nested loop for i and j if ith value is 5: jth value is 4: we get absolute difference as 1 ( increasing count by 1) jth value is 6: we get absolute difference as 1 (increasing count by 1)
but the absolute difference between 4 and 6 ( since both are a part of our subarray, with atleast the wrong assumption that we made), is 2. which is wrong.
//C++ code
int main()
{int n,a[100],i,h[100],max=0;
}
Same. Any solution to it?
Same here bro. Now that I come in discussion section I found this common to most of us all.
yes sir. I agree
same here!
include
int main() { int n,a[1000000],b,i,f,k=0,v=0; scanf("%d",&n); for(i=0;i
}
same here
same with me .
Same for me also
same here
same here
Same here, maybe it's hard to phrase it, but the first example is more harmful than helpful.
My story is that I spent quite a long time on solvnig it and didn't understand, what is wrong, but then I understood what was meant and wrote it whole under 1 minute, not kidding :(
as i also though like that bro
same here
|4-5|=1
Can anybody please help me out in finding solution for this question? (This question has no relevance with picking numbers. I misunderstood the question as formatted below.)
Given an array of integers, find and print the maximum number of integers you can select from the array such that they are consecutive numbers.
Example:
Input 4 6 5 3 3 2 2 1
Output 6
Reason {4,3,3,2,2,1}
Input 1 2 2 3 2 1 2
Output 7
Reason {1,2,2,3,2,1,2}
My solution in java : https://github.com/HarshSharma001/Hackerrank-Problems/blob/master/Hackerrank/src/picking_numbers/Solution.java
It's explanation lies here :: https://www.hackerrank.com/challenges/picking-numbers/forum/comments/532263
same here
SAMEEEEE but it worked out anyways it was messy but ok
why the answer is 4,3,3 not 4,5,3,3 ? |4-5|=1 right?
Yes, it's a badly worded question.
I agree. They should have said non-contiguous subarray, instead of just subarray. It took me a while to figure out a solution for a contiguous one. Then I realized that they are asking to solve a simpler problem.
2 versions of Python code solving in O(n) time:
version 2:
Yup. Read it as contiguous arrays in the original string.
guess i am the only one
yes same.
This question is so so incorrectly phrased.I had started thinking of sliding window logic, and somehow came to the discussion section.Thanks for saving our time!
Me too misread it. It's written to find the longest subarray but in explanation part it is taking multiset(i.e. sub-sequence).
according to my understanding, {1,2,3,4,5,6} should give answer 6;
but.. question is like same but, questions was framed to give here answer 2;
me too.
same
same here
yaan for me the same problem existed . Well there is a mistake from hackerrank it is not making its question clear to us ,actually we have to find the longest subsequence of specified type ,not subarray so you can run the same program with a minor change ,just sort the array.
me too
Same here....
it took me an hour to figure out the challenge well its pretty easy when i saw the solution i hope you also solved the challenge
same here. confusing quesiton
Yeah that's why i am not able to pass one of the testcases
Ya.. Same i realized ... i think in question , the correct intrepretaion is hard to find as the question asked.
yes, and I still don't get it tbh
same here, even after they paraphrased it, it still took me hours to figure it out