给定一个由N和整数K组成的数组arr[] ,任务是打印arr[]的元素,其最右边的设置位与K 中最右边的设置位位于相同的位置。
例子:
Input: arr[] = { 3, 4, 6, 7, 9, 12, 15 }, K = 7
Output: { 3, 7, 9, 15 }
Explanation:
Binary representation of K (= 7) is 0111.
Rightmost set bit in 7 is at position 1.
Therefore, all odd array elements will have rightmost set bit at position 1.
Input: arr[] = { 1, 2, 3, 4, 5 }, K = 3
Output: {1, 3, 5}
处理方法:按照以下步骤解决问题:
- 初始化一个变量,比如mask ,来存储K的掩码。
- 初始化一个变量,比如pos ,以存储K 中最右边的设置位的位置。
- 计算掩码和K的按位与,即pos = (mask & K)
- 遍历数组arr[]并针对每个数组元素:
- 如果掩码 & arr[i] == pos:打印 arr[i]。
- 否则,继续。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include "bits/stdc++.h"
using namespace std;
// Function to find the mask for
// finding rightmost set bit in K
int findMask(int K)
{
int mask = 1;
while ((K & mask) == 0)
{
mask = mask << 1;
}
return mask;
}
// Function to find all array elements
// with rightmost set bit same as that in K
void sameRightSetBitPos(
int arr[], int N, int K)
{
// Stores mask of K
int mask = findMask(K);
// Store position of rightmost set bit
int pos = (K & mask);
// Traverse the array
for (int i = 0; i < N; i++)
{
// Check if rightmost set bit of
// current array element is same as
// position of rightmost set bit in K
if ((arr[i] & mask) == pos)
cout << arr[i] << " ";
}
}
// Driver Code
int main()
{
// Input
int arr[] = { 3, 4, 6, 7, 9, 12, 15 };
int N = sizeof(arr) / sizeof(arr[0]);
int K = 7;
// Function call to find
// the elements having same
// rightmost set bit as of K
sameRightSetBitPos(arr, N, K);
return 0;
}
// This code is contributed by susmitakundugoaldanga.
Java
// Java program for
// the above approach
import java.io.*;
class GFG {
// Function to find the mask for
// finding rightmost set bit in K
static int findMask(int K)
{
int mask = 1;
while ((K & mask) == 0) {
mask = mask << 1;
}
return mask;
}
// Function to find all array elements
// with rightmost set bit same as that in K
public static void sameRightSetBitPos(
int[] arr, int N, int K)
{
// Stores mask of K
int mask = findMask(K);
// Store position of rightmost set bit
final int pos = (K & mask);
// Traverse the array
for (int i = 0; i < N; i++) {
// Check if rightmost set bit of
// current array element is same as
// position of rightmost set bit in K
if ((arr[i] & mask) == pos)
System.out.print(arr[i] + " ");
}
}
// Driver Code
public static void main(String[] args)
{
// Input
int[] arr = { 3, 4, 6, 7, 9, 12, 15 };
int N = arr.length;
int K = 7;
// Function call to find
// the elements having same
// rightmost set bit as of K
sameRightSetBitPos(arr, N, K);
}
}
Python3
# Python program for
# the above approach
# Function to find the mask for
# finding rightmost set bit in K
def findMask(K):
mask = 1;
while ((K & mask) == 0):
mask = mask << 1;
return mask;
# Function to find all array elements
# with rightmost set bit same as that in K
def sameRightSetBitPos(arr, N, K):
# Stores mask of K
mask = findMask(K);
# Store position of rightmost set bit
pos = (K & mask);
# Traverse the array
for i in range(N):
# Check if rightmost set bit of
# current array element is same as
# position of rightmost set bit in K
if ((arr[i] & mask) == pos):
print(arr[i], end=" ");
# Driver Code
if __name__ == '__main__':
# Input
arr = [3, 4, 6, 7, 9, 12, 15];
N = len(arr);
K = 7;
# Function call to find
# the elements having same
# rightmost set bit as of K
sameRightSetBitPos(arr, N, K);
# This code contributed by shikhasingrajput
C#
// C# program for the above approach
using System;
public class GFG
{
// Function to find the mask for
// finding rightmost set bit in K
static int findMask(int K)
{
int mask = 1;
while ((K & mask) == 0) {
mask = mask << 1;
}
return mask;
}
// Function to find all array elements
// with rightmost set bit same as that in K
public static void sameRightSetBitPos(
int[] arr, int N, int K)
{
// Stores mask of K
int mask = findMask(K);
// Store position of rightmost set bit
int pos = (K & mask);
// Traverse the array
for (int i = 0; i < N; i++) {
// Check if rightmost set bit of
// current array element is same as
// position of rightmost set bit in K
if ((arr[i] & mask) == pos)
Console.Write(arr[i] + " ");
}
}
// Driver Code
static public void Main ()
{
// Input
int[] arr = { 3, 4, 6, 7, 9, 12, 15 };
int N = arr.Length;
int K = 7;
// Function call to find
// the elements having same
// rightmost set bit as of K
sameRightSetBitPos(arr, N, K);
}
}
// This code is contributed by code_hunt.
Javascript
输出:
3 7 9 15
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live