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.
Picking Numbers
Picking Numbers
+ 0 comments Java Solution with explanation
Sort the list given
Create a count list to store the subarray
for i = 0 in a as soon as i is less than the size of a
- create a new temporal list (this will reset when i changes value to not count the previous item)
- add to the temp the value of a at position i
- compare i with i+1( j ), so: for j = i+1 as soon as j is less than size of a
- if the absolute value of a at pisition i minus the value of a a at position j is less than 2
- add j to temporal
- add to count the size of temp
- if the absolute value of a at pisition i minus the value of a a at position j is less than 2
- then return the max value of the list (use Collections.max()
or
- Sort the count list
- return counter at last index position
public static int pickingNumbers(List<Integer> a) { // Write your code here Collections.sort(a); List<Integer> count = new ArrayList<>(); for (int i = 0; i < a.size()-1; i++) { List<Integer> temp = new ArrayList<>(); temp.add(a.get(i)); for(int j = i+1; j < a.size(); j++){ if (Math.abs(a.get(i)-a.get(j)) < 2){ temp.add(j); } } count.add(temp.size()); } return Collections.max(count); }
+ 0 comments python solution
def pickingNumbers(l): t=[] for i in range(len(l)): for j in range(i+1,len(l)): a=[] a.append(l[i]) for z in range(j,len(l)): if(len(a)>1): m=a[0] n=a[1] if(m==n): if(l[z]==m+1): m=l[z] a[0]=m else: n=l[z] a[0]=n if((abs(l[i]-l[z])==0 or abs(l[i]-l[z])==1) and (l[z]==m or l[z]==n)): a.append(l[z]) else: if(abs(l[i]-l[z])==0 or abs(l[i]-l[z])==1): a.append(l[z]) t.append(len(a)) return(max(t))
+ 0 comments def pickingNumbers(a): d={} for x in a: d[x] = d[x]+1 if x in d else 1 result=max(d.values()) keys=sorted(d.keys()) for i in range(1,len(keys)): if keys[i]-keys[i-1]==1: val=d[keys[i]]+d[keys[i-1]] if val>result: result=val return result
+ 0 comments === Python Solution ===
# TC - O(nlogn) ; SC - O(1) curr_length, max_length = 0,0 min_num = float('inf') # Sorting the array in ascending order a.sort() # Looping through the array and track the difference and minimum for i in range(len(a)-1): if a[i+1] - a[i] < 2 and a[i+1] - min_num < 2: curr_min = min(min_num,a[i]) if curr_min <= min_num: min_num = curr_min curr_length += 1 max_length = max(curr_length,max_length) else: curr_length = 0 curr_min, min_num = float('inf'),float('inf') return max_length + 1
+ 0 comments c# version
public static int pickingNumbers(List<int> a) { var d = new Dictionary<int, int>(); // [n,q] foreach(int n in a) { int q; if (d.TryGetValue(n, out q)) { d[n] = q + 1; } else { d[n] = 1; } } int mx = 0; foreach(var x in d) { int xk = x.Key; int xv = x.Value; int q = 0; foreach(var y in d) { int yk = y.Key; if ( xk != yk && Math.Abs(xk - yk) < 2 ) { q = xv + y.Value; } } if (xv > q) { q = xv; } if (q > mx) { mx = q; } } return mx; }
Load more conversations
Sort 2145 Discussions, By:
Please Login in order to post a comment