📜  在K中最右置位的位置查找最右置位的数组元素

📅  最后修改于: 2021-05-18 00:25:14             🧑  作者: Mango

给定一个由N和整数K组成的数组arr [] ,任务是打印arr []的元素,这些元素的最右边的设置位与K中最右边的设置位在同一位置。

例子:

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

  1. 初始化一个变量,例如mask ,以存储K的掩码。
  2. 初始化一个变量pos ,以存储K中最右边的设置位的位置。
  3. 计算掩码K的按位与,即pos = (mask&K)
  4. 遍历数组arr [],并遍历每个数组元素:
    • 如果mask&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)