Given a sorted array of distinct integers that has been rotated at some pivot, and a value x, determine how many elements in the array are less than or equal to x.
Examples:
Input: arr[] = [6, 10, 12, 15, 2, 4, 5], x = 14
Output: 6
Explanation: 2, 4, 5, 6, 10, 12 are smaller than 14.Input: arr[] = [4, 5, 8, 1, 3], x = 6
Output: 4
Explanation: 1, 3, 4, 5 are smaller than 6.
Table of Content
[Naive Approach] Using Linear Search - O(n) Time and O(1) Space
Iterating through the entire array and counting each element that is less than or equal to x. Since the array is rotated, it cannot be assumed to be sorted throughout, so we must check every element individually.
#include <iostream>
#include <vector>
using namespace std;
// Function to count elements less than or equal to x
int countLessEqual(vector<int>& arr, int x) {
int count = 0;
// Traverse the array and increment count if element ≤ x
for (int num : arr) {
if (num <= x)
count++;
}
return count;
}
int main() {
vector<int> arr = {6, 10, 12, 15, 2, 4, 5};
int x = 14;
cout << countLessEqual(arr, x) << endl;
return 0;
}
public class GfG {
// Function to count elements less than or equal to x
static int countLessEqual(int[] arr, int x) {
int count = 0;
// Traverse the array and increment count if element ≤ x
for (int num : arr) {
if (num <= x)
count++;
}
return count;
}
public static void main(String[] args) {
int[] arr = {6, 10, 12, 15, 2, 4, 5};
int x = 14;
System.out.println(countLessEqual(arr, x));
}
}
def countLessEqual(arr, x):
count = 0
# Traverse the array and increment count if element ≤ x
for num in arr:
if num <= x:
count += 1
return count
if __name__ == "__main__":
arr = [6, 10, 12, 15, 2, 4, 5]
x = 14
print(countLessEqual(arr, x))
using System;
class GfG {
// Function to count elements less than or equal to x
static int countLessEqual(int[] arr, int x) {
int count = 0;
// Traverse the array and increment count if element ≤ x
foreach (int num in arr) {
if (num <= x)
count++;
}
return count;
}
static void Main() {
int[] arr = {6, 10, 12, 15, 2, 4, 5};
int x = 14;
Console.WriteLine(countLessEqual(arr, x));
}
}
function countLessEqual(arr, x) {
let count = 0;
// Traverse the array and increment count if element ≤ x
for (let num of arr) {
if (num <= x)
count++;
}
return count;
}
// Driver Code
let arr = [6, 10, 12, 15, 2, 4, 5] ;
let x = 14 ;
console.log(countLessEqual(arr, x)) ;
Output
6
[Expected Approach] Using Binary Search - O(log(n)) Time and O(1) Space
Modified binary search to count elements ≤ x, leveraging the rotated and partially sorted nature of the array. First, find the pivot (smallest element's index) to identify the two sorted subarrays. Then, perform binary search separately on each sorted segment to count how many elements are ≤ x. Add the counts from both segments to get the final result.
#include <iostream>
#include <vector>
using namespace std;
// Standard binary search to count
// elements ≤ x in a sorted subarray
int countInSorted(vector<int>& arr, int left, int right, int x) {
int l = left, r = right, res = left - 1;
while (l <= r) {
int mid = l + (r - l) / 2;
if (arr[mid] <= x) {
res = mid;
l = mid + 1;
} else {
r = mid - 1;
}
}
return res - left + 1;
}
// Function to find index of the smallest
// element (pivot) in rotated array
int findPivot(vector<int>& arr) {
int n = arr.size();
int l = 0, r = n - 1;
while (l < r) {
int mid = l + (r - l) / 2;
if (arr[mid] > arr[r]) {
l = mid + 1;
} else {
r = mid;
}
}
// index of the smallest element
return l ;
}
// Main function to count elements ≤ x
// in a rotated sorted array
int countLessEqual(vector<int>& arr, int x) {
int n = arr.size();
int pivot = findPivot(arr);
// Search in both sorted parts
int count1 = countInSorted(arr, 0, pivot - 1, x);
int count2 = countInSorted(arr, pivot, n - 1, x);
return count1 + count2;
}
int main() {
vector<int> arr = {6, 10, 12, 15, 2, 4, 5};
int x = 14;
cout << countLessEqual(arr, x) << endl;
return 0;
}
public class GfG {
// Standard binary search to count
// elements ≤ x in a sorted subarray
static int countInSorted(int[] arr, int left, int right, int x) {
int l = left, r = right, res = left - 1;
while (l <= r) {
int mid = l + (r - l) / 2;
if (arr[mid] <= x) {
res = mid;
l = mid + 1;
} else {
r = mid - 1;
}
}
return res - left + 1;
}
// Function to find index of the smallest
// element (pivot) in rotated array
static int findPivot(int[] arr) {
int n = arr.length;
int l = 0, r = n - 1;
while (l < r) {
int mid = l + (r - l) / 2;
if (arr[mid] > arr[r]) {
l = mid + 1;
} else {
r = mid;
}
}
// index of the smallest element
return l;
}
// Main function to count elements ≤ x
// in a rotated sorted array
static int countLessEqual(int[] arr, int x) {
int n = arr.length;
int pivot = findPivot(arr);
// Search in both sorted parts
int count1 = countInSorted(arr, 0, pivot - 1, x);
int count2 = countInSorted(arr, pivot, n - 1, x);
return count1 + count2;
}
public static void main(String[] args) {
int[] arr = {6, 10, 12, 15, 2, 4, 5};
int x = 14;
System.out.println(countLessEqual(arr, x)) ;
}
}
# Standard binary search to count
# elements ≤ x in a sorted subarray
def countInSorted(arr, left, right, x):
l, r = left, right
res = left - 1
while l <= r:
mid = l + (r - l) // 2
if arr[mid] <= x:
res = mid
l = mid + 1
else:
r = mid - 1
return res - left + 1
# Function to find index of the smallest
# element (pivot) in rotated array
def findPivot(arr):
n = len(arr)
l, r = 0, n - 1
while l < r:
mid = l + (r - l) // 2
if arr[mid] > arr[r]:
l = mid + 1
else:
r = mid
# index of the smallest element
return l
# Main function to count elements ≤ x in a rotated sorted array
def countLessEqual(arr, x):
n = len(arr)
pivot = findPivot(arr)
# Search in both sorted parts
count1 = countInSorted(arr, 0, pivot - 1, x)
count2 = countInSorted(arr, pivot, n - 1, x)
return count1 + count2
arr = [6, 10, 12, 15, 2, 4, 5]
x = 14
print(countLessEqual(arr, x))
using System;
class GfG {
// Standard binary search to count
// elements ≤ x in a sorted subarray
static int countInSorted(int[] arr, int left, int right, int x) {
int l = left, r = right, res = left - 1;
while (l <= r) {
int mid = l + (r - l) / 2;
if (arr[mid] <= x) {
res = mid;
l = mid + 1;
} else {
r = mid - 1;
}
}
return res - left + 1;
}
// Function to find index of the smallest
// element (pivot) in rotated array
static int findPivot(int[] arr) {
int n = arr.Length;
int l = 0, r = n - 1;
while (l < r) {
int mid = l + (r - l) / 2;
if (arr[mid] > arr[r]) {
l = mid + 1;
} else {
r = mid;
}
}
// index of the smallest element
return l;
}
// Main function to count elements ≤ x
// in a rotated sorted array
static int countLessEqual(int[] arr, int x) {
int n = arr.Length;
int pivot = findPivot(arr);
// Search in both sorted parts
int count1 = countInSorted(arr, 0, pivot - 1, x);
int count2 = countInSorted(arr, pivot, n - 1, x);
return count1 + count2;
}
static void Main() {
int[] arr = {6, 10, 12, 15, 2, 4, 5};
int x = 14;
Console.WriteLine(countLessEqual(arr, x));
}
}
// Standard binary search to count
// elements ≤ x in a sorted subarray
function countInSorted(arr, left, right, x) {
let l = left, r = right, res = left - 1;
while (l <= r) {
let mid = Math.floor(l + (r - l) / 2);
if (arr[mid] <= x) {
res = mid;
l = mid + 1;
} else {
r = mid - 1;
}
}
return res - left + 1;
}
// Function to find index of the smallest
// element (pivot) in rotated array
function findPivot(arr) {
let n = arr.length;
let l = 0, r = n - 1;
while (l < r) {
let mid = Math.floor(l + (r - l) / 2);
if (arr[mid] > arr[r]) {
l = mid + 1;
} else {
r = mid;
}
}
// index of the smallest element
return l;
}
// Main function to count elements ≤ x
// in a rotated sorted array
function countLessEqual(arr, x) {
let n = arr.length;
let pivot = findPivot(arr);
// Search in both sorted parts
let count1 = countInSorted(arr, 0, pivot - 1, x);
let count2 = countInSorted(arr, pivot, n - 1, x);
return count1 + count2;
}
// Driver Code
let arr = [6, 10, 12, 15, 2, 4, 5];
let x = 14;
console.log(countLessEqual(arr, x));
Output
6