Print the array after K operations

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.



  1. 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.
  2. After this first step, the array becomes stagnant and the values alternatively change from its value A[i] to M – A[i] alternatively.
  3. 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.

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.