在已排序的旋转数组中计算小于或等于给定值的元素
给定一个在某个点旋转的n 个不同整数的排序数组。给定一个值x 。问题是计算数组中小于或等于x的所有元素。
例子:
Input : arr[] = {4, 5, 8, 1, 3},
x = 6
Output : 4
Input : arr[] = {6, 10, 12, 15, 2, 4, 5},
x = 14
Output : 6
朴素的方法:一一遍历数组的所有元素并计算小于或等于x的元素。时间复杂度 O(n)。
高效方法:修改后的二进制搜索的先决条件,它可以返回小于或等于arr[l…h]排序范围内的给定值的最大元素的索引。
有关所需的修改二进制搜索,请参阅此帖子:
脚步:
- 查找旋转排序数组中最小元素的索引。参考这篇文章。让它成为min_index 。
- 如果x <= arr[n-1] ,则借助修改后的二分查找,在排序范围arr[min_index…n-1]中找到小于或等于x的最大元素的索引。
让它成为index1 。现在, count = index1 + 1 – min_index。 - 如果0 <= (min_index -1) && x <= arr[min_index – 1] ,在modified的帮助下找到排序范围arr[0…min_index-1]中小于或等于x的最大元素的索引二进制搜索。让它成为index2 。现在, count = n – min_index + index2 + 1。
- 否则计数= n。
C++
// C++ implementation to count elements less than or
// equal to a given value in a sorted rotated array
#include
using namespace std;
// function to find the minimum element's index
int findMinIndex(int arr[], int low, int high)
{
// This condition is needed to handle the case when
// array is not rotated at all
if (high < low) return 0;
// If there is only one element left
if (high == low) return low;
// Find mid
int mid = (low + high) / 2;
// Check if element (mid+1) is minimum element. Consider
// the cases like {3, 4, 5, 1, 2}
if (mid < high && arr[mid+1] < arr[mid])
return (mid + 1);
// Check if mid itself is minimum element
if (mid > low && arr[mid] < arr[mid - 1])
return mid;
// Decide whether we need to go to left half or right half
if (arr[high] > arr[mid])
return findMinIndex(arr, low, mid-1);
return findMinIndex(arr, mid+1, high);
}
// function returns the index of largest element
// smaller than equal to 'x' in 'arr[l...h]'.
// If no such element exits in the given range,
// then it returns l-1.
int binary_search(int arr[], int l, int h, int x)
{
while (l <= h)
{
int mid = (l+h) / 2;
// if 'x' is less than or equal to arr[mid],
// then search in arr[mid+1...h]
if (arr[mid] <= x)
l = mid + 1;
// else search in arr[l...mid-1]
else
h = mid - 1;
}
// required index
return h;
}
// function to count elements less than
// or equal to a given value
int countEleLessThanOrEqual(int arr[], int n, int x)
{
// index of the smallest element in the array
int min_index = findMinIndex(arr, 0, n-1);
// if largest element smaller than or equal to 'x' lies
// in the sorted range arr[min_index...n-1]
if (x <= arr[n-1])
return (binary_search(arr, min_index, n-1, x) + 1 - min_index);
// if largest element smaller than or equal to 'x' lies
// in the sorted range arr[0...min_index-1]
if ((min_index - 1) >= 0 && x <= arr[min_index - 1])
return (n - min_index + binary_search(arr, 0, min_index-1, x) + 1);
// else all the elements of the array
// are less than 'x'
return n;
}
// Driver program to test above
int main()
{
int arr[] = {6, 10, 12, 15, 2, 4, 5};
int n = sizeof(arr) / sizeof(arr[0]);
int x = 14;
cout << "Count = "
<< countEleLessThanOrEqual(arr, n, x);
return 0;
}
Java
// Java implementation to count elements
// less than or equal to a given
// value in a sorted rotated array
class GFG {
// function to find the minimum
// element's index
static int findMinIndex(int arr[], int low, int high)
{
// This condition is needed to handle
// the case when array is not rotated at all
if (high < low)
return 0;
// If there is only one element left
if (high == low)
return low;
// Find mid
int mid = (low + high) / 2;
// Check if element (mid+1) is
// minimum element. Consider
// the cases like {3, 4, 5, 1, 2}
if (mid < high && arr[mid + 1] < arr[mid])
return (mid + 1);
// Check if mid itself is minimum element
if (mid > low && arr[mid] < arr[mid - 1])
return mid;
// Decide whether we need to go to
// left half or right half
if (arr[high] > arr[mid])
return findMinIndex(arr, low, mid - 1);
return findMinIndex(arr, mid + 1, high);
}
// function returns the index of largest element
// smaller than equal to 'x' in 'arr[l...h]'.
// If no such element exits in the given range,
// then it returns l-1.
static int binary_search(int arr[], int l, int h, int x)
{
while (l <= h) {
int mid = (l + h) / 2;
// if 'x' is less than or equal to arr[mid],
// then search in arr[mid+1...h]
if (arr[mid] <= x)
l = mid + 1;
// else search in arr[l...mid-1]
else
h = mid - 1;
}
// required index
return h;
}
// function to count elements less than
// or equal to a given value
static int countEleLessThanOrEqual(int arr[], int n, int x)
{
// index of the smallest element in the array
int min_index = findMinIndex(arr, 0, n - 1);
// if largest element smaller than or
// equal to 'x' lies in the sorted
// range arr[min_index...n-1]
if (x <= arr[n - 1])
return (binary_search(arr, min_index, n - 1, x) + 1 - min_index);
// if largest element smaller than or
// equal to 'x' lies in the sorted
// range arr[0...min_index-1]
if ((min_index - 1) >= 0 && x <= arr[min_index - 1])
return (n - min_index + binary_search(arr, 0, min_index - 1, x) + 1);
// else all the elements of the array
// are less than 'x'
return n;
}
// Driver code
public static void main(String[] args)
{
int arr[] = {6, 10, 12, 15, 2, 4, 5};
int n = arr.length;
int x = 14;
System.out.print("Count = " +
countEleLessThanOrEqual(arr, n, x));
}
}
// This code is contributed by Anant Agarwal.
Python3
# Python implementation to
# count elements less than or
# equal to a given value
# in a sorted rotated array
# function to find the
# minimum element's index
def findMinIndex(arr,low,high):
# This condition is needed
# to handle the case when
# array is not rotated at all
if (high < low):
return 0
# If there is only one element left
if (high == low):
return low
# Find mid
mid = (low + high) // 2
# Check if element (mid+1) is
# minimum element. Consider
# the cases like {3, 4, 5, 1, 2}
if (mid < high and arr[mid+1] < arr[mid]):
return (mid + 1)
# Check if mid itself
# is minimum element
if (mid > low and arr[mid] < arr[mid - 1]):
return mid
# Decide whether we need to
# go to left half or right half
if (arr[high] > arr[mid]):
return findMinIndex(arr, low, mid-1)
return findMinIndex(arr, mid+1, high)
# function returns the
# index of largest element
# smaller than equal to
# 'x' in 'arr[l...h]'.
# If no such element exits
# in the given range,
# then it returns l-1.
def binary_search(arr,l,h,x):
while (l <= h):
mid = (l+h) // 2
# if 'x' is less than
# or equal to arr[mid],
# then search in arr[mid+1...h]
if (arr[mid] <= x):
l = mid + 1
# else search in arr[l...mid-1]
else:
h = mid - 1
# required index
return h
# function to count
# elements less than
# or equal to a given value
def countEleLessThanOrEqual(arr,n,x):
# index of the smallest
# element in the array
min_index = findMinIndex(arr, 0, n-1)
# if largest element smaller
# than or equal to 'x' lies
# in the sorted range arr[min_index...n-1]
if (x <= arr[n-1]):
return (binary_search(arr, min_index, n-1, x) + 1 - min_index)
# if largest element smaller
# than or equal to 'x' lies
# in the sorted range arr[0...min_index-1]
if ((min_index - 1) >= 0 and x <= arr[min_index - 1]):
return (n - min_index + binary_search(arr, 0, min_index-1, x) + 1)
# else all the elements of the array
# are less than 'x'
return n
# driver code
arr = [6, 10, 12, 15, 2, 4, 5]
n = len(arr)
x = 14
print("Count = ",end="")
print(countEleLessThanOrEqual(arr, n, x))
# This code is contributed
# by Anant Agarwal.
C#
// C# implementation to count elements
// less than or equal to a given
// value in a sorted rotated array
using System;
public class GFG {
// function to find the minimum
// element's index
static int findMinIndex(int []arr, int low,
int high)
{
// This condition is needed to handle
// the case when array is not rotated
// at all
if (high < low)
return 0;
// If there is only one element left
if (high == low)
return low;
// Find mid
int mid = (low + high) / 2;
// Check if element (mid+1) is
// minimum element. Consider
// the cases like {3, 4, 5, 1, 2}
if (mid < high && arr[mid + 1] < arr[mid])
return (mid + 1);
// Check if mid itself is minimum element
if (mid > low && arr[mid] < arr[mid - 1])
return mid;
// Decide whether we need to go to
// left half or right half
if (arr[high] > arr[mid])
return findMinIndex(arr, low, mid - 1);
return findMinIndex(arr, mid + 1, high);
}
// function returns the index of largest element
// smaller than equal to 'x' in 'arr[l...h]'.
// If no such element exits in the given range,
// then it returns l-1.
static int binary_search(int []arr, int l,
int h, int x)
{
while (l <= h)
{
int mid = (l + h) / 2;
// if 'x' is less than or equal to
// arr[mid], then search in
// arr[mid+1...h]
if (arr[mid] <= x)
l = mid + 1;
// else search in arr[l...mid-1]
else
h = mid - 1;
}
// required index
return h;
}
// function to count elements less than
// or equal to a given value
static int countEleLessThanOrEqual(int []arr,
int n, int x)
{
// index of the smallest element in
// the array
int min_index = findMinIndex(arr, 0, n - 1);
// if largest element smaller than or
// equal to 'x' lies in the sorted
// range arr[min_index...n-1]
if (x <= arr[n - 1])
return (binary_search(arr, min_index,
n - 1, x) + 1 - min_index);
// if largest element smaller than or
// equal to 'x' lies in the sorted
// range arr[0...min_index-1]
if ((min_index - 1) >= 0 &&
x <= arr[min_index - 1])
return (n - min_index + binary_search(arr,
0, min_index - 1, x) + 1);
// else all the elements of the array
// are less than 'x'
return n;
}
// Driver code
public static void Main()
{
int []arr = {6, 10, 12, 15, 2, 4, 5};
int n = arr.Length;
int x = 14;
Console.Write("Count = " +
countEleLessThanOrEqual(arr, n, x));
}
}
// This code is contributed by Sam007.
PHP
$low &&
$arr[$mid] < $arr[$mid - 1])
return $mid;
// Decide whether we need to go
// to left half or right half
if ($arr[$high] > $arr[$mid])
return findMinIndex($arr, $low, $mid - 1);
return findMinIndex($arr, $mid + 1, $high);
}
// function returns the index of largest
// element smaller than equal to 'x' in
// 'arr[l...h]'. If no such element exits
// in the given range, then it returns l-1.
function binary_search(&$arr, $l, $h, $x)
{
while ($l <= $h)
{
$mid = intval(($l + $h) / 2);
// if 'x' is less than or equal
// to arr[mid], then search in
// arr[mid+1...h]
if ($arr[$mid] <= $x)
$l = $mid + 1;
// else search in arr[l...mid-1]
else
$h = $mid - 1;
}
// required index
return $h;
}
// function to count elements less
// than or equal to a given value
function countEleLessThanOrEqual(&$arr, $n, $x)
{
// index of the smallest element
// in the array
$min_index = findMinIndex($arr, 0, $n - 1);
// if largest element smaller than
// or equal to 'x' lies in the sorted
// range arr[min_index...n-1]
if ($x <= $arr[$n - 1])
return (binary_search($arr, $min_index,
$n - 1, $x) + 1 - $min_index);
// if largest element smaller than or
// equal to 'x' lies in the sorted
// range arr[0...min_index-1]
if (($min_index - 1) >= 0 &&
$x <= $arr[$min_index - 1])
return ($n - $min_index +
binary_search($arr, 0,
$min_index - 1, $x) + 1);
// else all the elements of
// the array are less than 'x'
return $n;
}
// Driver Code
$arr = array(6, 10, 12, 15, 2, 4, 5);
$n = sizeof($arr);
$x = 14;
echo "Count = " . countEleLessThanOrEqual($arr, $n, $x);
// This code is contributed
// by ChitraNayal
?>
Javascript
输出:
Count = 6