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
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++
#include <bits/stdc++.h>
using namespace std;
bool check( int N, int A[])
{
sort(A, A + N);
for ( int i = 1; i < N; i++) {
if (A[i] == A[i - 1]) {
continue ;
}
if (A[i] < 2 * A[i - 1]) {
return true ;
}
}
return false ;
}
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
import java.util.*;
class GFG
{
public static boolean check( int N, int A[])
{
Arrays.sort(A);
for ( int i = 1 ; i < N; i++) {
if (A[i] == A[i - 1 ]) {
continue ;
}
if (A[i] < 2 * A[i - 1 ]) {
return true ;
}
}
return false ;
}
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" );
}
}
}
|
Python3
def check(N, A):
A.sort()
for i in range ( 1 ,N):
if (A[i] = = A[i - 1 ]):
continue
if (A[i] < 2 * A[i - 1 ]):
return True
return False
N = 4
A = [ 4 , 2 , 3 , 1 ]
answer = check(N, A)
if (answer = = True ):
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
public class GFG{
static bool check( int N, int [] A)
{
Array.Sort(A);
for ( int i = 1; i < N; i++) {
if (A[i] == A[i - 1]) {
continue ;
}
if (A[i] < 2 * A[i - 1]) {
return true ;
}
}
return false ;
}
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" );
}
}
}
|
Javascript
<script>
function check(N, A) {
A.sort();
for (let i = 1; i < N; i++) {
if (A[i] == A[i - 1]) {
continue ;
}
if (A[i] < 2 * A[i - 1]) {
return true ;
}
}
return false ;
}
let N = 4;
let A = [4, 2, 3, 1];
let answer = check(N, A);
if (answer == true ) {
document.write( "YES" );
}
else {
document.write( "NO" );
}
</script>
|
Time Complexity: O(N*log(N))
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...