
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count Elements Less Than or Equal to a Given Value in a Sorted Rotated Array in C++
A sorted and rotated array is an array that is sorted in ascending or descending order and then rotated either left or right by a specific number of elements. There should exist exactly one pivot point around which the array is rotated.
Example
Here is an example of counting elements less than or equal to a given value in the sorted and rotated array:
Case 1 The given array is: 30 40 50 10 20 Given value = 25 Output Count: 2 (10, 20) Case 2 The given array is: 10 20 30 40 50 Given value = 35 Output Count: 3 (10, 20, 30)
Counting elements less than or equal to a given value in a sorted rotated array
In this article, our task is to count the number of elements that are less than or equal to a given value in the sorted and rotated array. We will use the following two approaches mentioned below:
Using Linear Search
The linear search is a sequential search algorithm where every element of the array is traversed and compared with the key element to be found. Below is an implementation of the linear search algorithm to count elements less than or equal to a given value in the sorted and rotated array:
- In linear search, we iterate over all the elements of the array using the for loop and check if the current element is less than or equal to the given value.
- If we find an element less than or equal to the target value, we increase the count value.
Example
Here is an example of counting elements less than or equal to a given value in the sorted and rotated array using the linear search:
#include <iostream> using namespace std; int countElements(int arr[], int n, int target) { int count = 0; for (int i = 0; i < n; i++) { if (arr[i] <= target) { count++; } } return count; } int main() { int arr[] = {30, 40, 50, 10, 20}; int n = sizeof(arr) / sizeof(arr[0]); int target = 25; cout << "Array: "; for (int i = 0; i < n; i++) cout << arr[i] << " "; cout << endl; cout << "Given value: " << target << endl; cout << "Count of elements <= " << target << ": " << countElements(arr, n, target) << endl; return 0; }
The output of the above code is as follows:
Array: 30 40 50 10 20 Given value: 25 Count of elements <= 25: 2
Using Binary Search
In this approach, we have used the binary search to count elements less than or equal to a given value. Using binary search, we divide the array by middle element into left and right sub-arrays and count in each half.
- First, find the middle element of the given array. If the middle element is less than or equal to the target element then, set the count to 1.
- Then we compare the lowest array element with the middle element to check which half of the array is sorted.
- If the left half of the array is sorted, we compare each element of the left with the target element and then recursively call the countEle() function to count the elements in the right half.
- Similarly, if the right half is sorted, we recursively call the countEle() function to count the elements in the left half and compare each element in the right half with the target element.
- In the above two steps, we increase the counter if we find an element less than or equal to the target element, and the total count is returned.
Example
Here is an example of counting elements less than or equal to a given value in the sorted and rotated array using the binary search:
#include <bits/stdc++.h> using namespace std; int countEle(int *arr, int low, int high, int target) { if (low > high) { return 0; } int mid = low + (high - low) / 2; int count = 0; if (arr[mid] <= target) { count = 1; } // If left half is sorted if (arr[low] <= arr[mid]) { // Counting elements in left sub-array for (int i = low; i < mid; i++) { if (arr[i] <= target) count++; } // Counting in right half count += countEle(arr, mid + 1, high, target); } // If right half is sorted else { //Counting elements in left sub-array count += countEle(arr, low, mid - 1, target); // Counting in right half for (int i = mid + 1; i <= high; i++) { if (arr[i] <= target) count++; } } return count; } int main() { int arr[] = {30, 40, 50, 10, 20}; int n = sizeof(arr) / sizeof(arr[0]); int target = 25; cout << "Given array: "; for (int i = 0; i < n; i++) { cout << arr[i] << " "; } cout << endl; cout << "Target value: " << target << endl; cout << "Number of elements less than or equal to " << target << " is: " << countEle(arr, 0, n - 1, target) << endl; return 0; }
The output of the above code is as follows:
Given array: 30 40 50 10 20 Target value: 25 Number of elements less than or equal to 25 is: 2