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:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Check if a number has an odd count of odd divisors and even count of even divisors
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Even numbers at even index and odd numbers at odd index
- Segregating negative and positive maintaining order and O(1) space
- Sum of cubes of even and odd natural numbers
- Count of even and odd set bit with array element after XOR with K
- Count of even and odd set bit Array elements after XOR with K for Q queries
- Sum of range in a series of first odd then even natural numbers
- Find Kth element in an array containing odd elements first and then even elements
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
- Query to count odd and even parity elements in subarray after XOR with K
- Count total number of N digit numbers such that the difference between sum of even and odd digits is 1
- Number of odd and even results for every value of x in range [min, max] after performing N steps
- Generate an array of given size with equal count and sum of odd and even numbers
- Find kth smallest number in range [1, n] when all the odd numbers are deleted
- Nth term where K+1th term is product of Kth term with difference of max and min digit of Kth term
- K-th smallest element after removing given integers from natural numbers | Set 2
- Sort all even numbers in ascending order and then sort all odd numbers in descending order
- Rotate all odd numbers right and all even numbers left in an Array of 1 to N
- Count Odd and Even numbers in a range from L to R
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.