从所有元素中减去最大值 K 次后找到最终数组
给定一个包含N个整数的数组arr[]和一个整数K,任务是对该数组执行以下操作K次。操作如下:
- 从数组中找到最大元素(比如M )。
- 现在用Ma i替换每个元素。其中 1 ≤ i ≤ N。
例子:
Input: N = 6, K = 3, arr[] = {5, 38, 4, 96, 103, 41}
Output: 98 65 99 7 0 62
Explanation:
1st operation => Maximum array element is 103. Subtract 103 from all elements. arr[] = {98, 65, 99, 7, 0, 62}.
1st operation => Maximum array element is 99. Subtract 99 from all elements. arr[] = {1, 34, 0, 92, 99, 37}
1st operation => Maximum array element is 99. Subtract 99 from all elements. arr[] = {98, 65, 99, 7, 0, 62}.
This will the last state.
Input: N = 5, K = 1, arr[] = {8, 4, 3, 10, 15}
Output: 7 11 12 5 0
Naive Approach:简单的方法是将上述操作执行K次。每次在数组中找到最大元素,然后更新与最大值不同的所有元素。
时间复杂度: O(N*K)。
辅助空间: O(1)。
有效方法:此问题的有效解决方案基于以下观察:
If K is odd then the final answer will be equivalent to the answer after applying the operation only one time and if K is even then the final array will be equivalent to the array after applying operations only two time.
This can be proved by as per the following:
Say maximum = M, and initial array is arr[].
After the operations are performed 1st time:
=> The maximum element of arr[] becomes 0.
=> All other elements are M – arr[i].
=> Say the new array is a1[]. So, a1[i] = M – arr[i].
After the operation are performed 2nd time:
=> Say maximum of a1[] is M1.
=> The maximum element of a1[] becomes 0.
=> All other elements are M1 – a1[i].
=> Say the new array is a2[]. So, a2[i] = M1 – a1[i].
=> Maximum of a2[] will be M1, because the 0 present in a1[] changes to M1 and all other elements are less than M1.
After the operation are performed 3rd time:
=> The maximum is M1.
=> The maximum changes to 0.
=> All other elements are M1 – a2[i] = M1 – (M1 – a1[i]) = a1[i].
=> So the new array is same as a1[].
After the operation are performed 4th time:
=> Say maximum of the array is M1.
=> The maximum element changes to be 0.
=> All other elements are M1 – a1[i].
=> So the new array is the same as a2[]
From the above it is clear that the alternate states of the array are same.
按照以下步骤实施观察:
- 取一个变量max并存储arr的最大元素。
- 如果K是奇数
- 遍历数组并从最大元素中减去每个元素。
- 别的
- 遍历arr并从最大元素中减去每个元素,然后
将最大元素存储在max1变量中。 - 再次遍历arr并从最大元素中减去每个元素。
- 遍历arr并从最大元素中减去每个元素,然后
- 打印最终数组。
下面是上述方法的实现:
C++
// C++ code for the approach
#include
using namespace std;
// Function for converting the array
void find(int n, int k, int arr[])
{
// Find the maximum element in array
int max = INT_MIN;
for (int i = 0; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
// If k is odd
if (k % 2 != 0) {
for (int i = 0; i < n; i++) {
cout << max - arr[i] << " ";
}
}
// If k is even
else {
// Subtract the max from every
// element of array and store
// the next maximum element in max1
int max1 = INT_MIN;
for (int i = 0; i < n; i++) {
arr[i] = max - arr[i];
if (arr[i] > max1) {
max1 = arr[i];
}
}
// Print the output
for (int i = 0; i < n; i++) {
cout << max1 - arr[i] << " ";
}
}
}
// Driver code
int main()
{
int N = 6, K = 3;
int arr[] = { 5, 38, 4, 96, 103, 41 };
// Function call
find(N, K, arr);
return 0;
}
98 65 99 7 0 62
时间复杂度: O(N)
辅助空间: O(1)