C program to print the number of subset whose elements have difference 0 or 1
Given an array of integers, find and print the maximum number of integers you can select from the array such that the absolute difference between any two of the chosen integers is less than or equal to 1.
For example, if your array is a = [1,1,2,2,4,4,5,5,5]
You can create two subarrays meeting the criterion: [1,1,2,2] and [4,4,5,5,5]. The maximum length subarray has 5 elements.
Input format:
The first line contains a single integer a, the size of the array b.
The second line contains a space-separated integers b[i].
Output format:
A single integer denoting the maximum number of integers you can choose from the array such that the absolute difference between any two of the chosen integers is <=1.
Constraint:
2<=a<=100
Example:
Input:
6
4 6 5 3 3 1
Output:
3
Description:
We choose the following multiset of integers from the array:{4,3,3}. Each pair in the multiset has an absolute difference <=1(i.e., |4-3|=1 and |3-3|=0 ), So we print the number of chosen integers, 3 - as our answer.
Solution:
Output
need an explanation for this answer? contact us directly to get an explanation for this answer