Given an array X[] of length N, the task is to verify whether each possible subarray has a different bitwise OR value. If possible then return YES else NO.
Examples:
Input: N = 2, X[] = {1, 2}
Output: YES
Explanation: All the possible sub-arrays are {1}, {2}, {1, 2} having OR values as 1, 2 and 3 respectively. All possible sub-arrays have different OR values. Therefore, output is YES.Input: N = 4, X[]={5, 6, 3, 4}
Output: NO
Explanation: It can be verified that all the possible sub-arrays don’t have distinct bitwise OR value.
Approach: Implement the idea below to solve the problem
The problem is observation based and can be solved by using some bitwise concept observations. This problem requires HashSet data-structure.
Steps were taken to solve the problem:
- Create a boolean flag and initialize it as equal to false.
- If N ? 100, then output NO else follow the below-mentioned steps:
- Initialize the HashSet let’s say set.
- Run two nested loops for all the possible sub-arrays and initialize their OR value into the set.
- If N ? 100, then output NO else follow the below-mentioned steps:
- If set.size() == (N*(N+1)/2) then mark the flag as true else mark false.
- If the flag is true return YES else return NO.
Below is the code to implement the above approach:
// C++ code to implement the approach #include <bits/stdc++.h> using namespace std;
// Function for verifying the array string Is_Possible( int N, long X[])
{ // Flag initialized for final output
bool flag = false ;
// Condition at which array will
// not be verified
if (N >= 100)
cout << "NO" << endl;
else {
// Set initialized
set < long > s;
// Loop for initializing OR
// values into Set
for ( int i = 0; i < N; i++) {
long x = 0;
for ( int j = i; j < N; j++) {
x = x | X[j];
s.insert(x);
}
}
// Checking for the necessary
// conditions and Marking
// flag for output
if (s.size() == (N * (N + 1) / 2))
flag = true ;
else
flag = false ;
}
// Returning answer as output
return flag == true ? "YES" : "NO" ;
} int main() {
// Input values
int N = 2;
long X[] = { 1, 2 };
// Function call
cout << Is_Possible(N, X) << endl;
} |
// Java code to implement the approach import java.io.*;
import java.lang.*;
import java.util.*;
class Main {
// Driver Function
public static void main(String[] args)
throws java.lang.Exception
{
// Input values
int N = 2 ;
long X[] = { 1 , 2 };
// Function call
System.out.println(Is_Possible(N, X));
}
// Function for verifying the array
static String Is_Possible( int N, long X[])
{
// Flag initialized for
// final output
boolean flag = false ;
// Condition at which array will
// not be verified
if (N >= 100 )
System.out.println( "NO" );
else {
// HashSet initialized
Set<Long> set = new HashSet<>();
// Loop for initializing OR
// values into HashSet
for ( int i = 0 ; i < N; i++) {
long x = 0 ;
for ( int j = i; j < N; j++) {
x = x | X[j];
set.add(x);
}
}
// Checking for the necessary
// conditions and Marking
// flag for output
if (set.size() == (N * (N + 1 ) / 2 ))
flag = true ;
else
flag = false ;
}
// Returning answer as output
return flag == true ? "YES" : "NO" ;
}
} |
# Python code to implement the approach # Function for verifying the array def Is_Possible(N, X):
# Flag initialized for final output
flag = False
# Condition at which array will
# not be verified
if N > = 100 :
return "NO"
else :
# Set initialized
s = set ()
# Loop for initializing OR
# values into Set
for i in range (N):
x = 0
for j in range (i, N):
x = x | X[j]
s.add(x)
# Checking for the necessary
# conditions and marking
# flag for output
if len (s) = = (N * (N + 1 ) / / 2 ):
flag = True
else :
flag = False
# Returning answer as output
return "YES" if flag else "NO"
# Driver code N = 2
X = [ 1 , 2 ]
# Function call print (Is_Possible(N, X))
# This code is contributed by prasad264 |
using System;
using System.Collections.Generic;
class MainClass {
// Driver Function
static void Main( string [] args) {
// Input values
int N = 2;
long [] X = new long [] { 1, 2 };
// Function call
Console.WriteLine(Is_Possible(N, X));
}
// Function for verifying the array
static string Is_Possible( int N, long [] X) {
// Flag initialized for
// final output
bool flag = false ;
// Condition at which array will
// not be verified
if (N >= 100)
Console.WriteLine( "NO" );
else {
// HashSet initialized
HashSet< long > set = new HashSet< long >();
// Loop for initializing OR
// values into HashSet
for ( int i = 0; i < N; i++) {
long x = 0;
for ( int j = i; j < N; j++) {
x = x | X[j];
set .Add(x);
}
}
// Checking for the necessary
// conditions and Marking
// flag for output
if ( set .Count == (N * (N + 1) / 2))
flag = true ;
else
flag = false ;
}
// Returning answer as output
return flag == true ? "YES" : "NO" ;
}
} |
// JavaScript code to implement the approach // Function for verifying the array function Is_Possible(N, X)
{ // Flag initialized for final output
let flag = false ;
// Condition at which array will
// not be verified
if (N >= 100)
console.log( "NO" );
else {
// Set initialized
const s = new Set();
// Loop for initializing OR
// values into Set
for (let i = 0; i < N; i++) {
let x = 0;
for (let j = i; j < N; j++) {
x = x | X[j];
s.add(x);
}
}
// Checking for the necessary
// conditions and Marking
// flag for output
if (s.size == (N * (N + 1) / 2))
flag = true ;
else
flag = false ;
}
// Returning answer as output
return flag == true ? "YES" : "NO" ;
} // Input values const N = 2; const X = [ 1, 2 ]; // Function call console.log(Is_Possible(N, X)); |
YES
Time Complexity: O(N)
Auxiliary Space: O(N), As HashSet is used to store elements.