Sort by

recency

|

2602 Discussions

|

  • + 0 comments
    function pickingNumbers(a: number[]): number {
        
        // sort arr in ascding order
        a.sort((x,y)=>x-y);
        
        let ans = 0;
        let window_start = 0;
        
        for (let window_end = 0; window_end < a.length; window_end++) {
            
            while (a[window_end] - a[window_start] > 1) {
                window_start ++;
            }
            
             const window_size = window_end - window_start +1;
             
             ans = Math.max(window_size, ans);
        }
        
        return ans;
    }
    
  • + 0 comments

    As many exercises on this site, the description of the problem is misleading: it gives you the impression that data in the array have to stay unordered... but in reality the problem expects for you to sort the array.

    def pickingNumbers(a):
        a.sort()
        cnt = 1
        cmx = 0
        i = 1
        s = a[0]
        while i<len(a):
            t = a[i]
            if abs(t-s)<=1:
                cnt += 1
            else:
                s = a[i]
                cmx = max(cmx,cnt)
                cnt = 1
            i += 1
        if i == len(a):
            cmx = max(cmx, cnt)
        return cmx
    
  • + 0 comments

    The description is ambiguous. "subarray → consecutive sections" "Two elements → two adjacent elements in the section" Therefore, you may misunderstand that the array {4,5,6,7,8} is the correct answer.

  • + 0 comments

    This problem is not worded in a correct format to match the results desired by the problem itself, and just like when you realize you need a fence company to fix issues properly, working through the problem as written will give you the wrong answers.

  • + 1 comment

    import java.io.; import java.util.; import java.util.stream.*;

    class Result {

    /*
     * Complete the 'pickingNumbers' function below.
     *
     * The function is expected to return an INTEGER.
     * The function accepts INTEGER_ARRAY a as parameter.
     */
    
    public static int pickingNumbers(List<Integer> a) {
        int[] freq = new int[101]; // since 0 <= a[i] <= 100
        for (int num : a) {
            freq[num]++;
        }
    
        int maxLength = 0;
        for (int i = 1; i < freq.length; i++) {
            maxLength = Math.max(maxLength, freq[i] + freq[i - 1]);
        }
    
        return maxLength;
    }
    

    }

    public class Solution { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        int n = Integer.parseInt(bufferedReader.readLine().trim());
    
        List<Integer> a = Arrays.stream(bufferedReader.readLine().trim().split(" "))
            .map(Integer::parseInt)
            .collect(Collectors.toList());
    
        int result = Result.pickingNumbers(a);
    
        bufferedWriter.write(String.valueOf(result));
        bufferedWriter.newLine();
    
        bufferedReader.close();
        bufferedWriter.close();
    }
    

    }