Find array elements with rightmost set bit at the position of the rightmost set bit in K
Last Updated :
26 Oct, 2023
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:
- Initialize a variable, say mask, to store the mask of K.
- Initialize a variable, say pos, to store the position of the rightmost set bit in K.
- Calculate the Bitwise AND of the mask and K i.e. pos = (mask & K)
- 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++
#include "bits/stdc++.h"
using namespace std;
int findMask( int K)
{
int mask = 1;
while ((K & mask) == 0)
{
mask = mask << 1;
}
return mask;
}
void sameRightSetBitPos(
int arr[], int N, int K)
{
int mask = findMask(K);
int pos = (K & mask);
for ( int i = 0; i < N; i++)
{
if ((arr[i] & mask) == pos)
cout << arr[i] << " " ;
}
}
int main()
{
int arr[] = { 3, 4, 6, 7, 9, 12, 15 };
int N = sizeof (arr) / sizeof (arr[0]);
int K = 7;
sameRightSetBitPos(arr, N, K);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int findMask( int K)
{
int mask = 1 ;
while ((K & mask) == 0 ) {
mask = mask << 1 ;
}
return mask;
}
public static void sameRightSetBitPos(
int [] arr, int N, int K)
{
int mask = findMask(K);
final int pos = (K & mask);
for ( int i = 0 ; i < N; i++) {
if ((arr[i] & mask) == pos)
System.out.print(arr[i] + " " );
}
}
public static void main(String[] args)
{
int [] arr = { 3 , 4 , 6 , 7 , 9 , 12 , 15 };
int N = arr.length;
int K = 7 ;
sameRightSetBitPos(arr, N, K);
}
}
|
Python3
def findMask(K):
mask = 1 ;
while ((K & mask) = = 0 ):
mask = mask << 1 ;
return mask;
def sameRightSetBitPos(arr, N, K):
mask = findMask(K);
pos = (K & mask);
for i in range (N):
if ((arr[i] & mask) = = pos):
print (arr[i], end = " " );
if __name__ = = '__main__' :
arr = [ 3 , 4 , 6 , 7 , 9 , 12 , 15 ];
N = len (arr);
K = 7 ;
sameRightSetBitPos(arr, N, K);
|
C#
using System;
public class GFG
{
static int findMask( int K)
{
int mask = 1;
while ((K & mask) == 0) {
mask = mask << 1;
}
return mask;
}
public static void sameRightSetBitPos(
int [] arr, int N, int K)
{
int mask = findMask(K);
int pos = (K & mask);
for ( int i = 0; i < N; i++) {
if ((arr[i] & mask) == pos)
Console.Write(arr[i] + " " );
}
}
static public void Main ()
{
int [] arr = { 3, 4, 6, 7, 9, 12, 15 };
int N = arr.Length;
int K = 7;
sameRightSetBitPos(arr, N, K);
}
}
|
Javascript
<script>
function findMask(K)
{
let mask = 1;
while ((K & mask) == 0)
{
mask = mask << 1;
}
return mask;
}
function sameRightSetBitPos(arr, N, K)
{
let mask = findMask(K);
let pos = (K & mask);
for (let i = 0; i < N; i++)
{
if ((arr[i] & mask) == pos)
document.write(arr[i] + " " );
}
}
let arr = [ 3, 4, 6, 7, 9, 12, 15 ];
let N = arr.length;
let K = 7;
sameRightSetBitPos(arr, N, K);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Approach#2: Using list comprehension with bitwise operations
This approach to find the position of the rightmost set bit in the given number K, and then check if the rightmost set bit in each element of the given array is at the same position as K. If it is, then the element is added to the result list.
Algorithm
1. Find the position of the rightmost set bit in K using the bitwise AND operation with its 2’s complement.
2. Initialize an empty list result to store the elements of the array that have the rightmost set bit at the same position as K.
3. Iterate through each element num in the array:
a. Check if the rightmost set bit in num is at the same position as K using the bitwise AND operation with rightmost_set_bit.
b. If the rightmost set bit is at the same position, add num to the result list.
4. Return the result list.
C++
#include <iostream>
#include <vector>
using namespace std;
vector< int > find_elements(vector< int > arr, int K)
{
int rightmost_set_bit = K & -K;
vector< int > result;
for ( int num : arr) {
if (num & rightmost_set_bit == rightmost_set_bit) {
result.push_back(num);
}
}
return result;
}
int main()
{
vector< int > arr = { 3, 4, 6, 7, 9, 12, 15 };
int K = 7;
vector< int > result = find_elements(arr, K);
for ( int num : result) {
cout << num << " " ;
}
cout << endl;
return 0;
}
|
Java
import java.util.ArrayList;
import java.util.List;
public class Main {
public static List<Integer> findElements(List<Integer> arr, int K) {
int rightmostSetBit = K & -K;
List<Integer> result = new ArrayList<>();
for ( int num : arr) {
if ((num & rightmostSetBit) == rightmostSetBit) {
result.add(num);
}
}
return result;
}
public static void main(String[] args) {
List<Integer> arr = List.of( 3 , 4 , 6 , 7 , 9 , 12 , 15 );
int K = 7 ;
List<Integer> result = findElements(arr, K);
for ( int num : result) {
System.out.print(num + " " );
}
System.out.println();
}
}
|
Python3
def find_elements(arr, K):
rightmost_set_bit = K & - K
return [num for num in arr if num & rightmost_set_bit = = rightmost_set_bit]
arr = [ 3 , 4 , 6 , 7 , 9 , 12 , 15 ]
K = 7
result = find_elements(arr, K)
print ( " " .join( str (num) for num in result))
|
C#
using System;
using System.Collections.Generic;
class Program
{
static List< int > FindElements(List< int > arr, int K)
{
int rightmostSetBit = K & -K;
List< int > result = new List< int >();
foreach ( int num in arr)
{
if ((num & rightmostSetBit) == rightmostSetBit)
{
result.Add(num);
}
}
return result;
}
static void Main()
{
List< int > arr = new List< int > { 3, 4, 6, 7, 9, 12, 15 };
int K = 7;
List< int > result = FindElements(arr, K);
foreach ( int num in result)
{
Console.Write(num + " " );
}
Console.WriteLine();
}
}
|
Javascript
function find_elements(arr, K) {
let rightmost_set_bit = K & -K;
let result = [];
for (let num of arr) {
if ((num & rightmost_set_bit) === rightmost_set_bit) {
result.push(num);
}
}
return result;
}
let arr = [3, 4, 6, 7, 9, 12, 15];
let K = 7;
let result = find_elements(arr, K);
for (let num of result) {
console.log(num);
}
|
Time complexity: O(n), where n is the length of the input array. This is because the code iterates through each element of the array once to check if the rightmost set bit is at the same position as K.
Auxiliary Space: O(m), where m is the length of the result list. In the worst case, all elements of the array could have the rightmost set bit at the same position as K, so the result list could have m elements. However, in practice, the size of the result list will likely be much smaller than the size of the input array, so the space complexity will be closer to O(1) in most cases.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...