recategorized by
374 views
3 votes
3 votes
Explain the following code..especially the loop part #1 & #2 of below code:-
[Code is printing 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].
int main() {
   int n,k,max = 0;
    cin>>n;
    int a[100]={0};
    for(int i = 0;i<n;i++){                 //#1
        cin>>k;
        a[k]++;
    }
    for(int i = 0;i<n-1;i++){              //#2
        if(a[i]+a[i+1]> max) max = a[i]+a[i+1];
    }
    cout<<max;
    return 0;
}
recategorized by

Please log in or register to answer this question.

Related questions

0 votes
0 votes
4 answers
1
0 votes
0 votes
1 answer
2
iarnav asked May 27, 2017
289 views
When are C++ copy constructors, assignment operators, and destructors, respectively,invoked?
0 votes
0 votes
1 answer
3
rsansiya111 asked Oct 7, 2022
746 views
Which one of the following fundamental features of the OOPs is not supported by 𝐶++?(a) Persistence(b) Delegation(c) Genericity(d) Data abstraction