给定大小为N且数字为K的数组arr [] 。任务是在K次操作后打印数组arr [] ,以便在每次操作时,将数组中的每个元素arr [i]替换为max – arr [i] ,其中max是数组中的最大元素。
例子 :
Input: arr[] = {4, 8, 12, 16}, K = 4
Output: 0 4 8 12
Explanation:
For the given array arr[] = { 4, 8, 12, 16 }. The array is changed as follows for every operation K:
{ 12, 8, 4, 0 } – K = 1
{ 0, 4, 8, 12 } – K = 2
{ 12, 8, 4, 0 } – K = 3
{ 0, 4, 8, 12 } – K = 4
Input: arr[] = {8, 0, 3, 5}, K = 3
Output: 0 8 5 3
Explanation:
For the given array arr[] = { 8, 0, 3, 5 }. The array is changed as follows for every operation K:
{ 0, 8, 5, 3 } – K = 1
{ 8, 0, 3, 5 } – K = 2
{ 0, 8, 5, 3 } – K = 3
方法:想法是在每个步骤之后清楚地观察阵列。
- 最初,由于我们要从数组中减去最大元素,因此可以确定数组中至少有一个元素的值为零。这是在第一步K = 1之后发生的。令此步骤之后的数组为A [] ,元素最大为M。
- 第一步之后,数组停滞不前,其值也从其值A [i]更改为M – A [i] 。
- 例如,让我们采用数组arr [] = { 4,8,12,16 }。在此数组中,最大值为16,让我们假设K = 4。
- 在第一步(即,对于K = 1) ,将数组简化为{12,8,4,0} 。也就是说,将每个元素arr [i]替换为16 – arr [i]。假设此数组为A [],并且数组M中的最大元素为12。
- 第一步之后,此数组A [i]中的每个元素都会在A [i]和12 – A [i]之间交替变化。也就是说,对于K = 2 ,数组现在变为{0,4,8,12} 。
- 类似地,对于第三步(即K = 3) ,该数组再次变为{12,8,4,0} ,与K = 1相同。
- 而对于第四步,(即) K = 4 ,该数组变为{0,4,8,12} ,与K = 2相同,依此类推。
因此,可以得出结论,我们只需要检查K是否为奇数或偶数:
- 如果K为奇数:用arr [i]-min替换每个元素arr [i ]-其中min是数组中的最小元素。
- 如果K为偶数:将每个元素arr [i]替换为max – arr [i] ,其中max是数组中的最大元素。
下面是上述方法的实现:
CPP
// C++ program to print the array
// after K operations
#include
using namespace std;
// Function to print the array
// after K operations
void printArray(int A[], int n, int K)
{
// Variables to store the minimum and
// the maximum elements of the array
int minEle = INT_MAX,
maxEle = INT_MIN;
// Loop to find the minimum and the
// maximum elements of the array
for (int i = 0; i < n; i++) {
minEle = min(minEle, A[i]);
maxEle = max(maxEle, A[i]);
}
// If K is not equal to 0
if (K != 0) {
// If K is odd
if (K % 2 == 1) {
// Replace every element with
// max - arr[i]
for (int i = 0; i < n; i++)
A[i] = maxEle - A[i];
}
// If K is even
else {
// Replace every element with
// A[i] - min
for (int i = 0; i < n; i++)
A[i] = A[i] - minEle;
}
}
// Printing the array after K operations
for (int i = 0; i < n; i++)
cout << A[i] << " ";
}
// Driver code
int main()
{
int arr[] = { 4, 8, 12, 16 };
int K = 4;
int N = sizeof(arr) / sizeof(arr[0]);
printArray(arr, N, K);
return 0;
}
Java
// Java program to print the array
// after K operations
import java.io.*;
class GFG {
// Function to print the array
// after K operations
static void printArray(int[] A, int n, int K)
{
// Variables to store the minimum and
// the maximum elements of the array
int minEle = Integer.MAX_VALUE,
maxEle = Integer.MAX_VALUE;
// Loop to find the minimum and the
// maximum elements of the array
for (int i = 0; i < n; i++) {
minEle = Math.min(minEle, A[i]);
maxEle = Math.max(maxEle, A[i]);
}
// If K is not equal to 0
if (K != 0) {
// If K is odd
if (K % 2 == 1) {
// Replace every element with
// max - arr[i]
for (int i = 0; i < n; i++)
A[i] = maxEle - A[i];
}
// If K is even
else {
// Replace every element with
// A[i] - min
for (int i = 0; i < n; i++)
A[i] = A[i] - minEle;
}
}
// Printing the array after K operations
for (int i = 0; i < n; i++)
System.out.print(A[i] + " ");
}
// Driver Code
public static void main (String[] args)
{
int[] arr = { 4, 8, 12, 16 };
int K = 4;
int N = arr.length;
printArray(arr, N, K);
}
}
// This code is contributed by shivanisinghss2110
Python3
# Python3 program to print the array
# after K operations
# Function to print the array
# after K operations
def printArray(A, n, K):
# Variables to store the minimum and
# the maximum elements of the array
minEle = 10**9
maxEle = -10**9
# Loop to find the minimum and the
# maximum elements of the array
for i in range(n):
minEle = min(minEle, A[i])
maxEle = max(maxEle, A[i])
# If K is not equal to 0
if (K != 0):
# If K is odd
if (K % 2 == 1):
# Replace every element with
# max - arr[i]
for i in range(n):
A[i] = maxEle - A[i]
# If K is even
else:
# Replace every element with
# A[i] - min
for i in range(n):
A[i] = A[i] - minEle
# Printing the array after K operations
for i in A:
print(i, end=" ")
# Driver code
if __name__ == '__main__':
arr=[4, 8, 12, 16]
K = 4
N = len(arr)
printArray(arr, N, K)
# This code is contributed by mohit kumar 29
C#
// C# program to print the array
// after K operations
using System;
class GFG{
// Function to print the array
// after K operations
static void printArray(int[] A, int n, int K)
{
// Variables to store the minimum and
// the maximum elements of the array
int minEle = Int32.MaxValue,
maxEle = Int32.MinValue;
// Loop to find the minimum and the
// maximum elements of the array
for (int i = 0; i < n; i++) {
minEle = Math.Min(minEle, A[i]);
maxEle = Math.Max(maxEle, A[i]);
}
// If K is not equal to 0
if (K != 0) {
// If K is odd
if (K % 2 == 1) {
// Replace every element with
// max - arr[i]
for (int i = 0; i < n; i++)
A[i] = maxEle - A[i];
}
// If K is even
else {
// Replace every element with
// A[i] - min
for (int i = 0; i < n; i++)
A[i] = A[i] - minEle;
}
}
// Printing the array after K operations
for (int i = 0; i < n; i++)
Console.Write(A[i] + " ");
}
// Driver code
static public void Main ()
{
int[] arr = { 4, 8, 12, 16 };
int K = 4;
int N = arr.Length;
printArray(arr, N, K);
}
}
// This code is contributed by shubhamsingh10
Javascript
0 4 8 12
时间复杂度: O(N),其中N是数组的大小。