📌  相关文章
📜  计算所有相邻元素的按位XOR设置的位,最多N

📅  最后修改于: 2021-05-25 01:35:23             🧑  作者: Mango

给定正整数N ,任务是通过对[0,N]范围内的所有可能相邻元素执行按位XOR运算,以找到设置位的总数。

例子:

简单方法:解决这个问题的最简单的方法是迭代范围[0,N]和通过在范围[0,N]每个相邻元件上执行位异或计算所有可能的组的比特。最后,打印所有可能的设置位的总数。

时间复杂度: O(N * log 2 N)
辅助空间: O(1)

高效的方法:为了优化上述方法,该想法是基于以下观察结果:

请按照以下步骤解决问题:

  • 初始化一个变量,例如bit_position,以存储范围[0,N]中最右边的所有可能值。
  • 通过对范围[0,N]上所有可能的相邻元素执行按位XOR操作,初始化一个变量,例如total_set_bits,以存储所有可能的设置位的总和。
  • 迭代范围[0,N]并更新N = N –(N +1)/ 2total_set_bits + =((N + 1)/ 2 * bit_position)
  • 最后,打印total_set_bits的值。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Funtion to count of set bits in Bitwise
// XOR of adjacent elements up to N
int countXORSetBitsAdjElemRange1_N(int N)
{
 
    // Stores count of set bits by Bitwise
    // XOR on adjacent elements of [0, N]
    int total_set_bits = 0;
 
    // Stores all possible values on
    // right most set bit over [0, N]
    int bit_Position = 1;
 
    // Iterate over the range [0, N]
    while (N) {
        total_set_bits += ((N + 1) / 2
                           * bit_Position);
 
        // Update N
        N -= (N + 1) / 2;
 
        // Update bit_Position
        bit_Position++;
    }
 
    return total_set_bits;
}
 
// Driver Code
int main()
{
    int N = 4;
 
    cout << countXORSetBitsAdjElemRange1_N(N);
 
    return 0;
}


Java
// Java program to implement
// the above approach
import java.io.*;
import java.util.*;
   
class GFG{
   
// Funtion to count of set bits in Bitwise
// XOR of adjacent elements up to N
static int countXORSetBitsAdjElemRange1_N(int N)
{
     
    // Stores count of set bits by Bitwise
    // XOR on adjacent elements of [0, N]
    int total_set_bits = 0;
  
    // Stores all possible values on
    // right most set bit over [0, N]
    int bit_Position = 1;
  
    // Iterate over the range [0, N]
    while (N != 0)
    {
        total_set_bits += ((N + 1) / 2 *
                           bit_Position);
  
        // Update N
        N -= (N + 1) / 2;
  
        // Update bit_Position
        bit_Position++;
    }
    return total_set_bits;
}
   
// Driver Code
public static void main(String[] args)
{
    int N = 4;
  
    System.out.println(countXORSetBitsAdjElemRange1_N(N));
}
}
   
// This code is contributed by susmitakundugoaldanga


Python3
# Python3 program to implement
# the above approach
 
# Funtion to count of set bits in Bitwise
# XOR of adjacent elements up to N
def countXORSetBitsAdjElemRange1_N(N):
     
    # Stores count of set bits by Bitwise
    # XOR on adjacent elements of [0, N]
    total_set_bits = 0
 
    # Stores all possible values on
    # right most set bit over [0, N]
    bit_Position = 1
 
    # Iterate over the range [0, N]
    while (N):
        total_set_bits += ((N + 1) //
                            2 * bit_Position)
                             
        # Update N
        N -= (N + 1) // 2
 
        # Update bit_Position
        bit_Position += 1
 
    return total_set_bits
 
# Driver Code
if __name__ == '__main__':
     
    N = 4
     
    print(countXORSetBitsAdjElemRange1_N(N))
 
# This code is contributed by SURENDRA_GANGWAR


C#
// C# program to implement
// the above approach 
using System;
    
class GFG{
     
// Funtion to count of set bits in Bitwise
// XOR of adjacent elements up to N
static int countXORSetBitsAdjElemRange1_N(int N)
{
     
    // Stores count of set bits by Bitwise
    // XOR on adjacent elements of [0, N]
    int total_set_bits = 0;
   
    // Stores all possible values on
    // right most set bit over [0, N]
    int bit_Position = 1;
   
    // Iterate over the range [0, N]
    while (N != 0)
    {
        total_set_bits += ((N + 1) / 2 *
                           bit_Position);
   
        // Update N
        N -= (N + 1) / 2;
   
        // Update bit_Position
        bit_Position++;
    }
    return total_set_bits;
}
  
// Driver Code
public static void Main()
{
    int N = 4;
   
    Console.Write(countXORSetBitsAdjElemRange1_N(N));
}
}
 
// This code is contributed by code_hunt


输出:
7











时间复杂度: O(Log 2 N)
辅助空间: O(1)