Sort by

recency

|

2560 Discussions

|

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

    Perl:

    sub pickingNumbers {
        my $aa = shift;
    
        my $max = 0;
        my @a = sort {$a <=> $b} @$aa;
        for (my $i = 0; $i < scalar(@a); $i++) {
            my $cnt = 0;
            for (my $j = $i+1; $j < scalar(@a); $j++) {
                if (abs($a[$i] - $a[$j]) <= 1) {
                    $cnt++;
                }            
            }
            $max = $cnt if ($cnt > $max);
        }
        return ++$max;
    }
    
  • + 0 comments

    Kotlin:

    fun pickingNumbers(a: Array<Int>): Int {
        var max=0
        a.sort()
        for(i in 0 until a.size){
            var currentMax=0
            for(j in i+1 until a.size){
                val diff=Math.abs(a[i]-a[j])
                if(diff<=1){
                    currentMax++
                }
            }
            if(currentMax>max) max=currentMax
        }
        return max+1
    
    }
    
  • + 0 comments

    I think test cases are incorrect

    JS:

    function pickingNumbers(a) {
        let longestSubarrayLen = 0
        let i = 0;
        
        while (i < a.length) {
            let arr = [a[i]];
            const visitedIndexes = [];
            let j = i+1;
            
            while (j < a.length) {
                const current = a[j];
                const last = arr[arr.length-1]
                const prev = arr.length > 1 ? arr[arr.length - 2] : undefined;
                if (Math.abs(current - last) <=1 && !visitedIndexes.includes(j)) {
                    arr.push(a[j]);
                    visitedIndexes.push(j);
                    if (longestSubarrayLen < arr.length) {
                        longestSubarrayLen = arr.length
                        console.log({arr})
                    }
                } else if (prev && j === a.length - 1) {
                    j = visitedIndexes[visitedIndexes.length - 2] || i+1;
                    arr.pop();
                }
                j++
            }
            i++;
        }
        
        return longestSubarrayLen;
    }
    
  • + 0 comments

    RUST:

    fn pickingNumbers(a: &[i32]) -> i32 {
        let mut a_sorted: Vec<i32> = a.to_vec();
        a_sorted.sort_unstable();
    
        let mut max_counter = 0;
        let mut counter = 0;
        let mut checked = a_sorted[0];
        
        for num in a_sorted {
            if (checked - num).abs() > 1 {
                checked = num;
                counter = 1;
            } else {
                counter += 1;
            }
            max_counter = max_counter.max(counter);
        }
        max_counter
    }