给定一个由N 个整数和一个整数K组成的数组 arr[] ,任务是打印一个生成的数组,使得生成的数组的相同索引元素与给定数组的按位或之和等于K。如果是不可能生成这样的数组,然后打印“-1”。
例子:
Input: arr[] = {6, 6, 6, 6}, K = 34
Output: 15 7 6 6
Explanation:
Bitwise XOR of same-indexed elements of the arrays {6, 6, 6, 6} and {15, 7, 6, 6} are:
6|15 = 15
6|7 = 7
6|6 = 6
6|6 = 6
Sum of Bitwise ORs = 15 + 7 + 6 + 6 = 34 (= K)
Input: arr[] = {1, 2, 3, 4}, K = 32
Output: 23 2 3 4
处理方法:按照以下步骤解决问题:
- 首先,计算数组arr[]的总和并将其存储在一个变量中,比如sum。
- 初始化一个 vector
,比如B,以存储结果数组。 - 将 K的值更新为K = K – sum。
- 如果K小于0 ,则打印-1。
- 遍历数组arr[]并执行以下操作:
- 初始化一个变量,比如curr,以存储结果数组的元素。
- 遍历当前数组元素的位。
- 检查当前位是否已设置且2 j ≤ K。如果发现为真,则将curr更新为curr = curr | (1<
和K = K – (1 << j)。 - 现在,将curr推入向量B。
- 如果K为0 ,则打印向量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)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live