📌  相关文章
📜  生成一个数组,该数组具有相同索引元素的按位或的和,且给定数组等于K

📅  最后修改于: 2021-05-19 17:26:23             🧑  作者: Mango

给定由N个整数和整数K组成的数组arr [] ,任务是打印生成的数组,以使生成的数组与给定数组的相同索引元素的按位与之和等于K。无法生成这样的数组,然后打印“ -1”。

例子:

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

  • 首先,计算数组arr []的总和,并将其存储在变量中,例如sum。
  • 初始化一个向量,例如B,以存储结果数组。
  • 将K的值更新为K = K –总和。
  • 如果K小于0 ,则打印-1。
  • 遍历数组arr []并执行以下操作:
    • 初始化一个变量,例如curr,以存储结果数组的元素。
    • 遍历当前数组元素的位。
    • 检查当前位是否设置为2 j≤K 。如果发现为真,则将curr更新为curr = curr | (1 << j)K = K –(1 << j)。
    • 现在,将curr推入向量B。
  • 如果K0,则打印向量B。否则,打印-1。

下面是上述方法的实现:

C++14
// C++ program for the above approach
#include 
using namespace std;
 
// Function to print the resultant array
void constructArr(int A[], int N, int K)
{
    // Stores the sum of the array
    int sum = 0;
 
    // Calculate sum of the array
    for (int i = 0; i < N; i++) {
        sum += A[i];
    }
 
    // If sum > K
    if (sum > K) {
 
        // Not possible to
        // construct the array
        cout << -1 << "\n";
        return;
    }
 
    // Update K
    K -= sum;
 
    // Stores the resultant array
    vector B;
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
 
        // Stores the current element
        int curr = A[i];
 
        for (int j = 32; j >= 0 and K; j--) {
 
            // If jth bit is not set and
            // value of 2^j is less than K
            if ((curr & (1LL << j)) == 0
                and (1LL << j) <= K) {
 
                // Update curr
                curr |= (1LL << j);
 
                // Update K
                K -= (1LL << j);
            }
        }
 
        // Push curr into B
        B.push_back(curr);
    }
 
    // If K is greater than 0
    if (!K) {
 
        // Print the vector B
        for (auto it : B) {
            cout << it << " ";
        }
        cout << "\n";
    }
 
    // Otherwise
    else {
        cout << "-1"
             << "\n";
    }
}
 
// Driver Code
int main()
{
    // Input
    int arr[] = { 1, 2, 3, 4 };
 
    // Size of the array
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Given input
    int K = 32;
 
    // Function call to print
    // the required array
    constructArr(arr, N, K);
}


Python3
# Python 3 program for the above approach
 
# Function to print the resultant array
def constructArr(A, N, K):
   
    # Stores the sum of the array
    sum = 0
 
    # Calculate sum of the array
    for i in range(N):
        sum += A[i]
 
    # If sum > K
    if (sum > K):
       
        # Not possible to
        # construct the array
        print(-1)
        return
 
    # Update K
    K -= sum
 
    # Stores the resultant array
    B = []
 
    # Traverse the array
    for i in range(N):
       
        # Stores the current element
        curr = A[i]
        j = 32
 
        while(j >= 0 and K>0):
           
            # If jth bit is not set and
            # value of 2^j is less than K
            if ((curr & (1 << j)) == 0 and (1 << j) <= K):
               
                # Update curr
                curr |= (1 << j)
 
                # Update K
                K -= (1 << j)
            j -= 1
 
        # Push curr into B
        B.append(curr)
 
    # If K is greater than 0
    if (K == 0):
        # Print the vector B
        for it in B:
            print(it,end=" ")
        print("\n",end = "")
 
    # Otherwise
    else:
        print("-1")
 
# Driver Code
if __name__ == '__main__':
    # Input
    arr = [1, 2, 3, 4]
     
    # Size of the array
    N = len(arr)
     
    # Given input
    K = 32
     
    # Function call to print
    # the required array
    constructArr(arr, N, K)
 
# This code is contributed by ipg2016107.


输出:
23 2 3 4

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