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

📅  最后修改于: 2021-09-07 02:20:57             🧑  作者: Mango

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

例子:

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

  • 初始化一个变量count来存储所需的计数,一个向量V来存储每个数组元素的反转位,以及一个 Map 来存储原始数组中的数组元素。
  • 遍历给定的数组arr[]并执行以下步骤:
    • 存储通过反转向量V中元素arr[i]的二进制表示的每一位而形成的数字。
    • 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.


Javascript


输出:
4

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live