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++
// C++ code for the approach #include <bits/stdc++.h> using namespace std; // Function to count the number of elements with MSB and LSB // set int countMSBLSBSet( int arr[], int n) { // Initialize count to zero int count = 0; for ( int i = 0; i < n; i++) { int msb = 0, lsb = 0; int num = arr[i]; // Check if MSB and LSB of the current element are // set or not for ( int j = 0; j < 31; j++) { // if jth bit is set and msb is not set earlier // then set it if ((num & (1 << j)) && !msb) { msb = 1; } // if jth bit set and j is 0th one then lsb is // set if ((num & (1 << j)) && j == 0) { lsb = 1; } } // If both MSB and LSB are set, increment count if (msb && lsb) { count++; } } return count; } // Driver's code int main() { // Input int N = 5; int arr[] = { 1, 2, 3, 7, 8 }; // Function Call int ans = countMSBLSBSet(arr, N); cout << ans << endl; return 0; } |
Java
//Java code for the approach import java.io.*; class GFG { // Function to count the number of elements with MSB and LSB set public static int countMSBLSBSet( int [] arr, int n){ // Initialize count to zero int count = 0 ; for ( int i = 0 ; i < n; i++) { int msb = 0 , lsb = 0 ; int num = arr[i]; // Check if MSB and LSB of the current element are set or not for ( int j = 0 ; j < 31 ; j++) { // if jth bit is set and msb is not set earlier then set it if ((num & ( 1 << j))!= 0 && msb== 0 ) { msb = 1 ; } // if jth bit set and j is 0th one then lsb is set if ((num & ( 1 << j))!= 0 && j == 0 ) { lsb = 1 ; } } //If both MSB and LSB are set, increment count if (msb!= 0 && lsb!= 0 ) { count++; } } return count; } // Driver's code public static void main(String[] args) { // Input int N = 5 ; int [] arr = { 1 , 2 , 3 , 7 , 8 }; // Function Call int ans = countMSBLSBSet(arr, N); System.out.println(ans); } } |
Python3
def countMSBLSBSet(arr, n): # Initialize count to zero count = 0 for i in range (n): msb = 0 lsb = 0 num = arr[i] # Check if MSB and LSB of the current element are set or not for j in range ( 31 ): # if jth bit is set and msb is not set earlier, then set it if (num & ( 1 << j)) and not msb: msb = 1 # if jth bit is set and j is 0th one then lsb is set if (num & ( 1 << j)) and j = = 0 : lsb = 1 # If both MSB and LSB are set, increment count if msb and lsb: count + = 1 return count # Driver's code def main(): # Input N = 5 arr = [ 1 , 2 , 3 , 7 , 8 ] # Function Call ans = countMSBLSBSet(arr, N) print (ans) if __name__ = = "__main__" : main() |
C#
using System; namespace CountMSBLSBSet { class GFG { // Function to count the number of elements with MSB and LSB set static int CountMSBLSBSet( int [] arr, int n) { // Initialize count to zero int count = 0; for ( int i = 0; i < n; i++) { int msb = 0, lsb = 0; int num = arr[i]; // Check if MSB and LSB of the current element are set or not for ( int j = 0; j < 31; j++) { // If jth bit is set and msb is not set earlier, then set it if ((num & (1 << j)) != 0 && msb == 0) { msb = 1; } // If jth bit set and j is 0th one, then lsb is set if ((num & (1 << j)) != 0 && j == 0) { lsb = 1; } } // If both MSB and LSB are set, increment count if (msb!=0 && lsb!=0) { count++; } } return count; } // Driver's code static void Main( string [] args) { // Input int N = 5; int [] arr = { 1, 2, 3, 7, 8 }; // Function Call int ans = CountMSBLSBSet(arr, N); Console.WriteLine(ans); } } } |
Javascript
// Function to count the number of elements with MSB and LSB set function countMSBLSBSet(arr) { // Initialize count to zero let count = 0; // Iterate through the array for (let i = 0; i < arr.length; i++) { let msb = 0; let lsb = 0; let num = arr[i]; // Check if MSB and LSB of the current element are set or not for (let j = 0; j < 31; j++) { // If jth bit is set and msb is not set earlier, then set it if ((num & (1 << j)) !== 0 && msb === 0) { msb = 1; } // If jth bit set and j is 0th one, then lsb is set if ((num & (1 << j)) !== 0 && j === 0) { lsb = 1; } } // If both MSB and LSB are set, increment count if (msb !== 0 && lsb !== 0) { count++; } } return count; } // Main function function main() { // Input const arr = [1, 2, 3, 7, 8]; // Function Call const ans = countMSBLSBSet(arr); console.log(ans); } // Call the main function main(); |
3
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++
// C++ code for the above approach: #include <bits/stdc++.h> using namespace std; // Count the number of odd elements int count(vector< int > arr, int n) { int i, count = 0; for (i = 0; i < n; i++) { // If element is odd // increment count if (arr[i] % 2) count++; } return count; } // Driver Code int main() { int N = 5; vector< int > arr = { 1, 2, 3, 7, 8 }; cout << count(arr, N); return 0; } |
Java
// Java code for the above approach: import java.io.*; class GFG { // Count the number of odd elements static int count( int [] arr, int n) { int i, count = 0 ; for (i = 0 ; i < n; i++) { // If element is odd // increment count if (arr[i] % 2 == 1 ) count++; } return count; } // Driver Code public static void main (String[] args) { int N = 5 ; int arr[] = { 1 , 2 , 3 , 7 , 8 }; System.out.println(count(arr, N)); } } // This code is contributed by hrithikgarg03188. |
Python3
# Python3 code for the above approach # count the number of odd elements def count(arr, n): i = 0 count = 0 for i in range (n): if arr[i] % 2 : count + = 1 return count # Driver Code N = 5 arr = [ 1 , 2 , 3 , 7 , 8 ] print (count(arr, N)) # This code is contributed by phasing17 |
C#
// C# code for the above approach: using System; class GFG { // Count the number of odd elements static int count( int [] arr, int n) { int i, count = 0; for (i = 0; i < n; i++) { // If element is odd // increment count if (arr[i] % 2 == 1) count++; } return count; } // Driver Code public static void Main() { int N = 5; int [] arr = { 1, 2, 3, 7, 8 }; Console.Write(count(arr, N)); } } // This code is contributed by Samim Hossain Mondal. |
Javascript
<script> // JavaScript code for the above approach // Count the number of odd elements function count( arr, n) { let i, count = 0; for (i = 0; i < n; i++) { // If element is odd // increment count if (arr[i] % 2) count++; } return count; } // Driver Code let N = 5; let arr = [ 1, 2, 3, 7, 8 ]; document.write(count(arr, N)); // This code is contributed by Potta Lokesh </script> |
3
Time Complexity: O(N)
Auxiliary Space: O(1).
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!