Given two integers N and K, the task is to find the count of set bits in the Kth number in the Odd-Even sequence made of the number from the range [1, N]. The Odd-Even sequence first contains all the odd numbers from 1 to N and then all the even numbers from 1 to N.
Input: N = 8, K = 4
The sequence is 1, 3, 5, 7, 2, 4, 6 and 8.
4th element is 7 and the count
of set bits in it is 3.
Input: N = 18, K = 12
Approach: An approach to find the Kth element of the required sequence has been discussed in this article. So, find the required number and then use __builtin_popcount() to find the count of set bits in it.
Below is the implementation of the above approach:
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Count pairs of natural numbers with GCD equal to given number
- Count natural numbers whose factorials are divisible by x but not y
- Find the count of natural Hexadecimal numbers of size N
- Count non decreasing subarrays of size N from N Natural numbers
- Count of numbers whose 0th and Nth bits are set
- Count total set bits in all numbers from 1 to n | Set 2
- Count total set bits in all numbers from 1 to N | Set 3
- Count total unset bits in all the numbers from 1 to N
- Find if given number is sum of first n natural numbers
- Number of pairs from the first N natural numbers whose sum is divisible by K
- Number of valid indices in the permutation of first N natural numbers
- Sum of first N natural numbers by taking powers of 2 as negative number
- Number of distinct prime factors of first n natural numbers
- Find the number of sub arrays in the permutation of first N natural numbers such that their median is M
- Count pairs (A, B) such that A has X and B has Y number of set bits and A+B = C
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.