The Wayback Machine - https://web.archive.org/web/20210219091955/https://www.geeksforgeeks.org/find-array-elements-with-rightmost-set-bit-at-the-position-of-the-rightmost-set-bit-in-k/
Skip to content
Related Articles

Related Articles

Count array elements with rightmost set bit at the position of the rightmost set bit in K
  • Difficulty Level : Basic
  • Last Updated : 18 Feb, 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, 2, 3}

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


 
 

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 :