Check if ceil of number divided by power of two exist in sorted array
Last Updated :
14 Jun, 2022
Given a sorted array arr[] and an integer K, the task is to check if there exists a ceil of number K divided by some power of 2 in the array.
Note: If there is no such element print -1.
Examples:
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.
Approach: The idea is to try for every power of 2 and check that there exists a ceil of that number starting from power as 0. Checking an element exists in the array or not can be done using Binary Search.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
int findNumberDivByPowerofTwo( int ar[],
int k, int n)
{
int found = -1, m = k;
while (m > 0)
{
int l = 0;
int r = n - 1;
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;
}
}
if (found != -1)
{
break ;
}
m = m / 2;
}
return found;
}
int main()
{
int arr[] = { 3, 5, 7, 8, 10 };
int k = 4, n = 5;
cout << findNumberDivByPowerofTwo(arr, k, n);
}
|
Java
import java.util.Scanner;
public class GreeksForGreeksQuestions {
static int findNumberDivByPowerofTwo(
int [] ar, int k, int n)
{
int found = - 1 , m = k;
while (m > 0 ) {
int l = 0 ;
int r = n - 1 ;
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 ;
}
}
if (found != - 1 ) {
break ;
}
m = m / 2 ;
}
return found;
}
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
def findNumberDivByPowerofTwo(ar, k, n):
found = - 1
m = k
while (m > 0 ):
l = 0
r = n - 1
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
if (found ! = - 1 ):
break
m = m / / 2
return found
arr = [ 3 , 5 , 7 , 8 , 10 ]
k = 4
n = 5
print (findNumberDivByPowerofTwo(arr, k, n))
|
C#
using System;
class GFG{
static int findNumberDivByPowerofTwo( int [] ar, int k,
int n)
{
int found = -1, m = k;
while (m > 0)
{
int l = 0;
int r = n - 1;
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;
}
}
if (found != -1)
{
break ;
}
m = m / 2;
}
return found;
}
public static void Main(String[] args)
{
int []arr = { 3, 5, 7, 8, 10 };
int k = 4, n = 5;
Console.WriteLine(findNumberDivByPowerofTwo(
arr, k, n));
}
}
|
Javascript
<script>
function findNumberDivByPowerofTwo(ar , k , n) {
var found = -1, m = k;
while (m > 0) {
var l = 0;
var r = n - 1;
while (l <= r) {
var mid = parseInt((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;
}
}
if (found != -1) {
break ;
}
m = parseInt(m / 2);
}
return found;
}
var arr = [ 3, 5, 7, 8, 10 ];
var k = 4, n = 5;
document.write(findNumberDivByPowerofTwo(arr, k, n));
</script>
|
Time Complexity: O(logn), required to do binary search operations
Auxiliary Space: O(1), as no extra space is used
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...