Count of Smaller or Equal Elements in Sorted Array in C++



In this article, we have a sorted array of integers. Our task is to find the count of elements of the given sorted array that are less than or equal to the given value K.

Example

Here are some examples of counting the array elements smaller than the given target element:

Input:
arr = {6, 12, 16, 23, 32, 45, 48, 50}
target = 40
Output: 5

Input:
arr = {6, 12, 15, 20, 32, 45, 48, 50}
target = 20
Output: 4

Counting of smaller or equal elements in the sorted array

Here is a list of approaches for counting the number of elements smaller than the target element in the sorted array:

To count the number of elements smaller or equal to the given target elements in a sorted array, we have used the linear search algorithm. In linear search, we traverse each element and compare it with the key element to be found.

  • Here, we traverse each array element to compare it with the given target element.
  • For each element less than the target element, we increase the counter by 1.
  • After the completion of the for loop, we return the total count.

Example

The example given below uses the linear search for counting the number smaller than the given target:

#include <iostream>
using namespace std;

int countMin(int arr[], int n, int target)
{
   int count = 0;
   for (int i = 0; i < n; i++)
   {
      if (arr[i] <= target)
         count++;
      else
         break;
   }
   return count;
}

int main()
{
   int arr[] = {10, 25, 43, 49, 55, 63, 87};
   int n = sizeof(arr) / sizeof(arr[0]);
   int target = 45;

   cout << "Given array is: ";
   for (int i = 0; i < n; i++)
   {
      cout << arr[i] << " ";
   }

   cout << "\nNumber of elements less than " << target
        << " is: " << countMin(arr, n, target) << endl;
   return 0;
}

The output of the above code is as follows:

Given array is: 10 25 43 49 55 63 87 
Number of elements less than 45 is: 3

In this approach, we have used the binary search approach for counting the elements less than the target element. In the binary search, we compare the middle element with the target element. Based on the comparison with the middle element, we then search in the left or right half of the array.

  • We have defined two pointer variables low and high that initially point to the starting and last index of the array.
  • Then, we calculate the middle element of the array and compare the target with the middle element.
  • If the middle element is less than the target element then we store the middle index in the counter variable and increase the low pointer by 1.
  • If the middle element is not less than the target element, then we decrease the high pointer by 1.
  • We repeat the above three steps until the low pointer reaches the high pointer and return the count.

Example

The following example uses the above steps for implementing binary search for counting elements less than the target element:

#include <iostream>
using namespace std;

int countMin(int arr[], int n, int target)
{
   int low = 0, high = n - 1, count = -1;
   while (low <= high)
   {
      int mid = low + (high - low) / 2;
      if (arr[mid] <= target)
      {
         count = mid;
         low = mid + 1;
      }
      else
      {
         high = mid - 1;
      }
   }
   return count + 1;
}

int main()
{
   int arr[] = {10, 25, 43, 49, 55, 63, 87};
   int n = sizeof(arr) / sizeof(arr[0]);
   int target = 45;

   cout << "Given array is: ";
   for (int i = 0; i < n; i++)
   {
      cout << arr[i] << " ";
   }

   cout << "\nNumber of elements less than " << target
        << " is: " << countMin(arr, n, target) << endl;
   return 0;
}

The output of the above code is as follows:

Given array is: 10 25 43 49 55 63 87 
Number of elements less than 45 is: 3

Using upper_bound() Function

The upper_bound() function returns a pointer pointing to the first element in the sorted range that is greater than the given value. Here, using the upper_bound() function, we get the address of the first element greater than the target element. Then, by subtracting the starting index, we get the count of the elements less than the target element.

Example

In this example, we have used the upper_bound() function to count the elements smaller than the target element.

#include <iostream>
#include <algorithm>
using namespace std;

int countMin(int arr[], int n, int target)
{
   int count = upper_bound(arr, arr + n, target) - arr;
   return count;
}

int main()
{
   int arr[] = {10, 25, 43, 49, 55, 63, 87};
   int n = sizeof(arr) / sizeof(arr[0]);
   int target = 45;

   cout << "Given array is: ";
   for (int i = 0; i < n; i++)
   {
      cout << arr[i] << " ";
   }

   cout << "\nNumber of elements less than " << target
        << " is: " << countMin(arr, n, target) << endl;
   return 0;
}

The output of the above code is as follows:

Given array is: 10 25 43 49 55 63 87 
Number of elements less than 45 is: 3

Complexity Comparison

Here is a comparison of the time and space complexity of all the above approaches.

Approach Time Complexity Space Complexity
Linear Search O(n) O(1)
Binary Search O(log n) O(1)
upper_bound() Function O(log n) O(1)
Updated on: 2025-06-10T17:46:52+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started