给定一个已排序的数组和一个值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)。
不需要额外的空间,因此空间复杂度是恒定的
高效方法
方法:问题在于,给定的数组已排序。这个想法是使用Binary Search将二进制数x的底数与中间元素进行比较,然后将搜索空间划分为一半,从而找到排序数组中的数字x的底数。
算法:
- 该算法可以递归实现,也可以通过迭代实现,但基本思想保持不变。
- 有一些基本案例要处理。
- 如果没有大于x的数字,则打印最后一个元素
- 如果第一个数字大于x,则打印-1
- 创建三个变量low = 0 ,mid and 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
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.
Java脚本
输出:
Floor of 7 is 6.
复杂度分析:
- 时间复杂度: O(log n)。
要运行二进制搜索,所需的时间复杂度为O(log n)。 - 空间复杂度: O(1)。
由于不需要额外的空间,因此空间复杂度是恒定的。