Count remaining array elements after reversing binary representation of each array element
Last Updated :
31 May, 2022
Given an array arr[] consisting of N positive integers, the task is to modify every array element by reversing them binary representation and count the number of elements in the modified array that were also present in the original array.
Examples:
Input: arr[] = {2, 4, 5, 20, 16}
Output: 2
Explanation:
2 -> (10)2 -> (1) 2 -> 1 i.e. not present in the original array
4 -> (100 )2 -> (1 )2 -> 1 i.e. not present in the original array
5 -> (101 )2 -> (101 )2 -> 5 i.e. present in the original array
20 -> (10100)2 -> (101)2 -> 5 i.e. present in the original array
16 -> (10000)2 -> (1)2 -> 1 i.e. not present in the original array
Input: arr[] = {1, 30, 3, 8, 12}
Output: 4
Approach: Follow the steps below to solve the problem:
- Initialize a variable, say count, to store the required count, a vector, say V, to store the reversed bits of each array element, and a Map to store the array elements in the original array.
- Traverse the given array arr[] and perform the following steps:
- Store the number formed by reversing each bit of binary representation of element arr[i] in the vector V.
- Mark the presence of the current element arr[i] in the Map.
- Traverse the vector V, and if any element present in the vector is also present in the Map, then increment count by 1.
- After completing the above steps, print the value of the count as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findReverse( int N)
{
int rev = 0;
while (N > 0) {
rev <<= 1;
if (N & 1 == 1)
rev ^= 1;
N >>= 1;
}
return rev;
}
void countElements( int arr[], int N)
{
vector< int > ans;
for ( int i = 0; i < N; i++) {
ans.push_back(findReverse(arr[i]));
}
unordered_map< int , int > cnt;
for ( int i = 0; i < N; i++) {
cnt[arr[i]] = 1;
}
int count = 0;
for ( auto i : ans) {
if (cnt[i])
count++;
}
cout << count << endl;
}
int main()
{
int arr[] = { 1, 30, 3, 8, 12 };
int N = sizeof (arr) / sizeof (arr[0]);
countElements(arr, N);
return 0;
}
|
Java
import java.util.*;
public class GFG
{
static int findReverse( int N)
{
int rev = 0 ;
while (N > 0 )
{
rev <<= 1 ;
if ((N & 1 ) == 1 )
rev ^= 1 ;
N >>= 1 ;
}
return rev;
}
static void countElements( int arr[], int N)
{
Vector<Integer> ans = new Vector<Integer>();
for ( int i = 0 ; i < N; i++)
{
ans.add(findReverse(arr[i]));
}
HashMap<Integer, Integer> cnt = new HashMap<Integer, Integer>();
for ( int i = 0 ; i < N; i++)
{
cnt.put(arr[i], 1 );
}
int count = 0 ;
for (Integer i : ans) {
if (cnt.containsKey(i))
count++;
}
System.out.println(count);
}
public static void main(String[] args)
{
int [] arr = { 1 , 30 , 3 , 8 , 12 };
int N = arr.length;
countElements(arr, N);
}
}
|
Python3
def findReverse(N):
rev = 0
while (N > 0 ):
rev << = 1
if (N & 1 = = 1 ):
rev ^ = 1
N >> = 1
return rev
def countElements(arr, N):
ans = []
for i in range (N):
ans.append(findReverse(arr[i]))
cnt = {}
for i in range (N):
cnt[arr[i]] = 1
count = 0
for i in ans:
if (i in cnt):
count + = 1
print (count)
if __name__ = = '__main__' :
arr = [ 1 , 30 , 3 , 8 , 12 ]
N = len (arr)
countElements(arr, N)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int findReverse( int N)
{
int rev = 0;
while (N > 0) {
rev <<= 1;
if ((N & 1) == 1)
rev ^= 1;
N >>= 1;
}
return rev;
}
static void countElements( int [] arr, int N)
{
List< int > ans = new List< int >();
for ( int i = 0; i < N; i++) {
ans.Add(findReverse(arr[i]));
}
Dictionary< int , int > cnt
= new Dictionary< int , int >();
for ( int i = 0; i < N; i++) {
cnt[arr[i]] = 1;
}
int count = 0;
foreach ( int i in ans)
{
if (cnt.ContainsKey(i))
count++;
}
Console.WriteLine(count);
}
public static void Main()
{
int [] arr = { 1, 30, 3, 8, 12 };
int N = arr.Length;
countElements(arr, N);
}
}
|
Javascript
<script>
function findReverse(N) {
let rev = 0;
while (N > 0) {
rev <<= 1;
if (N & 1 == 1)
rev ^= 1;
N >>= 1;
}
return rev;
}
function countElements( arr, N)
{
let ans=[];
for (let i = 0; i < N; i++) {
ans.push(findReverse(arr[i]));
}
let cnt = new Map();
for (let i = 0; i < N; i++) {
cnt[arr[i]] = 1;
}
let count = 0;
for (let i = 0;i<ans.length;i++) {
if (cnt[ans[i]])
count++;
}
document.write( count, '<br>' );
}
let arr = [ 1, 30, 3, 8, 12 ];
let N = arr.length;
countElements(arr, N);
</script>
|
Time Complexity: O(N), as we are using a loop to traverse N times.
Auxiliary Space: O(N), as we are using extra space for the map cnt.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...