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.
- Count pairs of natural numbers with GCD equal to given number
- Count of pairs with sum N from first N natural numbers
- Count natural numbers whose factorials are divisible by x but not y
- Count of natural numbers in range [L, R] which are relatively prime with N
- Count of numbers whose 0th and Nth bits are set
- Find the count of natural Hexadecimal numbers of size N
- Count non decreasing subarrays of size N from N Natural numbers
- Count total set bits in all numbers from 1 to N | Set 3
- Count total set bits in all numbers from 1 to n | Set 2
- 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 Set-bits of number using Recursion
- Count pairs (A, B) such that A has X and B has Y number of set bits and A+B = C
- Count of pairs in an Array with same number of set bits
- Count pairs of elements such that number of set bits in their AND is B[i]
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.