• + 0 comments

    Brute force approach is too slow the best approach or the solution for this is the frequency array approach (C++ solution)

    `int pickingNumbers(vector a) {

    int maxLength = 0;

    //brute force approach on n^2
    /for (int i = 0; i < a.size(); i++) { int count = 0;
    for (int j = 0; j < a.size(); j++) { if (a[j] == a[i] || a[j] == a[i] + 1) { count++; } } maxLength = max(maxLength, count); }
    /

    //frequency array approach vector freq(101, 0); for (int i = 0; i < a.size(); i++) { freq[a[i]]++; // Increment the count for number a[i] }

    for (int i = 1; i < 100; i++) { maxLength = max(maxLength, freq[i] + freq[i + 1]); } return maxLength;