Check if each possible Subarray has a different bitwise OR value
Last Updated :
16 Mar, 2023
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 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++
#include <bits/stdc++.h>
using namespace std;
string Is_Possible( int N, long X[])
{
bool flag = false ;
if (N >= 100)
cout << "NO" << endl;
else {
set < long > s;
for ( int i = 0; i < N; i++) {
long x = 0;
for ( int j = i; j < N; j++) {
x = x | X[j];
s.insert(x);
}
}
if (s.size() == (N * (N + 1) / 2))
flag = true ;
else
flag = false ;
}
return flag == true ? "YES" : "NO" ;
}
int main() {
int N = 2;
long X[] = { 1, 2 };
cout << Is_Possible(N, X) << endl;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class Main {
public static void main(String[] args)
throws java.lang.Exception
{
int N = 2 ;
long X[] = { 1 , 2 };
System.out.println(Is_Possible(N, X));
}
static String Is_Possible( int N, long X[])
{
boolean flag = false ;
if (N >= 100 )
System.out.println( "NO" );
else {
Set<Long> set = new 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);
}
}
if (set.size() == (N * (N + 1 ) / 2 ))
flag = true ;
else
flag = false ;
}
return flag == true ? "YES" : "NO" ;
}
}
|
Python3
def Is_Possible(N, X):
flag = False
if N > = 100 :
return "NO"
else :
s = set ()
for i in range (N):
x = 0
for j in range (i, N):
x = x | X[j]
s.add(x)
if len (s) = = (N * (N + 1 ) / / 2 ):
flag = True
else :
flag = False
return "YES" if flag else "NO"
N = 2
X = [ 1 , 2 ]
print (Is_Possible(N, X))
|
C#
using System;
using System.Collections.Generic;
class MainClass {
static void Main( string [] args) {
int N = 2;
long [] X = new long [] { 1, 2 };
Console.WriteLine(Is_Possible(N, X));
}
static string Is_Possible( int N, long [] X) {
bool flag = false ;
if (N >= 100)
Console.WriteLine( "NO" );
else {
HashSet< long > set = new HashSet< long >();
for ( int i = 0; i < N; i++) {
long x = 0;
for ( int j = i; j < N; j++) {
x = x | X[j];
set .Add(x);
}
}
if ( set .Count == (N * (N + 1) / 2))
flag = true ;
else
flag = false ;
}
return flag == true ? "YES" : "NO" ;
}
}
|
Javascript
function Is_Possible(N, X)
{
let flag = false ;
if (N >= 100)
console.log( "NO" );
else {
const s = new 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);
}
}
if (s.size == (N * (N + 1) / 2))
flag = true ;
else
flag = false ;
}
return flag == true ? "YES" : "NO" ;
}
const N = 2;
const X = [ 1, 2 ];
console.log(Is_Possible(N, X));
|
Time Complexity: O(N)
Auxiliary Space: O(N), As HashSet is used to store elements.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...