📜  从N个自然数中分离出奇数和奇数后,计数第K个数字中的设置位

📅  最后修改于: 2021-04-22 02:24:52             🧑  作者: Mango

给定两个整数NK ,任务是在由[1,N]范围内的数字构成的奇偶序列中找到第K数字中的置位数。奇偶序列第一包含的所有奇数从1N,然后将所有的偶数从1N。
例子:

方法:本文讨论了一种找到所需序列的第K个元素的方法。因此,找到所需的数字,然后使用__builtin_popcount()查找其中的设置位数。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the kth element
// of the Odd-Even sequence
// of length n
int findK(int n, int k)
{
    int pos;
 
    // Finding the index from where the
    // even numbers will be stored
    if (n % 2 == 0) {
        pos = n / 2;
    }
    else {
        pos = (n / 2) + 1;
    }
 
    // Return the kth element
    if (k <= pos) {
        return (k * 2 - 1);
    }
    else
 
        return ((k - pos) * 2);
}
 
// Function to return the count of
// set bits in the kth number of the
// odd even sequence of length n
int countSetBits(int n, int k)
{
 
    // Required kth number
    int kth = findK(n, k);
 
    // Return the count of set bits
    return __builtin_popcount(kth);
}
 
// Driver code
int main()
{
    int n = 18, k = 12;
 
    cout << countSetBits(n, k);
 
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
     
    // Function to return the kth element
    // of the Odd-Even sequence
    // of length n
    static int findK(int n, int k)
    {
        int pos;
     
        // Finding the index from where the
        // even numbers will be stored
        if (n % 2 == 0)
        {
            pos = n / 2;
        }
        else
        {
            pos = (n / 2) + 1;
        }
     
        // Return the kth element
        if (k <= pos)
        {
            return (k * 2 - 1);
        }
        else
            return ((k - pos) * 2);
    }
     
    // Function to return the count of
    // set bits in the kth number of the
    // odd even sequence of length n
    static int countSetBits(int n, int k)
    {
     
        // Required kth number
        int kth = findK(n, k);
         
        int count = 0;
         
        while (kth > 0)
        {
            count += kth & 1;
            kth >>= 1;
        }
         
        // Return the count of set bits
        return count;
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int n = 18, k = 12;
     
        System.out.println(countSetBits(n, k));
    }
}
 
// This code is contributed by AnkitRai01


Python3
# Python3 implementation of the approach
 
# Function to return the kth element
# of the Odd-Even sequence
# of length n
def findK(n, k) :
     
    # Finding the index from where the
    # even numbers will be stored
    if (n % 2 == 0) :
        pos = n // 2;
    else :
        pos = (n // 2) + 1;
 
    # Return the kth element
    if (k <= pos) :
        return (k * 2 - 1);
    else :
        return ((k - pos) * 2);
 
# Function to return the count of
# set bits in the kth number of the
# odd even sequence of length n
def countSetBits( n, k) :
     
    # Required kth number
    kth = findK(n, k);
     
    # Return the count of set bits
    return bin(kth).count('1');
 
# Driver code
if __name__ == "__main__" :
    n = 18; k = 12;
    print(countSetBits(n, k));
 
# This code is contributed by kanugargng


C#
// C# implementation of the above approach
using System;
 
class GFG
{
     
    // Function to return the kth element
    // of the Odd-Even sequence
    // of length n
    static int findK(int n, int k)
    {
        int pos;
     
        // Finding the index from where the
        // even numbers will be stored
        if (n % 2 == 0)
        {
            pos = n / 2;
        }
        else
        {
            pos = (n / 2) + 1;
        }
     
        // Return the kth element
        if (k <= pos)
        {
            return (k * 2 - 1);
        }
        else
            return ((k - pos) * 2);
    }
     
    // Function to return the count of
    // set bits in the kth number of the
    // odd even sequence of length n
    static int countSetBits(int n, int k)
    {
     
        // Required kth number
        int kth = findK(n, k);
         
        int count = 0;
         
        while (kth > 0)
        {
            count += kth & 1;
            kth >>= 1;
        }
         
        // Return the count of set bits
        return count;
    }
     
    // Driver code
    public static void Main (String[] args)
    {
        int n = 18, k = 12;
     
        Console.WriteLine(countSetBits(n, k));
    }
}
 
// This code is contributed by PrinciRaj1992


Javascript


输出:
2