Skip to content
Related Articles

Related Articles

Find array elements with rightmost set bit at the position of the rightmost set bit in K
  • Difficulty Level : Medium
  • Last Updated : 05 Apr, 2021

Given an array arr[] consisting of N and an integer K, the task is to print the elements of arr[] whose rightmost set bit is at the same position as the rightmost set bit in K.

Examples:

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}

Approach: Follow the steps below to solve the problem:



  1. Initialize a variable, say mask, to store the mask of K.
  2. Initialize a variable, say pos, to store the position of the rightmost set bit in K.
  3. Calculate the Bitwise AND of the mask and K i.e. pos = (mask & K)
  4. Traverse the array arr[] and for each array element:
    • If mask & arr[i] == pos: Print arr[i].
    • Otherwise, continue.

Below is the implementation of the above approach:

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




<script>
// JavaScript program for the above approach
 
// Function to find the mask for
// finding rightmost set bit in K
    function findMask(K)
    {
        let 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
    function sameRightSetBitPos(arr, N, K)
    {
     
        // Stores mask of K
        let mask = findMask(K);
 
        // Store position of rightmost set bit
        let pos = (K & mask);
 
        // Traverse the array
        for (let 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)
                document.write(arr[i] + " ");
        }
    }
 
// Driver Code
 
    // Input
        let arr = [ 3, 4, 6, 7, 9, 12, 15 ];
        let N = arr.length;
        let 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 Manoj.
</script>

 
 

Output: 
3 7 9 15

 

Time Complexity: O(N)
Auxiliary Space: O(1) 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :