Skip to content
Related Articles

Related Articles

Check if ceil of number divided by power of two exist in sorted array

Improve Article
Save Article
  • Last Updated : 14 Jun, 2022
Improve Article
Save Article

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:
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




// C++14 implementation to check
// if a number divided by power
// of two exist in the sorted array
#include <bits/stdc++.h>
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




<script>
// javascript 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
    function findNumberDivByPowerofTwo(ar , k , n) {
        var found = -1, m = k;
 
        // Loop to check if there exist
        // a number by divided by power of 2
        while (m > 0) {
            var l = 0;
            var r = n - 1;
 
            // Binary Search
            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;
                }
            }
 
            // 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 = parseInt(m / 2);
        }
 
        return found;
    }
 
    // Driver Code
     
        var arr = [ 3, 5, 7, 8, 10 ];
        var k = 4, n = 5;
 
        document.write(findNumberDivByPowerofTwo(arr, k, n));
 
// This code contributed by gauravrajput1
</script>

Output: 

-1

 

Time Complexity: O(logn), required to do binary search operations
Auxiliary Space: O(1), as no extra space is used


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!