📌  相关文章
📜  反转每个数组元素的二进制表示形式后,计数剩余的数组元素

📅  最后修改于: 2021-05-17 20:53:31             🧑  作者: Mango

给定一个由N个正整数组成的数组arr [] ,任务是通过反转每个数组元素的二进制表示来对其进行修改,并计算修改后的数组中还存在于原始数组中的元素的数量。

例子:

方法:请按照以下步骤解决问题:

  • 初始化一个变量,例如count ,以存储所需的计数;初始化一个向量,即V ,以存储每个数组元素的反转位;以及一个Map,以将数组元素存储在原始数组中。
  • 遍历给定数组arr []并执行以下步骤:
    • 将通过反转元素arr [i]的二进制表示形式的每个位存储在向量V中形成的数字
    • Map中标记当前元素arr [i]的存在。
  • 遍历向量V,如果向量中存在的任何元素也存在于Map中,则将count递增1
  • 完成上述步骤后,打印count的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
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 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 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
// 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 ans = new Vector();
 
    // Iterate from [0, N]
    for (int i = 0; i < N; i++)
    {
      ans.add(findReverse(arr[i]));
    }
 
    // Stores the presence of integer
    HashMap cnt = new HashMap();
    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 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#
// 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 ans = new List();
 
    // Iterate from [0, N]
    for (int i = 0; i < N; i++) {
      ans.Add(findReverse(arr[i]));
    }
 
    // Stores the presence of integer
    Dictionary cnt
      = new Dictionary();
    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.


输出:
4

时间复杂度: O(N)
辅助空间: O(N)