Given an array **arr[]** of size **N** and a number **K**. The task is to print the array **arr[]** after **K** operations such that at every operation, replace every element **arr[i]** in the array with **max – arr[i]** where max is the maximum element in the array.

**Examples :**

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

**Approach:** The idea is to clearly observe the array after every step.

- Initially, since we are subtracting the maximum element from the array, we can be sure that there will be at least one element in the array with zero value. This happens after the first step that is
**K = 1**. Let the array after this step be**A[]**with maximum element**M**. - After this first step, the array becomes stagnant and the values alternatively change from its value
**A[i]**to**M – A[i]**alternatively. - For example, let us take the array
**arr[]**= {4, 8, 12, 16}. In this array, the maximum value is 16 and let us assume**K**= 4.- At the first step, (i.e.) for
**K = 1**, the array is reduced to**{12, 8, 4, 0}**. That is, every element arr[i] is replaced with 16 – arr[i]. Let this array be A[] and the maximum element in this array M is 12. - After the first step, every element in this array
**A[i]**alternatively change between**A[i]**and**12 – A[i]**. That is, for**K = 2**, the array now becomes**{0, 4, 8, 12}**. - Similarly, for the third step, (i.e.)
**K = 3**, the array again becomes**{12, 8, 4, 0}**which is the same as for**K = 1**. - And for the fourth step, (i.e.)
**K = 4**, the array becomes**{0, 4, 8, 12}**which is the same as for**K = 2**and so on.

- At the first step, (i.e.) for

Therefore, it can be concluded that we only need to check if K is odd or even:

- If K is
**odd**: replace every element**arr[i]**with**arr[i] – min**where min is the minimum element in the array. - If K is
**even**: replace every element**arr[i]**with**max – arr[i]**where max is the maximum element in the array.

Below is the implementation of the above approach:

## CPP

`// C++ program to print the array ` `// after K operations ` `#include <bits/stdc++.h> ` `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; ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

**Output:**

0 4 8 12

**Time Complexity:** O(N) where N is the size of the array.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Print modified array after multiple array range increment operations
- Find array sum using Bitwise OR after splitting given array in two halves after K circular shifts
- Maximum value in an array after m range increment operations
- Binary array after M range toggle operations
- Maximum Possible Product in Array after performing given Operations
- Minimum element left from the array after performing given operations
- Maximum count of equal numbers in an array after performing given operations
- Online Queries for GCD of array after divide operations
- Maximum sum of all elements of array after performing given operations
- Number of array elements derivable from D after performing certain operations
- Find the modified array after performing k operations of given type
- Find maximum value of the last element after reducing the array with given operations
- Number of elements from the array which are reachable after performing given operations on D
- Find the final sequence of the array after performing given operations
- Maximum number of unique values in the array after performing given operations
- Maximum score possible after performing given operations on an Array
- Maximum possible Array sum after performing given operations
- Maximum count of values of S modulo M lying in a range [L, R] after performing given operations on the array
- Find the weight at (Xi, Yi) after M operations in a Matrix
- Maximum possible sum after M operations on N cards

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.