给定一个已排序的数组和一个值 x,x 的下限是数组中小于或等于 x 的最大元素。编写有效的函数来找到 x 的下限。
例子:
Input : arr[] = {1, 2, 8, 10, 10, 12, 19}, x = 5
Output : 2
2 is the largest element in
arr[] smaller than 5.
Input : arr[] = {1, 2, 8, 10, 10, 12, 19}, x = 20
Output : 19
19 is the largest element in
arr[] smaller than 20.
Input : arr[] = {1, 2, 8, 10, 10, 12, 19}, x = 0
Output : -1
Since floor doesn't exist,
output is -1.
简单方法
做法:思路很简单,遍历数组,找到第一个大于x的元素。刚找到元素之前的元素是 x 的下限。
算法:
- 从头到尾遍历数组。
- 如果当前元素大于 x 打印前一个数字并中断循环。
- 如果没有大于 x 的数字,则打印最后一个元素
- 如果第一个数字大于 x 则打印 -1
C++
// C/C++ program to find floor of a given number
// in a sorted array
#include
/* An inefficient function to get
index of floor of x in arr[0..n-1] */
int floorSearch(int arr[], int n, int x)
{
// If last element is smaller than x
if (x >= arr[n - 1])
return n - 1;
// If first element is greater than x
if (x < arr[0])
return -1;
// Linearly search for the first element
// greater than x
for (int i = 1; i < n; i++)
if (arr[i] > x)
return (i - 1);
return -1;
}
/* Driver program to check above functions */
int main()
{
int arr[] = { 1, 2, 4, 6, 10, 12, 14 };
int n = sizeof(arr) / sizeof(arr[0]);
int x = 7;
int index = floorSearch(arr, n - 1, x);
if (index == -1)
printf("Floor of %d doesn't exist in array ", x);
else
printf("Floor of %d is %d", x, arr[index]);
return 0;
}
Java
// Java program to find floor of
// a given number in a sorted array
import java.io.*;
import java.util.*;
import java.lang.*;
class GFG {
/* An inefficient function to get index of floor
of x in arr[0..n-1] */
static int floorSearch(
int arr[], int n, int x)
{
// If last element is smaller than x
if (x >= arr[n - 1])
return n - 1;
// If first element is greater than x
if (x < arr[0])
return -1;
// Linearly search for the first element
// greater than x
for (int i = 1; i < n; i++)
if (arr[i] > x)
return (i - 1);
return -1;
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 1, 2, 4, 6, 10, 12, 14 };
int n = arr.length;
int x = 7;
int index = floorSearch(arr, n - 1, x);
if (index == -1)
System.out.print(
"Floor of " + x
+ " doesn't exist in array ");
else
System.out.print(
"Floor of " + x + " is "
+ arr[index]);
}
}
// This code is contributed
// by Akanksha Rai(Abby_akku)
Python3
# Python3 program to find floor of a
# given number in a sorted array
# Function to get index of floor
# of x in arr[low..high]
def floorSearch(arr, low, high, x):
# If low and high cross each other
if (low > high):
return -1
# If last element is smaller than x
if (x >= arr[high]):
return high
# Find the middle point
mid = int((low + high) / 2)
# If middle point is floor.
if (arr[mid] == x):
return mid
# If x lies between mid-1 and mid
if (mid > 0 and arr[mid-1] <= x
and x < arr[mid]):
return mid - 1
# If x is smaller than mid,
# floor must be in left half.
if (x < arr[mid]):
return floorSearch(arr, low, mid-1, x)
# If mid-1 is not floor and x is greater than
# arr[mid],
return floorSearch(arr, mid + 1, high, x)
# Driver Code
arr = [1, 2, 4, 6, 10, 12, 14]
n = len(arr)
x = 7
index = floorSearch(arr, 0, n-1, x)
if (index == -1):
print("Floor of", x, "doesn't exist \
in array ", end = "")
else:
print("Floor of", x, "is", arr[index])
# This code is contributed by Smitha Dinesh Semwal.
C#
// C# program to find floor of a given number
// in a sorted array
using System;
class GFG {
/* An inefficient function to get index of floor
of x in arr[0..n-1] */
static int floorSearch(int[] arr, int n, int x)
{
// If last element is smaller than x
if (x >= arr[n - 1])
return n - 1;
// If first element is greater than x
if (x < arr[0])
return -1;
// Linearly search for the first element
// greater than x
for (int i = 1; i < n; i++)
if (arr[i] > x)
return (i - 1);
return -1;
}
// Driver Code
static void Main()
{
int[] arr = { 1, 2, 4, 6, 10, 12, 14 };
int n = arr.Length;
int x = 7;
int index = floorSearch(arr, n - 1, x);
if (index == -1)
Console.WriteLine("Floor of " + x + " doesn't exist in array ");
else
Console.WriteLine("Floor of " + x + " is " + arr[index]);
}
}
// This code is contributed
// by mits
PHP
= $arr[$n - 1])
return $n - 1;
// If first element is greater
// than x
if ($x < $arr[0])
return -1;
// Linearly search for the
// first element greater than x
for ($i = 1; $i < $n; $i++)
if ($arr[$i] > $x)
return ($i - 1);
return -1;
}
// Driver Code
$arr = array (1, 2, 4, 6, 10, 12, 14);
$n = sizeof($arr);
$x = 7;
$index = floorSearch($arr, $n - 1, $x);
if ($index == -1)
echo "Floor of ", $x,
"doesn't exist in array ";
else
echo "Floor of ", $x,
" is ", $arr[$index];
// This code is contributed by ajit
?>
Javascript
C++
// A C/C++ program to find floor
// of a given number in a sorted array
#include
/* Function to get index of floor of x in
arr[low..high] */
int floorSearch(int arr[], int low,
int high, int x)
{
// If low and high cross each other
if (low > high)
return -1;
// If last element is smaller than x
if (x >= arr[high])
return high;
// Find the middle point
int mid = (low + high) / 2;
// If middle point is floor.
if (arr[mid] == x)
return mid;
// If x lies between mid-1 and mid
if (mid > 0 && arr[mid - 1] <= x
&& x < arr[mid])
return mid - 1;
// If x is smaller than mid, floor
// must be in left half.
if (x < arr[mid])
return floorSearch(
arr, low, mid - 1, x);
// If mid-1 is not floor and x is
// greater than arr[mid],
return floorSearch(arr, mid + 1, high, x);
}
/* Driver program to check above functions */
int main()
{
int arr[] = { 1, 2, 4, 6, 10, 12, 14 };
int n = sizeof(arr) / sizeof(arr[0]);
int x = 7;
int index = floorSearch(arr, 0, n - 1, x);
if (index == -1)
printf(
"Floor of %d doesn't exist in array ", x);
else
printf(
"Floor of %d is %d", x, arr[index]);
return 0;
}
Java
// Java program to find floor of
// a given number in a sorted array
import java.io.*;
class GFG {
/* Function to get index of floor of x in
arr[low..high] */
static int floorSearch(
int arr[], int low,
int high, int x)
{
// If low and high cross each other
if (low > high)
return -1;
// If last element is smaller than x
if (x >= arr[high])
return high;
// Find the middle point
int mid = (low + high) / 2;
// If middle point is floor.
if (arr[mid] == x)
return mid;
// If x lies between mid-1 and mid
if (
mid > 0 && arr[mid - 1] <= x && x < arr[mid])
return mid - 1;
// If x is smaller than mid, floor
// must be in left half.
if (x < arr[mid])
return floorSearch(
arr, low,
mid - 1, x);
// If mid-1 is not floor and x is
// greater than arr[mid],
return floorSearch(
arr, mid + 1, high,
x);
}
/* Driver program to check above functions */
public static void main(String[] args)
{
int arr[] = { 1, 2, 4, 6, 10, 12, 14 };
int n = arr.length;
int x = 7;
int index = floorSearch(
arr, 0, n - 1,
x);
if (index == -1)
System.out.println(
"Floor of " + x + " dosen't exist in array ");
else
System.out.println(
"Floor of " + x + " is " + arr[index]);
}
}
// This code is contributed by Prerna Saini
Python3
# Python3 program to find floor of a
# given number in a sorted array
# Function to get index of floor
# of x in arr[low..high]
def floorSearch(arr, low, high, x):
# If low and high cross each other
if (low > high):
return -1
# If last element is smaller than x
if (x >= arr[high]):
return high
# Find the middle point
mid = int((low + high) / 2)
# If middle point is floor.
if (arr[mid] == x):
return mid
# If x lies between mid-1 and mid
if (mid > 0 and arr[mid-1] <= x
and x < arr[mid]):
return mid - 1
# If x is smaller than mid,
# floor must be in left half.
if (x < arr[mid]):
return floorSearch(arr, low, mid-1, x)
# If mid-1 is not floor and x is greater than
# arr[mid],
return floorSearch(arr, mid + 1, high, x)
# Driver Code
arr = [1, 2, 4, 6, 10, 12, 14]
n = len(arr)
x = 7
index = floorSearch(arr, 0, n-1, x)
if (index == -1):
print("Floor of", x, "doesn't exist\
in array ", end = "")
else:
print("Floor of", x, "is", arr[index])
# This code is contributed by Smitha Dinesh Semwal.
C#
// C# program to find floor of
// a given number in a sorted array
using System;
class GFG {
/* Function to get index of floor of x in
arr[low..high] */
static int floorSearch(
int[] arr, int low,
int high, int x)
{
// If low and high cross each other
if (low > high)
return -1;
// If last element is smaller than x
if (x >= arr[high])
return high;
// Find the middle point
int mid = (low + high) / 2;
// If middle point is floor.
if (arr[mid] == x)
return mid;
// If x lies between mid-1 and mid
if (mid > 0 && arr[mid - 1] <= x && x < arr[mid])
return mid - 1;
// If x is smaller than mid, floor
// must be in left half.
if (x < arr[mid])
return floorSearch(arr, low,
mid - 1, x);
// If mid-1 is not floor and x is
// greater than arr[mid],
return floorSearch(arr, mid + 1, high,
x);
}
/* Driver program to check above functions */
public static void Main()
{
int[] arr = { 1, 2, 4, 6, 10, 12, 14 };
int n = arr.Length;
int x = 7;
int index = floorSearch(arr, 0, n - 1,
x);
if (index == -1)
Console.Write("Floor of " + x + " dosen't exist in array ");
else
Console.Write("Floor of " + x + " is " + arr[index]);
}
}
// This code is contributed by nitin mittal.
Javascript
输出:
Floor of 7 is 6.
复杂度分析:
- 时间复杂度: O(n)。
遍历一个数组只需要一个循环,所以时间复杂度是 O(n)。 - 空间复杂度: O(1)。
不需要额外的空间,所以空间复杂度是恒定的
高效的方法
方法:问题中有一个问题,给定的数组已排序。这个想法是使用二分搜索通过将数字x与中间元素进行比较并将搜索空间分成两半来找到排序数组中数字x的下限。
算法:
- 该算法可以通过递归或迭代实现,但基本思想保持不变。
- 有基本情况需要处理。
- 如果没有大于 x 的数字,则打印最后一个元素
- 如果第一个数字大于 x 则打印 -1
- 创建三个变量low = 0 , mid 和high = n-1和另一个变量来存储答案
- 运行循环或递归直到并且除非低小于或等于高。
- 检查中间 ( (low + high) /2 ) 元素是否小于 x,如果是,则更新低,即low = mid + 1 ,并使用中间元素更新答案。在这一步中,我们将搜索空间减少到一半。
- 否则更新低,即高 = 中 – 1
- 打印答案。
C++
// A C/C++ program to find floor
// of a given number in a sorted array
#include
/* Function to get index of floor of x in
arr[low..high] */
int floorSearch(int arr[], int low,
int high, int x)
{
// If low and high cross each other
if (low > high)
return -1;
// If last element is smaller than x
if (x >= arr[high])
return high;
// Find the middle point
int mid = (low + high) / 2;
// If middle point is floor.
if (arr[mid] == x)
return mid;
// If x lies between mid-1 and mid
if (mid > 0 && arr[mid - 1] <= x
&& x < arr[mid])
return mid - 1;
// If x is smaller than mid, floor
// must be in left half.
if (x < arr[mid])
return floorSearch(
arr, low, mid - 1, x);
// If mid-1 is not floor and x is
// greater than arr[mid],
return floorSearch(arr, mid + 1, high, x);
}
/* Driver program to check above functions */
int main()
{
int arr[] = { 1, 2, 4, 6, 10, 12, 14 };
int n = sizeof(arr) / sizeof(arr[0]);
int x = 7;
int index = floorSearch(arr, 0, n - 1, x);
if (index == -1)
printf(
"Floor of %d doesn't exist in array ", x);
else
printf(
"Floor of %d is %d", x, arr[index]);
return 0;
}
Java
// Java program to find floor of
// a given number in a sorted array
import java.io.*;
class GFG {
/* Function to get index of floor of x in
arr[low..high] */
static int floorSearch(
int arr[], int low,
int high, int x)
{
// If low and high cross each other
if (low > high)
return -1;
// If last element is smaller than x
if (x >= arr[high])
return high;
// Find the middle point
int mid = (low + high) / 2;
// If middle point is floor.
if (arr[mid] == x)
return mid;
// If x lies between mid-1 and mid
if (
mid > 0 && arr[mid - 1] <= x && x < arr[mid])
return mid - 1;
// If x is smaller than mid, floor
// must be in left half.
if (x < arr[mid])
return floorSearch(
arr, low,
mid - 1, x);
// If mid-1 is not floor and x is
// greater than arr[mid],
return floorSearch(
arr, mid + 1, high,
x);
}
/* Driver program to check above functions */
public static void main(String[] args)
{
int arr[] = { 1, 2, 4, 6, 10, 12, 14 };
int n = arr.length;
int x = 7;
int index = floorSearch(
arr, 0, n - 1,
x);
if (index == -1)
System.out.println(
"Floor of " + x + " dosen't exist in array ");
else
System.out.println(
"Floor of " + x + " is " + arr[index]);
}
}
// This code is contributed by Prerna Saini
蟒蛇3
# Python3 program to find floor of a
# given number in a sorted array
# Function to get index of floor
# of x in arr[low..high]
def floorSearch(arr, low, high, x):
# If low and high cross each other
if (low > high):
return -1
# If last element is smaller than x
if (x >= arr[high]):
return high
# Find the middle point
mid = int((low + high) / 2)
# If middle point is floor.
if (arr[mid] == x):
return mid
# If x lies between mid-1 and mid
if (mid > 0 and arr[mid-1] <= x
and x < arr[mid]):
return mid - 1
# If x is smaller than mid,
# floor must be in left half.
if (x < arr[mid]):
return floorSearch(arr, low, mid-1, x)
# If mid-1 is not floor and x is greater than
# arr[mid],
return floorSearch(arr, mid + 1, high, x)
# Driver Code
arr = [1, 2, 4, 6, 10, 12, 14]
n = len(arr)
x = 7
index = floorSearch(arr, 0, n-1, x)
if (index == -1):
print("Floor of", x, "doesn't exist\
in array ", end = "")
else:
print("Floor of", x, "is", arr[index])
# This code is contributed by Smitha Dinesh Semwal.
C#
// C# program to find floor of
// a given number in a sorted array
using System;
class GFG {
/* Function to get index of floor of x in
arr[low..high] */
static int floorSearch(
int[] arr, int low,
int high, int x)
{
// If low and high cross each other
if (low > high)
return -1;
// If last element is smaller than x
if (x >= arr[high])
return high;
// Find the middle point
int mid = (low + high) / 2;
// If middle point is floor.
if (arr[mid] == x)
return mid;
// If x lies between mid-1 and mid
if (mid > 0 && arr[mid - 1] <= x && x < arr[mid])
return mid - 1;
// If x is smaller than mid, floor
// must be in left half.
if (x < arr[mid])
return floorSearch(arr, low,
mid - 1, x);
// If mid-1 is not floor and x is
// greater than arr[mid],
return floorSearch(arr, mid + 1, high,
x);
}
/* Driver program to check above functions */
public static void Main()
{
int[] arr = { 1, 2, 4, 6, 10, 12, 14 };
int n = arr.Length;
int x = 7;
int index = floorSearch(arr, 0, n - 1,
x);
if (index == -1)
Console.Write("Floor of " + x + " dosen't exist in array ");
else
Console.Write("Floor of " + x + " is " + arr[index]);
}
}
// This code is contributed by nitin mittal.
Javascript
输出:
Floor of 7 is 6.
复杂度分析:
- 时间复杂度: O(log n)。
要运行二进制搜索,所需的时间复杂度为 O(log n)。 - 空间复杂度: O(1)。
由于不需要额外的空间,所以空间复杂度是恒定的。
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。