• + 95 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?

    • + 3 comments

      yes, same here.

      • + 3 comments
        [deleted]
        • + 1 comment

          I used collections counter to solve it easily.

          Full python solution can be found here. Hackerrank - Picking Numbers Solution

        • + 0 comments

          here is problem solution in java python c++ c and javascript programming.

          HackerRank Picking Numbers solution

        • + 0 comments

          Here is my O(n) c++ solution, you can find the explanation here : https://youtu.be/0zvqEO1gDRw

          int pickingNumbers(vector<int> a) {
              map<int, int> mp;
              for(int e : a) mp[e]++;
              int ans = mp[0];
              for(int i = 1; i < 99; i++){
                  int curr = max(mp[i] + mp[i+1], mp[i] + mp[i-1]);
                  ans = max(ans, curr);
              }
              return ans;
          }
          
      • + 1 comment

        yes

        • + 0 comments

          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

      • + 0 comments

        same thing happened with me bro..

    • + 4 comments

      same and i still dont know what the correct iterpretation is :p

      • + 1 comment

        same here

        • + 1 comment

          I to felt same brother

          • + 2 comments

            I too felt same.

      • + 3 comments

        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.

        • + 0 comments

          thanks buddy

        • + 1 comment

          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

          • + 0 comments

            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

        • + 0 comments

          oh :/, it was so simple, why did they write it so badly 0.0

      • + 0 comments

        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;

      • + 0 comments

        same .... I don't get what the question wants me to do.

    • + 0 comments

      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

    • + 1 comment

      Thank you! Some relief :)

      • + 1 comment

        Here is the solution https://www.hackerrank.com/challenges/picking-numbers/forum/comments/454763

        • + 1 comment

          explain your code why you used Arrays.sort() at last

          • + 1 comment

            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.

            • + 0 comments

              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.

    • + 1 comment

      me too

      • + 0 comments

        Here is the solution https://www.hackerrank.com/challenges/picking-numbers/forum/comments/454763

    • + 1 comment

      me tooo

      • + 0 comments

        Here is the solution https://www.hackerrank.com/challenges/picking-numbers/forum/comments/454763

    • + 0 comments

      same here. I misinterpreted it in the beginning

    • + 0 comments

      same.

    • + 0 comments

      Same here!

    • + 0 comments

      me too!

    • + 12 comments

      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.

      #include <iostream>
      using namespace std;
      
      int n,a[100],b[200];
      int main() {
          cin>>n;
          for(int i=0;i<n;i++)
              cin>>a[i];
          for(int i=0;i<n;i++){
              for(int j=0;j<n;j++){
                  int k= 2*i;
                  if(a[i]==a[j]||a[i]==a[j]+1)
                      b[k]++;
                  if(a[i]==a[j]||a[i]==a[j]-1)
                      b[k+1]++;
              }
          }
          for(int i=0;i<200;i++)
              if(b[i]>b[0])
                  b[0]=b[i];
          cout<<b[0];
          return 0;
      }
      
      • + 1 comment

        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.

        • + 1 comment

          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.

          • + 2 comments

            so many loops, the complaxity for this basic code is very high

            • + 0 comments

              it O(N^2) because only 2 nested loops

      • + 1 comment

        b[k]++ b[k+1]++ //can you explain please.

        • + 0 comments

          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.

      • + 0 comments

        What array b stores? Can you please explain

      • + 7 comments

        // Complete the pickingNumbers function below.

        int pickingNumbers(vector a) {

        int n = a.size();
        vector<int> sol(100,0);
        //sort(a.begin(), a.end());
        for(int i=0; i<n; i++){
            sol[a[i]]++;
        }
        int pos = max_element(sol.begin(),sol.end()) - sol.begin();
        //cout<<sol[pos];
        return maxi(sol[pos+1]+sol[pos],sol[pos-1]+sol[pos]);
        

        }

        This can be done in linear time.

        • + 3 comments

          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

          • + 0 comments

            I am not getting your point. Have you understood the question

          • + 4 comments

            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 .

            • + 1 comment

              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

              • + 0 comments

                i cannot see your code , can you post the code again?

            • + 0 comments

              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"

            • + 0 comments

              Thanks.Your logic is really helpful.

            • + 0 comments

              @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<>();

              public static int pickingNumbers(List<Integer> a) 
              {
                  Collections.sort(a);
                  int min=0;
              
                      for(int i=0; i<a.size(); i++)
                      {
                          min = a.get(i);
                          if(i==0)
                          {
                              array1.add(min);                    
                          }
              
                          //System.out.println("New Minimum = " +min);
              
                          for(int j=1; j<a.size(); j++)
                          {
                              if(a.get(j) == min+1)
                              {
                                  array1.add(a.get(j));
                                  status=true;
                                  //System.out.println("Adding array[j] > " +a.get(j));
                              }
              
                              else if(a.get(j) == min)
                              {
                                  array1.add(a.get(j));
                                  status=true;
                                  //System.out.println("Adding array[j] = " +a.get(j));
                              }
                          }
              
                          if(status==true)
                          {
                              array.add(array1.size());
                              array1.clear();
                          }
              
                          else if(status==false)
                          {
                              array1.clear();
                              //System.out.println("Clearing....");
                          }
                      }
              
                  //System.out.println(array1);
              
                  if(array1.size()==1)
                  {
                      array1.clear();
                  }
              
                  for(int size: array)
                  {
                      if(size > maxLength)
                      {
                          maxLength = size;
                      }
                  }
              
                  return maxLength;
              }
              
          • + 1 comment
            [deleted]
            • + 0 comments

              A simple tweak to above solution. Passed all the test cases! My code:

              https://www.hackerrank.com/challenges/picking-numbers/submissions/code/95999127

        • + 1 comment

          actually to sort things you need at least n*log n so it is the lower bound of complexity.

          • + 1 comment
            [deleted]
            • + 0 comments

              there is no need to sort the array.only the largest element in the array is to be found

        • + 5 comments

          What's wrong with something simple (Python) like:

          return max([sum((a.count(i), a.count(i+1))) for i in set(a)])

          • + 0 comments

            Hi, If you could please tell which concept it is of Python? I am new to Python .

          • + 0 comments

            yeah seriously that was my first idea

          • + 0 comments

            just remove sum function and make the comma plus . It will work.

          • + 0 comments

            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?

          • + 1 comment

            Best solution that I have found....Python is super cooool

            • + 0 comments

              thank you so much

        • + 1 comment

          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

          • + 0 comments

            No it gives correct answer of 6

        • + 0 comments

          it'll take O(nlogn) for sorting purpose how come it is linear?

        • + 1 comment

          **public static int pickingNumbers(List a) { // Write your code here int[] arr = new int[100]; for(int temp : a) { arr[temp]++; }

              int ans = 0;
              for(int i = 2 ; i < 100 ; i++) {
                  ans = Math.max(ans, arr[i]+arr[i-1]);
              }
              return ans;
          }**
          
          • + 1 comment

            could you please explain the basic logic of this code.

            • + 0 comments

              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.

      • + 0 comments

        Wow very good thinking

      • + 0 comments

        awesome man,nice approach

      • + 1 comment

        tl;dr How about some comments in your code??? Or variables that are more than one letter?

        • + 0 comments

          you can ask where ever you need an explanation

      • + 0 comments

        when we use complexity of n^3,they are giving error with message,"exceeded compile time".

      • + 0 comments

        why int k = 2*i ?

      • + 1 comment

        complexity of ur code is too high its O(n^2), isn't there any other way to reduce the complexity?

      • + 0 comments

        could have lower time complexity

      • + 0 comments

        thank u

    • + 0 comments

      We have to find absolute difference which will be either 0 or 1

    • + 0 comments

      same here

    • + 1 comment

      Same issue. I think the question is phrased wrongly.

      • + 1 comment

        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.

    • + 1 comment

      Yeah even i did the same thing.

      • + 0 comments

        Yes, same here.

    • + 0 comments

      haha lol same here.

    • + 0 comments

      Thanks! Without this discussion thread, I would not have known what was even being asked! :-)

    • + 0 comments

      yes same here

    • + 0 comments

      same

    • + 0 comments

      same here

    • + 0 comments

      same here!

    • + 0 comments

      yeah same here

    • + 0 comments

      yes.....same here

    • + 1 comment

      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.

    • + 14 comments

      I don't think the question is phrased in confusing way. Here is what I thought..

      int pickingNumbers(vector <int> a) {
          int z=0;
          int freq[100]={0};     
          for(int i=0;i<a.size();i++)
              freq[a[i]]++;
          
          for(int i=2;i<100;i++)
              z=max(z,freq[i]+freq[i-1]);
          return z;
      }
      

      I was here to check whether anyone has done it in O(1) space. I guess, no one:)

      • + 0 comments

        can you explain your logic? im still not clear how the max of sum of frequencies gets the answer!

      • + 2 comments

        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)

        • + 1 comment

          Brillient

          • + 0 comments

            thanks boiiii

        • + 0 comments

          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;
          }

      • + 0 comments

        Just Brilliant .

      • + 0 comments

        Very good. I am supposed to be used to frequency approach. You've enlightened me.

      • + 2 comments

        Here is the equivelent JavaScript code with comments:

        function pickingNumbers(a) {
            return a.reduce((count,val) => {
                // count occurance of each number
                count[val]++
                return count;
            }, new Array(100).fill(0))
                .reduce((max, val, index, count) =>{
                // max number of integers such that the absolute 
                // difference between any two is 1
                if (max < val + count[index + 1]){
                        max = val + count[index + 1];
                }
                return max;
            }, 0);
        }
        
        1. Fill an array with zeros
        2. Increment the count for each number found
        3. Find the maximum pair of numbers

        It should be noted that you could end up with a subset containing all the same integer.

        • + 0 comments

          Nice!! Nested Reduce for the win. I will study this!!

        • + 0 comments

          I also used reduce but sligthly another way ))

          function pickingNumbers(a) {    
              const count = a.sort((n1, n2) => n1 - n2).reduce((p, c) => {
                  p[c] = p[c] ?  p[c] + 1 : 1;
                  p[c - 1] = p[c - 1] ? p[c - 1] + 1 : undefined;        
                  return p;
              }, {});
              return Object.values(count).sort((n1, n2) => n2 - n1)[0];    
          }
          
      • + 0 comments

        Why shouldn't the last for loop start at i=1 instead of i=2?

      • + 2 comments

        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;
        

        }

        • + 0 comments

          Same approach, cleaner code:

          
          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
          }
          
        • + 0 comments

          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.

      • + 0 comments

        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.

      • + 0 comments

        GENIUS

      • + 0 comments

        But how do you know that the max number in the list will be 2 digits long only??

      • + 0 comments

        Fine coding skills! thanks.

      • + 0 comments

        thank u so much Excellent logic !

    • + 2 comments

      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;

      for (i=0;i<n;i++){
          np=0;
          nn=0;
          for(j=0;j<n;j++){
      
              if ((a[i]-a[j]>=0)&&(a[i]-a[j])<=1){
      
                  np++;            
      
              }
      
              if ((a[i]-a[j]<=0)&&(a[i]-a[j])>=-1){
      
                  nn++;            
      
              }
      
      
      
          }//jloop
      
          if(max<nn){
              max=nn;
          }
          if(max<np){
              max=np;
          }
      
      
      }// iloop
      
      return max;
      

      }

      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; }

      • + 1 comment

        your code is very helpful as well as efficient. it only needs o(1) space. thank u

        • + 0 comments

          No, Bro the code complexity will be o(n^2). See carefully.

      • + 0 comments

        its taking O(n*n) time. while it can be solved in O(nlgn) and O(n) too. Try it.

    • + 1 comment

      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

      • + 2 comments

        i used that algorithm and it worked fine. i don't think anything is wrong with it

        • + 0 comments

          i haven't tried the algorithm yet i just read the question and tried on my own first

    • + 0 comments

      on the same page

    • + 1 comment

      I'm not even going to try. Doesn't make sense. They should re-word it.

      • + 0 comments

        nice trick

    • + 0 comments

      Same here, though I never had this issue before...

    • + 0 comments

      Here is the solution https://www.hackerrank.com/challenges/picking-numbers/forum/comments/454763

    • + 0 comments

      i understood by reading examples

    • + 0 comments

      but the difference between 4 and 5 also 1 ryt?

    • + 0 comments

      Same here :(

    • + 0 comments

      same here!!!!

    • + 0 comments

      Select is the keyword here i guess. Just a tricky sentence.

    • + 1 comment

      Yes, in problem of 4, 5, 6, 3, 3, 1 answer should be 4. for {4, 5, 3, 3} but Hackerrank expected 3.

      • + 0 comments

        but difference of 3 and 5 is 2 so it doesnt fulfill the condition.

    • + 0 comments

      Same here!

    • + 0 comments

      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.

    • + 0 comments

      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

    • + 0 comments

      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.

    • + 0 comments

      me too

    • + 0 comments

      Yeah, correct!

    • + 0 comments

      Exact same here.

    • + 0 comments

      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.

    • + 0 comments

      same it bro

    • + 0 comments

      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

    • + 0 comments

      Same here Brother

    • + 0 comments

      same shit happened with me

    • + 0 comments

      def pickingNumbers(a): return max(len([x for x in a if 0 <= u - x <= 1]) for u in set(a))

    • + 0 comments

      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.

    • + 1 comment

      //C++ code

      int main()

      {int n,a[100],i,h[100],max=0;

      cin>>n;
      for(i=0;i<n;i++){
       cin>>a[i];
      }
      for(i=0;i<n;i++){
          h[i]=0;
      }
      for(i=0;i<n;i++){
          h[a[i]]++;
      }
      for(i=0;i<n-1;i++){
         if( max<h[i]+h[i+1])
         max=h[i]+h[i+1];
      }
      cout<<max;
      

      }

    • + 0 comments

      Same. Any solution to it?

    • + 0 comments

      Same here bro. Now that I come in discussion section I found this common to most of us all.

    • + 0 comments

      yes sir. I agree

    • + 0 comments

      same here!

    • + 0 comments

      include

      int main() { int n,a[1000000],b,i,f,k=0,v=0; scanf("%d",&n); for(i=0;i

          if(v<b)
          v=b;
      }
      printf("%d",v);
      return 0;
      

      }

    • + 1 comment

      same here

    • + 0 comments

      same with me .

    • + 0 comments

      Same for me also

    • + 0 comments

      same here

    • + 0 comments

      same here

    • + 0 comments

      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 :(

    • + 0 comments

      as i also though like that bro

    • + 0 comments

      same here

    • + 0 comments

      |4-5|=1

    • + 1 comment

      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}

      • + 0 comments

        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

    • + 0 comments

      same here

    • + 0 comments

      SAMEEEEE but it worked out anyways it was messy but ok

    • + 0 comments

      why the answer is 4,3,3 not 4,5,3,3 ? |4-5|=1 right?

    • + 0 comments

      Yes, it's a badly worded question.

    • + 0 comments

      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.

    • + 0 comments

      2 versions of Python code solving in O(n) time:

      from collections import Counter
      
      def pickingNumbers(a):
          c = Counter(a)
          return max( c[n]+c.get(n-1,0) for n in c )
      

      version 2:

      def pickingNumbers1(a):
          a = sorted(a)
          m=i=0
          while i<len(a):
              s=i
              while i<len(a) and abs(a[i]-a[s])<=1: i+=1
              m = i-s if i-s>m else m
          return m
      
    • + 0 comments

      Yup. Read it as contiguous arrays in the original string.

    • + 0 comments

      guess i am the only one

    • + 0 comments

      yes same.

    • + 0 comments

      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!

    • + 0 comments

      Me too misread it. It's written to find the longest subarray but in explanation part it is taking multiset(i.e. sub-sequence).

    • + 0 comments

      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;

    • + 0 comments

      me too.

    • + 0 comments

      same

    • + 0 comments

      same here

    • + 0 comments

      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.

    • + 0 comments

      me too

    • + 1 comment

      Same here....

      • + 0 comments

        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

    • + 0 comments

      same here. confusing quesiton

    • + 0 comments

      Yeah that's why i am not able to pass one of the testcases

    • + 0 comments

      Ya.. Same i realized ... i think in question , the correct intrepretaion is hard to find as the question asked.

    • + 0 comments

      yes, and I still don't get it tbh

    • + 0 comments

      same here, even after they paraphrased it, it still took me hours to figure it out