Open In App

Find X in range [1, N] of bit size A[i] such that X^2’s bit size is not present in Array

Last Updated : 31 Mar, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given an array A of N integers, where:

  • Each element of array represents the bit size of an imaginary unsigned integer datatype.
  • If ith imaginary datatype has a size of A[i] bits, then it can store integers from size 0 to 2A[i]-1.

The task is to check if there exist some integer X, such that:

  • X is in range 1 to N
  • X can fit in a datatype of bit size A[i]
  • X2 does not fit in any other distinct datatype of bit size A[j] (A[i] < A[j]), not even with leading zeroes.

Examples:

Input: N = 4 , A = {4, 2, 3, 1}
Output: YES
Explanation: Let X = 7. Now, 7 = (111)2.  
Clearly, 7 fits in 3 bits (3 is present in given array). 
72 = 49 , which is equal to (110001)2 , which clearly doesn’t fits in 4 bits .

Input: N = 3 , A = {16, 64, 32}
Output: NO

 

Approach: The idea is to check if there is a pair (a, b) in the array, such that for the pair, there exist an integer X that fits in a bits and X*X does not fits in b bits.

Observations:

The best candidate for X is the largest possible number that fits in a bits. (i.e. 2a-1). The reason being that it increases the possibility of existence of such b, such that X*X does not fit in b bits.

If an integer X = 2a – 1 , then X * X = (2a – 1) * (2a – 1) , which would require 2*a  bits to be stored. 

So, for each element A[i], it would be sufficient to check if smallest element greater than it is less than 2 * A[i] or not. 

Based on above observation, following approach can be used to solve the problem:

  • Sort the given array.
  • Iterate through the array and check if any element is less than twice of its previous element.
  • If so, return true.
  • If iteration completes without returning true, then return false.

Following is the code based on above approach:

C++




// C++ code for above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if there exist
// an integer X, such that X fits in bits
// represented by some array element and
// X*X does not fit in bits represented
// by some other element
bool check(int N, int A[])
{
 
    // sorting the given array
    sort(A, A + N);
 
    // iterating through the array
    for (int i = 1; i < N; i++) {
 
        // If an element is equal to the
        // last element simply skip that
        // case and continue the iteration
        // as the task is to find two
        // distinct integers such that
        // X fits in one them and
        // X*X does not fit in other
        if (A[i] == A[i - 1]) {
            continue;
        }
 
        // If the value of an element is
        // less than twice of its
        // previous element, that means
        // X would fit in previous element bits,
        // but X*X would not fit in current
        // element bits
        if (A[i] < 2 * A[i - 1]) {
            return true;
        }
    }
 
    // return false if iterations completes
    return false;
}
 
// Driver Code
int main()
{
    int N = 4;
    int A[] = { 4, 2, 3, 1 };
    bool answer = check(N, A);
    if (answer == true) {
        cout << "YES";
    }
    else {
        cout << "NO";
    }
}


Java




// JAVA code for above approach
import java.util.*;
class GFG
{
 
  // Function to check if there exist
  // an integer X, such that X fits in bits
  // represented by some array element and
  // X*X does not fit in bits represented
  // by some other element
  public static boolean check(int N, int A[])
  {
 
    // sorting the given array
    Arrays.sort(A);
 
    // iterating through the array
    for (int i = 1; i < N; i++) {
 
      // If an element is equal to the
      // last element simply skip that
      // case and continue the iteration
      // as the task is to find two
      // distinct integers such that
      // X fits in one them and
      // X*X does not fit in other
      if (A[i] == A[i - 1]) {
        continue;
      }
 
      // If the value of an element is
      // less than twice of its
      // previous element, that means
      // X would fit in previous element bits,
      // but X*X would not fit in current
      // element bits
      if (A[i] < 2 * A[i - 1]) {
        return true;
      }
    }
 
    // return false if iterations completes
    return false;
  }
 
  // Driver Code
  public static void main(String args[])
  {
    int N = 4;
    int A[] = new int[] { 4, 2, 3, 1 };
    boolean answer = check(N, A);
    if (answer == true) {
      System.out.print("YES");
    }
    else {
      System.out.print("NO");
    }
  }
}
 
// This code is contributed by Taranpreet


Python3




# Python code for above approach
 
# Function to check if there exist
# an integer X, such that X fits in bits
# represented by some array element and
# X*X does not fit in bits represented
# by some other element
def check(N, A):
 
    # sorting the given array
    A.sort()
 
    # iterating through the array
    for i in range(1,N):
 
        # If an element is equal to the
        # last element simply skip that
        # case and continue the iteration
        # as the task is to find two
        # distinct integers such that
        # X fits in one them and
        # X*X does not fit in other
        if (A[i] == A[i - 1]):
            continue
 
        # If the value of an element is
        # less than twice of its
        # previous element, that means
        # X would fit in previous element bits,
        # but X*X would not fit in current
        # element bits
        if (A[i] < 2 * A[i - 1]):
            return True
 
    # return false if iterations completes
    return False
 
# Driver Code
N = 4
A = [ 4, 2, 3, 1 ]
answer = check(N, A)
if (answer == True):
    print("YES")
else:
    print("NO")
     
# This code is contributed by shinjanpatra


C#




// C# code for above approach
using System;
 
public class GFG{
 
  // Function to check if there exist
  // an integer X, such that X fits in bits
  // represented by some array element and
  // X*X does not fit in bits represented
  // by some other element
  static bool check(int N, int[] A)
  {
 
    // sorting the given array
    Array.Sort(A);
 
    // iterating through the array
    for (int i = 1; i < N; i++) {
 
      // If an element is equal to the
      // last element simply skip that
      // case and continue the iteration
      // as the task is to find two
      // distinct integers such that
      // X fits in one them and
      // X*X does not fit in other
      if (A[i] == A[i - 1]) {
        continue;
      }
 
      // If the value of an element is
      // less than twice of its
      // previous element, that means
      // X would fit in previous element bits,
      // but X*X would not fit in current
      // element bits
      if (A[i] < 2 * A[i - 1]) {
        return true;
      }
    }
 
    // return false if iterations completes
    return false;
  }
 
  // Driver Code
  static public void Main (){
 
    int N = 4;
    int[] A = { 4, 2, 3, 1 };
    bool answer = check(N, A);
    if (answer == true) {
      Console.Write("YES");
    }
    else {
      Console.Write("NO");
    }
  }
}
 
// This code is contributed by hrithikgarg03188.


Javascript




<script>
        // JavaScript code for the above approach
 
        // Function to check if there exist
        // an integer X, such that X fits in bits
        // represented by some array element and
        // X*X does not fit in bits represented
        // by some other element
        function check(N, A) {
 
            // sorting the given array
            A.sort();
 
            // iterating through the array
            for (let i = 1; i < N; i++) {
 
                // If an element is equal to the
                // last element simply skip that
                // case and continue the iteration
                // as the task is to find two
                // distinct integers such that
                // X fits in one them and
                // X*X does not fit in other
                if (A[i] == A[i - 1]) {
                    continue;
                }
 
                // If the value of an element is
                // less than twice of its
                // previous element, that means
                // X would fit in previous element bits,
                // but X*X would not fit in current
                // element bits
                if (A[i] < 2 * A[i - 1]) {
                    return true;
                }
            }
 
            // return false if iterations completes
            return false;
        }
 
        // Driver Code
        let N = 4;
        let A = [4, 2, 3, 1];
        let answer = check(N, A);
        if (answer == true) {
            document.write("YES");
        }
        else {
            document.write("NO");
        }
 
   // This code is contributed by Potta Lokesh
    </script>


 
 

Output

YES

 

Time Complexity: O(N*log(N))
Auxiliary Space: O(1)

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads