给定一个由N 个正整数组成的数组arr[] ,任务是通过反转它们的二进制表示来修改每个数组元素,并计算修改后的数组中也存在于原始数组中的元素的数量。
例子:
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
处理方法:按照以下步骤解决问题:
- 初始化一个变量count来存储所需的计数,一个向量V来存储每个数组元素的反转位,以及一个 Map 来存储原始数组中的数组元素。
- 遍历给定的数组arr[]并执行以下步骤:
- 存储通过反转向量V中元素arr[i]的二进制表示的每一位而形成的数字。
- 在Map 中标记当前元素arr[i]的存在。
- 遍历向量 V,如果向量中存在的任何元素也存在于Map 中,则将count增加1 。
- 完成上述步骤后,打印计数的值作为结果。
下面是上述方法的实现:
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)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。