Construct a sorted Array such that setbit in bitwise XOR of any pair is even
Last Updated :
07 Nov, 2022
Given an integer N(1 ≤ N ≤ 500), Construct an integer arr[] of length N, such that it should follow all the given conditions below:
- Each element of arr[] should be distinct.
- Binary representation of (arr[i]^arr[j]) should contain even number of set bits for all ( 1≤i≤N ) and ( i≠j ).
- All the elements in arr[] should be in sorted order.
- Ai > 0 for all (1 ≤ i ≤ N).
Examples:
Input: N = 2
Output: {10, 15}
Explanation: It can be verified that all elements of arr[]
in each output are distinct and in sorted order and
all the possible pairs of arr[] fulfill the condition mentioned in problem statement..
Input: N = 3
Output: {3, 5, 12}
Approach: To solve the problem follow the below observation:
It can be observe from outputs that arr[] contains only elements having even parity of set bits in their binary representation. If we try to get some such type of elements under range 1 to 10 using a brute-force code we will get a mathematical series as { 3, 5, 6, 9}.
This series is called “Evil Number“ series and related to Number theory. This series follows all the given conditions of the problem. Therefore, This problem can be solve by printing first N terms of Evil Number series(Excluding 0, As Arr[i] should be greater than zero).
Follow the steps to solve the problem:
- Print the first N terms of Evil number Series or First N integers greater than zero having even parity of set bits.
Below is the implementation for the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string toBinary( int n)
{
string r;
while (n!=0) {r=(n%2==0 ? "0" : "1" )+r; n/=2;}
return r;
}
int count1(string str)
{
int counter = 0;
for ( int i = 0; i < str.size(); i++)
{
if (str[i] == '1' )
{
counter++;
}
}
return counter;
}
int main() {
int N = 10;
int counter = 1;
for ( int i = 1; i <= N; i++) {
while ((count1(toBinary(counter)))
% 2
!= 0) {
counter++;
}
cout<<counter<< " " ;
counter++;
}
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
public static void main(String[] args)
throws java.lang.Exception
{
int N = 10 ;
int counter = 1 ;
for ( int i = 1 ; i <= N; i++) {
while ((count1(Integer.toBinaryString(counter)))
% 2
!= 0 ) {
counter++;
}
System.out.print(counter + " " );
counter++;
}
}
static int count1(String str)
{
int counter = 0 ;
for ( int i = 0 ; i < str.length(); i++) {
if (str.charAt(i) == '1' )
counter++;
}
return counter;
}
}
|
Python3
class GFG :
@staticmethod
def main( args) :
N = 10
counter = 1
i = 1
while (i < = N) :
while ((GFG.count1( str ( bin (counter)))) % 2 ! = 0 ) :
counter + = 1
print ( str (counter) + " " , end = "")
counter + = 1
i + = 1
@staticmethod
def count1( str ) :
counter = 0
i = 0
while (i < len ( str )) :
if ( str [i] = = '1' ) :
counter + = 1
i + = 1
return counter
if __name__ = = "__main__" :
GFG.main([])
|
C#
using System;
public class GFG
{
public static void Main(String[] args)
{
var N = 10;
var counter = 1;
for ( int i = 1; i <= N; i++)
{
while ((GFG.count1(Convert.ToString(counter, 2))) % 2 != 0)
{
counter++;
}
Console.Write(counter.ToString() + " " );
counter++;
}
}
public static int count1(String str)
{
var counter = 0;
for ( int i = 0; i < str.Length; i++)
{
if (str[i] == '1' )
{
counter++;
}
}
return counter;
}
}
|
Javascript
function toBinary(n)
{
let r = "" ;
while (n != 0) {r = (n % 2 == 0 ? "0" : "1" ) + r; n = Math.floor(n/2);}
return r;
}
function count1(str)
{
let counter = 0;
for (let i = 0; i < str.length; i++)
{
if (str[i] == '1' )
{
counter++;
}
}
return counter;
}
let N = 10;
let counter = 1;
for (let i = 1; i <= N; i++) {
while ((count1(toBinary(counter)))
% 2
!= 0) {
counter++;
}
console.log(counter);
counter++;
}
|
Output
3 5 6 9 10 12 15 17 18 20
Time Complexity: O(N2)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...