在未排序的数组中查找 floor 和 ceil
给定一个未排序的数组 arr[] 和一个元素 x,在 arr[0..n-1] 中找到 x 的下限和上限。
x的下限是小于或等于 x 的最大元素。如果 x 小于 arr[] 的最小元素,则 x 的下限不存在。
x的ceil是大于或等于x的最小元素。如果 x 大于 arr[] 的greates 元素,则x 的ceil 不存在。
例子:
Input : arr[] = {5, 6, 8, 9, 6, 5, 5, 6}
x = 7
Output : Floor = 6
Ceiling = 8
Input : arr[] = {5, 6, 8, 9, 6, 5, 5, 6}
x = 6
Output : Floor = 6
Ceiling = 6
Input : arr[] = {5, 6, 8, 9, 6, 5, 5, 6}
x = 10
Output : Floor = 9
Ceiling doesn't exist.
方法一(使用排序)
1) 对输入数组进行排序。
2) 使用二分搜索找到 x 的下限和上限。请参阅此和此以在排序数组中实现地板和天花板。
时间复杂度:O(n log n)
辅助空间:O(1)
如果静态数组上有多个地板和天花板查询,此解决方案效果很好。我们可以对数组进行一次排序并在 O(Log n) 时间内回答查询。
方法 2(使用线性搜索
这个想法是遍历数组并跟踪相对于 x 的两个距离。
1) 大于或等于 x 的元素的最小距离。
2)小于或等于x的元素的最小距离。
最后以最小距离打印元素。
C++
// C++ program to find floor and ceiling in an
// unsorted array.
#include
using namespace std;
// Function to floor and ceiling of x in arr[]
void floorAndCeil(int arr[], int n, int x)
{
// Indexes of floor and ceiling
int fInd, cInd;
// Distances of current floor and ceiling
int fDist = INT_MAX, cDist = INT_MAX;
for (int i=0; i= x && cDist > (arr[i] - x))
{
cInd = i;
cDist = arr[i] - x;
}
// If current element is closer than
// previous floor.
if (arr[i] <= x && fDist > (x - arr[i]))
{
fInd = i;
fDist = x - arr[i];
}
}
if (fDist == INT_MAX)
cout << "Floor doesn't exist " << endl;
else
cout << "Floor is " << arr[fInd] << endl;
if (cDist == INT_MAX)
cout << "Ceil doesn't exist " << endl;
else
cout << "Ceil is " << arr[cInd] << endl;
}
// Driver code
int main()
{
int arr[] = {5, 6, 8, 9, 6, 5, 5, 6};
int n = sizeof(arr)/sizeof(int);
int x = 7;
floorAndCeil(arr, n, x);
return 0;
}
Java
// Java program to find floor and ceiling in an
// unsorted array.
import java.io.*;
class GFG
{
// Function to floor and ceiling of x in arr[]
public static void floorAndCeil(int arr[], int x)
{
int n = arr.length;
// Indexes of floor and ceiling
int fInd = -1, cInd = -1;
// Distances of current floor and ceiling
int fDist = Integer.MAX_VALUE, cDist = Integer.MAX_VALUE;
for (int i = 0; i < n; i++)
{
// If current element is closer than
// previous ceiling.
if (arr[i] >= x && cDist > (arr[i] - x))
{
cInd = i;
cDist = arr[i] - x;
}
// If current element is closer than
// previous floor.
if (arr[i] <= x && fDist > (x - arr[i]))
{
fInd = i;
fDist = x - arr[i];
}
}
if(fDist == Integer.MAX_VALUE)
System.out.println("Floor doesn't exist " );
else
System.out.println("Floor is " + arr[fInd]);
if(cDist == Integer.MAX_VALUE)
System.out.println("Ceil doesn't exist ");
else
System.out.println("Ceil is " + arr[cInd]);
}
public static void main (String[] args)
{
int arr[] = {5, 6, 8, 9, 6, 5, 5, 6};
int x = 7;
floorAndCeil(arr, x);
}
}
Python 3
# Python 3 program to find
# floor and ceiling in an
# unsorted array.
import sys
# Function to floor and
# ceiling of x in arr[]
def floorAndCeil(arr, n, x):
# Distances of current
# floor and ceiling
fDist = sys.maxsize
cDist = sys.maxsize
for i in range(n):
# If current element is closer
# than previous ceiling.
if (arr[i] >= x and
cDist > (arr[i] - x)):
cInd = i
cDist = arr[i] - x
# If current element is closer
# than previous floor.
if (arr[i] <= x and fDist > (x - arr[i])):
fInd = i
fDist = x - arr[i]
if (fDist == sys.maxsize):
print("Floor doesn't exist ")
else:
print("Floor is " + str(arr[fInd]))
if (cDist == sys.maxsize):
print( "Ceil doesn't exist ")
else:
print("Ceil is " + str(arr[cInd]))
# Driver code
if __name__ == "__main__":
arr = [5, 6, 8, 9, 6, 5, 5, 6]
n = len(arr)
x = 7
floorAndCeil(arr, n, x)
# This code is contributed
# by ChitraNayal
C#
// C# program to find floor and ceiling in an
// unsorted array.
using System;
class GFG {
// Function to floor and ceiling of x in arr[]
public static void floorAndCeil(int []arr, int x)
{
int n = arr.Length;
// Indexes of floor and ceiling
int fInd = -1, cInd = -1;
// Distances of current floor and ceiling
int fDist = int.MaxValue,
cDist =int.MaxValue;
for (int i = 0; i < n; i++)
{
// If current element is closer than
// previous ceiling.
if (arr[i] >= x && cDist > (arr[i] - x))
{
cInd = i;
cDist = arr[i] - x;
}
// If current element is closer than
// previous floor.
if (arr[i] <= x && fDist > (x - arr[i]))
{
fInd = i;
fDist = x - arr[i];
}
}
if(fDist == int.MaxValue)
Console.Write("Floor doesn't exist " );
else
Console.WriteLine("Floor is " + arr[fInd]);
if(cDist == int.MaxValue)
Console.Write("Ceil doesn't exist ");
else
Console.Write("Ceil is " + arr[cInd]);
}
// Driver code
public static void Main ()
{
int []arr = {5, 6, 8, 9, 6, 5, 5, 6};
int x = 7;
floorAndCeil(arr, x);
}
}
// This code is contributed by nitin mittal.
PHP
= $x &&
$cDist > ($arr[$i] - $x))
{
$cInd = $i;
$cDist = $arr[$i] - $x;
}
// If current element
// is closer than
// previous floor.
if ($arr[$i] <= $x &&
$fDist > ($x - $arr[$i]))
{
$fInd = $i;
$fDist = $x - $arr[$i];
}
}
if ($fDist == 999999)
echo "Floor doesn't ".
"exist " . "\n" ;
else
echo "Floor is " .
$arr[$fInd] . "\n";
if ($cDist == 999999)
echo "Ceil doesn't " .
"exist " . "\n";
else
echo "Ceil is " .
$arr[$cInd] . "\n";
}
// Driver code
$arr = array(5, 6, 8, 9,
6, 5, 5, 6);
$n = count($arr);
$x = 7;
floorAndCeil($arr, $n, $x);
// This code is contributed
// by Sam007
?>
Javascript
输出 :
Floor is 6
Ceil is 8