Open In App

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 array

Input: arr[] = {1, 30, 3, 8, 12}
Output: 4



Approach: Follow the steps below to solve the problem:

Below is the implementation of the above approach:




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to reverse the binary
// representation of a number
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
void 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 Code
int main()
{
    int arr[] = { 1, 30, 3, 8, 12 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    countElements(arr, N);
 
    return 0;
}




// Java program for the above approach
import 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.




# Python 3 program for the above approach
 
# Function to reverse the binary
# representation of a number
def 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 element
def 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 Code
if __name__ == '__main__':
    arr =  [1, 30, 3, 8, 12]
    N =  len(arr)
 
    countElements(arr, N)
 
    # This code is contributed by SURENDRA_GANGWAR.




// C# program for the above approach
using 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.




<script>
// Js program for the above approach
 
// Function to reverse the binary
// representation of a number
function 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 element
function 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 Code
let arr = [ 1, 30, 3, 8, 12 ];
let N = arr.length;
 
countElements(arr, N);
</script>

Output: 
4

 

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.


Article Tags :