Count remaining array elements after reversing binary representation of each array element
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 arrayInput: 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++
// C++ program for the above approach#include <bits/stdc++.h>using namespace std;// Function to reverse the binary// representation of a numberint findReverse(int N){ int rev = 0; // Traverse bits of N // from the right while (N > 0) { // Bitwise left // shift 'rev' by 1 rev <<= 1; // If current bit is '1' if (N & 1 == 1) rev ^= 1; // Bitwise right // shift N by 1 N >>= 1; } // Required number return rev;}// Function to count elements from the// original array that are also present// in the array formed by reversing the// binary representation of each elementvoid countElements(int arr[], int N){ // Stores the reversed num vector<int> ans; // Iterate from [0, N] for (int i = 0; i < N; i++) { ans.push_back(findReverse(arr[i])); } // Stores the presence of integer unordered_map<int, int> cnt; for (int i = 0; i < N; i++) { cnt[arr[i]] = 1; } // Stores count of elements // present in original array int count = 0; // Traverse the array for (auto i : ans) { // If current number is present if (cnt[i]) count++; } // Print the answer cout << count << endl;}// Driver Codeint main(){ int arr[] = { 1, 30, 3, 8, 12 }; int N = sizeof(arr) / sizeof(arr[0]); countElements(arr, N); return 0;} |
Java
// Java program for the above approachimport java.util.*;public class GFG{ // Function to reverse the binary // representation of a number static int findReverse(int N) { int rev = 0; // Traverse bits of N // from the right while (N > 0) { // Bitwise left // shift 'rev' by 1 rev <<= 1; // If current bit is '1' if ((N & 1) == 1) rev ^= 1; // Bitwise right // shift N by 1 N >>= 1; } // Required number return rev; } // Function to count elements from the // original array that are also present // in the array formed by reversing the // binary representation of each element static void countElements(int arr[], int N) { // Stores the reversed num Vector<Integer> ans = new Vector<Integer>(); // Iterate from [0, N] for (int i = 0; i < N; i++) { ans.add(findReverse(arr[i])); } // Stores the presence of integer HashMap<Integer, Integer> cnt = new HashMap<Integer, Integer>(); for (int i = 0; i < N; i++) { cnt.put(arr[i], 1); } // Stores count of elements // present in original array int count = 0; // Traverse the array for(Integer i : ans) { // If current number is present if (cnt.containsKey(i)) count++; } // Print the answer System.out.println(count); } // Driver code public static void main(String[] args) { int[] arr = { 1, 30, 3, 8, 12 }; int N = arr.length; countElements(arr, N); }}// This code is contributed by divyeshrabadiya07. |
Python3
# Python 3 program for the above approach# Function to reverse the binary# representation of a numberdef findReverse(N): rev = 0 # Traverse bits of N # from the right while (N > 0): # Bitwise left # shift 'rev' by 1 rev <<= 1 # If current bit is '1' if (N & 1 == 1): rev ^= 1 # Bitwise right # shift N by 1 N >>= 1 # Required number return rev# Function to count elements from the# original array that are also present# in the array formed by reversing the# binary representation of each elementdef countElements(arr, N): # Stores the reversed num ans = [] # Iterate from [0, N] for i in range(N): ans.append(findReverse(arr[i])) # Stores the presence of integer cnt = {} for i in range(N): cnt[arr[i]] = 1 # Stores count of elements # present in original array count = 0 # Traverse the array for i in ans: # If current number is present if (i in cnt): count += 1 # Print the answer print(count)# Driver Codeif __name__ == '__main__': arr = [1, 30, 3, 8, 12] N = len(arr) countElements(arr, N) # This code is contributed by SURENDRA_GANGWAR. |
C#
// C# program for the above approachusing System;using System.Collections.Generic;class GFG{ // Function to reverse the binary // representation of a number static int findReverse(int N) { int rev = 0; // Traverse bits of N // from the right while (N > 0) { // Bitwise left // shift 'rev' by 1 rev <<= 1; // If current bit is '1' if ((N & 1) == 1) rev ^= 1; // Bitwise right // shift N by 1 N >>= 1; } // Required number return rev; } // Function to count elements from the // original array that are also present // in the array formed by reversing the // binary representation of each element static void countElements(int[] arr, int N) { // Stores the reversed num List<int> ans = new List<int>(); // Iterate from [0, N] for (int i = 0; i < N; i++) { ans.Add(findReverse(arr[i])); } // Stores the presence of integer Dictionary<int, int> cnt = new Dictionary<int, int>(); for (int i = 0; i < N; i++) { cnt[arr[i]] = 1; } // Stores count of elements // present in original array int count = 0; // Traverse the array foreach(int i in ans) { // If current number is present if (cnt.ContainsKey(i)) count++; } // Print the answer Console.WriteLine(count); } // Driver Code public static void Main() { int[] arr = { 1, 30, 3, 8, 12 }; int N = arr.Length; countElements(arr, N); }}// This code is contributed by chitranayal. |
Javascript
<script>// Js program for the above approach// Function to reverse the binary// representation of a numberfunction findReverse(N) { let rev = 0; // Traverse bits of N // from the right while (N > 0) { // Bitwise left // shift 'rev' by 1 rev <<= 1; // If current bit is '1' if (N & 1 == 1) rev ^= 1; // Bitwise right // shift N by 1 N >>= 1; } // Required number return rev;}// Function to count elements from the// original array that are also present// in the array formed by reversing the// binary representation of each elementfunction countElements( arr, N){ // Stores the reversed num let ans=[]; // Iterate from [0, N] for (let i = 0; i < N; i++) { ans.push(findReverse(arr[i])); } // Stores the presence of integer let cnt = new Map(); for (let i = 0; i < N; i++) { cnt[arr[i]] = 1; } // Stores count of elements // present in original array let count = 0; // Traverse the array for (let i = 0;i<ans.length;i++) { // If current number is present if (cnt[ans[i]]) count++; } // Print the answer document.write( count,'<br>');}// Driver Codelet arr = [ 1, 30, 3, 8, 12 ];let N = arr.length;countElements(arr, N);</script> |
4
Time Complexity: O(N)
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.


