# Count of Integers in given Array whose MSB and LSB are set

• Difficulty Level : Medium
• Last Updated : 28 Mar, 2022

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.

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

 ``

Output

`3`

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

My Personal Notes arrow_drop_up
Related Articles