Count of smaller or equal elements in the sorted array in C++


We are given an array of integers. The goal is to find the count of elements of an array which are less than or equal to the given value K.

Input 

Arr[]= { 1, 2, 3, 14, 50, 69, 90 } K=12

Output 

Numbers smaller or equal to K: 3

Explanation 

Numbers 1,2,3 is smaller or equal to 12.

Input 

Arr[]= { 12, 13, 13, 13, 14, 50, 54, 100 } K=14

Output 

Numbers smaller or equal to K: 5

Explanation 

Numbers 12, 13, 14 are smaller or equal to 14.

Naive Approach

Approach used in the below program is as follows

  • We take the integer array Arr[] and K.

  • Function smallorEqual(int arr[],int k,int len) returns the count of elements of arr[] that are small or equal to K

  • Take the initial variable count as 0 for such numbers.

  • Traverse array of numbers using for loop. i=0 to i<len

  • Now for each number arr[i], if it is <=k, increment count.

  • At the end loop count will have a total number which satisfies the condition.

  • Return the count as result.

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int smallorEqual(int arr[],int k,int len){
   int count = 0;
   for (int i = 0; i < len; i++){
      if(arr[i]<=k)
         { count++; }
      else
         { break; }
   }
   return count;
}
int main(){
   int Arr[] = { 1,5,11,12,19,21,32,53,70,100 };
   int K = 21;
   int Length= sizeof(Arr)/sizeof(Arr[0]);
   cout <<"Numbers smaller or equal to K: "<<smallorEqual(Arr,K,Length);
   return 0;
}

Output

If we run the above code it will generate the following output −

Numbers smaller or equal to K: 6

Efficient Approach ( Using binary search )

Approach used in the below program is as follows

  • We take the integer array Arr[] and K.

  • Function binarySearch(int arr[],int k,int len) returns the count of elements of arr[] that are small or equal to K

  • Take indexes low=0, high=len-1 and mid=(low+high)/2;/p>

  • Take variable index=-1;

  • Using while loop, till low<=high

  • Check value of arr[mid]. If it is <= k. Then index=mid. New low=mid+1

  • Otherwise new high=mid-1.

  • At the end of the while loop index will index of last number<=k.

  • Return the index+1 as result because array indexing starts from 0 and all numbers from index 0 to index are less than k.

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int binarySearch(int arr[],int k,int len){
   int low = 0;
   int high = len -1;
   int mid = (high+low)/2;
   int index = -1;
   while(low <= high){
      mid =( low + high ) / 2;
      if(arr[mid] <= k){
         index = mid;
         low = mid+1;
      }
      else{
         high=mid-1;
      }
   }
   return (index+1);
}
int main(){
   int Arr[] = { 1,5,11,12,19,21,32,53,70,100 };
   int K = 21;
   int Length= sizeof(Arr)/sizeof(Arr[0]);
   cout <<"Numbers smaller or equal to K: "<<binarySearch(Arr,K,Length);
   return 0;
}

Output

If we run the above code it will generate the following output −

Numbers smaller or equal to K: 6

Updated on: 31-Oct-2020

893 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements