给定一个已排序的数组arr[]和一个整数K ,任务是检查数组中是否存在数字K除以某个 2 的幂的单元格。
注意:如果没有这样的元素打印-1。
例子:
Input: arr[] = {3, 5, 7, 8, 10}, K = 4
Output: -1
Explanation:
There is no such element.
Input: arr[] = {1, 2, 3, 5, 7, 8}, K = 4
Output: 2
Explanation:
When 4 is divided by 2 to the power 1, there exist an element in the array.
方法:这个想法是尝试 2 的每个幂,并检查是否存在从幂为 0 开始的那个数的 ceil。检查元素是否存在于数组中可以使用二分搜索来完成。
下面是上述方法的实现:
C++14
// C++14 implementation to check
// if a number divided by power
// of two exist in the sorted array
#include
using namespace std;
// Function to find there exist a
// number or not in the array
int findNumberDivByPowerofTwo(int ar[],
int k, int n)
{
int found = -1, m = k;
// Loop to check if there exist
// a number by divided by power of 2
while (m > 0)
{
int l = 0;
int r = n - 1;
// Binary Search
while (l <= r)
{
int mid = (l + r) / 2;
if (ar[mid] == m)
{
found = m;
break;
}
else if (ar[mid] > m)
{
r = mid - 1;
}
else if (ar[mid] < m)
{
l = mid + 1;
}
}
// Condition to check the number
// is found in the array or not
if (found != -1)
{
break;
}
// Otherwise divide the number
// by increasing the one more
// power of 2
m = m / 2;
}
return found;
}
// Driver Code
int main()
{
int arr[] = { 3, 5, 7, 8, 10 };
int k = 4, n = 5;
cout << findNumberDivByPowerofTwo(arr, k, n);
}
// This code is contributed by code_hunt
Java
// Java implementation to check
// if a number divided by power
// of two exist in the sorted array
import java.util.Scanner;
public class GreeksForGreeksQuestions {
// Function to find there exist a
// number or not in the array
static int findNumberDivByPowerofTwo(
int[] ar, int k, int n)
{
int found = -1, m = k;
// Loop to check if there exist
// a number by divided by power of 2
while (m > 0) {
int l = 0;
int r = n - 1;
// Binary Search
while (l <= r) {
int mid = (l + r) / 2;
if (ar[mid] == m) {
found = m;
break;
}
else if (ar[mid] > m) {
r = mid - 1;
}
else if (ar[mid] < m) {
l = mid + 1;
}
}
// Condition to check the number
// is found in the array or not
if (found != -1) {
break;
}
// Otherwise divide the number
// by increasing the one more
// power of 2
m = m / 2;
}
return found;
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 3, 5, 7, 8, 10 };
int k = 4, n = 5;
System.out.println(
findNumberDivByPowerofTwo(
arr, k, n));
}
}
Python3
# Python3 implementation to check
# if a number divided by power
# of two exist in the sorted array
# Function to find there exist a
# number or not in the array
def findNumberDivByPowerofTwo(ar, k, n):
found = -1
m = k
# Loop to check if there exist
# a number by divided by power of 2
while (m > 0):
l = 0
r = n - 1
# Binary Search
while (l <= r):
mid = (l + r) // 2
if (ar[mid] == m):
found = m
break
elif (ar[mid] > m):
r = mid - 1
elif (ar[mid] < m):
l = mid + 1
# Condition to check the number
# is found in the array or not
if (found != -1):
break
# Otherwise divide the number
# by increasing the one more
# power of 2
m = m // 2
return found
# Driver Code
arr = [ 3, 5, 7, 8, 10 ]
k = 4
n = 5
print(findNumberDivByPowerofTwo(arr, k, n))
# This code is contributed by code_hunt
C#
// C# implementation to check
// if a number divided by power
// of two exist in the sorted array
using System;
class GFG{
// Function to find there exist a
// number or not in the array
static int findNumberDivByPowerofTwo(int[] ar, int k,
int n)
{
int found = -1, m = k;
// Loop to check if there exist
// a number by divided by power of 2
while (m > 0)
{
int l = 0;
int r = n - 1;
// Binary Search
while (l <= r)
{
int mid = (l + r) / 2;
if (ar[mid] == m)
{
found = m;
break;
}
else if (ar[mid] > m)
{
r = mid - 1;
}
else if (ar[mid] < m)
{
l = mid + 1;
}
}
// Condition to check the number
// is found in the array or not
if (found != -1)
{
break;
}
// Otherwise divide the number
// by increasing the one more
// power of 2
m = m / 2;
}
return found;
}
// Driver Code
public static void Main(String[] args)
{
int []arr = { 3, 5, 7, 8, 10 };
int k = 4, n = 5;
Console.WriteLine(findNumberDivByPowerofTwo(
arr, k, n));
}
}
// This code is contributed by princi singh
Javascript
输出:
-1
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live