Given an array A[] of length N, the task is to count the number of elements of the array having their MSB and LSB set.
Examples:
Input: A[] = {2, 3, 1, 5}
Output: 3
Explanation: Binary representation of the array elements {2, 3, 1, 5} are {10, 11, 1, 101}
The integers 3, 1, 5 are the integers whose first and last bit are set bit.
Input: A[] = {2, 6, 18, 4}
Output: 0
Naive approach: The basic idea to solve the problem is to convert all the array element into their binary form and then check if first and last bit of the respective integers are set or not.
Algorithm:
- Initialize a variable count to zero.
- Traverse through each element i of the array from 0 to N-1.
- Convert the current element arr[i] into its binary form using bitwise operators.
- Initialize two variables msb and lsb to zero.
- Traverse through each bit j of the binary representation of the current element arr[i] from 0 to 30.
- If the j-th bit is set and msb is not set, set msb to 1.
- If the j-th bit is set and j is 0 (i.e., LSB), set lsb to 1.
- If both msb and lsb are set, increment count.
- Return count as the answer.
Below is the implementation of the approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countMSBLSBSet( int arr[], int n)
{
int count = 0;
for ( int i = 0; i < n; i++) {
int msb = 0, lsb = 0;
int num = arr[i];
for ( int j = 0; j < 31; j++) {
if ((num & (1 << j)) && !msb) {
msb = 1;
}
if ((num & (1 << j)) && j == 0) {
lsb = 1;
}
}
if (msb && lsb) {
count++;
}
}
return count;
}
int main()
{
int N = 5;
int arr[] = { 1, 2, 3, 7, 8 };
int ans = countMSBLSBSet(arr, N);
cout << ans << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
public static int countMSBLSBSet( int [] arr, int n){
int count = 0 ;
for ( int i = 0 ; i < n; i++) {
int msb = 0 , lsb = 0 ;
int num = arr[i];
for ( int j = 0 ; j < 31 ; j++) {
if ((num & ( 1 << j))!= 0 && msb== 0 ) {
msb = 1 ;
}
if ((num & ( 1 << j))!= 0 && j == 0 ) {
lsb = 1 ;
}
}
if (msb!= 0 && lsb!= 0 ) {
count++;
}
}
return count;
}
public static void main(String[] args) {
int N = 5 ;
int [] arr = { 1 , 2 , 3 , 7 , 8 };
int ans = countMSBLSBSet(arr, N);
System.out.println(ans);
}
}
|
Python3
def countMSBLSBSet(arr, n):
count = 0
for i in range (n):
msb = 0
lsb = 0
num = arr[i]
for j in range ( 31 ):
if (num & ( 1 << j)) and not msb:
msb = 1
if (num & ( 1 << j)) and j = = 0 :
lsb = 1
if msb and lsb:
count + = 1
return count
def main():
N = 5
arr = [ 1 , 2 , 3 , 7 , 8 ]
ans = countMSBLSBSet(arr, N)
print (ans)
if __name__ = = "__main__" :
main()
|
C#
using System;
namespace CountMSBLSBSet
{
class GFG
{
static int CountMSBLSBSet( int [] arr, int n)
{
int count = 0;
for ( int i = 0; i < n; i++)
{
int msb = 0, lsb = 0;
int num = arr[i];
for ( int j = 0; j < 31; j++)
{
if ((num & (1 << j)) != 0 && msb == 0)
{
msb = 1;
}
if ((num & (1 << j)) != 0 && j == 0)
{
lsb = 1;
}
}
if (msb!=0 && lsb!=0)
{
count++;
}
}
return count;
}
static void Main( string [] args)
{
int N = 5;
int [] arr = { 1, 2, 3, 7, 8 };
int ans = CountMSBLSBSet(arr, N);
Console.WriteLine(ans);
}
}
}
|
Javascript
function countMSBLSBSet(arr) {
let count = 0;
for (let i = 0; i < arr.length; i++) {
let msb = 0;
let lsb = 0;
let num = arr[i];
for (let j = 0; j < 31; j++) {
if ((num & (1 << j)) !== 0 && msb === 0) {
msb = 1;
}
if ((num & (1 << j)) !== 0 && j === 0) {
lsb = 1;
}
}
if (msb !== 0 && lsb !== 0) {
count++;
}
}
return count;
}
function main() {
const arr = [1, 2, 3, 7, 8];
const ans = countMSBLSBSet(arr);
console.log(ans);
}
main();
|
Time complexity: O(N * d), where d is the bit count in the maximum element of the array.
Auxiliary Space: O(1)
Efficient Approach: The idea to solve the problem is by traversing the array and counting the number of odd elements present in the array, because all the odd integers have LSB and MSB set.
Follow the steps mentioned below to solve the problem:
- Traverse the array A[] of length and for each element:
- Check, if the element is odd or not.
- If Yes, increase the count by 1
- Return the count.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int count(vector< int > arr, int n)
{
int i, count = 0;
for (i = 0; i < n; i++) {
if (arr[i] % 2)
count++;
}
return count;
}
int main()
{
int N = 5;
vector< int > arr = { 1, 2, 3, 7, 8 };
cout << count(arr, N);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int count( int [] arr, int n)
{
int i, count = 0 ;
for (i = 0 ; i < n; i++) {
if (arr[i] % 2 == 1 )
count++;
}
return count;
}
public static void main (String[] args) {
int N = 5 ;
int arr[] = { 1 , 2 , 3 , 7 , 8 };
System.out.println(count(arr, N));
}
}
|
Python3
def count(arr, n):
i = 0
count = 0
for i in range (n):
if arr[i] % 2 :
count + = 1
return count
N = 5
arr = [ 1 , 2 , 3 , 7 , 8 ]
print (count(arr, N))
|
C#
using System;
class GFG {
static int count( int [] arr, int n)
{
int i, count = 0;
for (i = 0; i < n; i++) {
if (arr[i] % 2 == 1)
count++;
}
return count;
}
public static void Main()
{
int N = 5;
int [] arr = { 1, 2, 3, 7, 8 };
Console.Write(count(arr, N));
}
}
|
Javascript
<script>
function count( arr, n)
{
let i, count = 0;
for (i = 0; i < n; i++) {
if (arr[i] % 2)
count++;
}
return count;
}
let N = 5;
let arr = [ 1, 2, 3, 7, 8 ];
document.write(count(arr, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1).
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
15 Oct, 2023
Like Article
Save Article