Print the array after K operations
Last Updated :
11 Mar, 2023
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.
Therefore, it can be concluded that we only need to check if K is odd or even:
- If K is even: replace every element arr[i] with arr[i] – min where min is the minimum element in the array.
- If K is odd: 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
#include <bits/stdc++.h>
using namespace std;
void printArray( int A[], int n, int K)
{
int minEle = INT_MAX,
maxEle = INT_MIN;
for ( int i = 0; i < n; i++) {
minEle = min(minEle, A[i]);
maxEle = max(maxEle, A[i]);
}
if (K != 0) {
if (K % 2 == 1) {
for ( int i = 0; i < n; i++)
A[i] = maxEle - A[i];
}
else {
for ( int i = 0; i < n; i++)
A[i] = A[i] - minEle;
}
}
for ( int i = 0; i < n; i++)
cout << A[i] << " " ;
}
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
import java.io.*;
class GFG {
static void printArray( int [] A, int n, int K)
{
int minEle = Integer.MAX_VALUE,
maxEle = Integer.MAX_VALUE;
for ( int i = 0 ; i < n; i++) {
minEle = Math.min(minEle, A[i]);
maxEle = Math.max(maxEle, A[i]);
}
if (K != 0 ) {
if (K % 2 == 1 ) {
for ( int i = 0 ; i < n; i++)
A[i] = maxEle - A[i];
}
else {
for ( int i = 0 ; i < n; i++)
A[i] = A[i] - minEle;
}
}
for ( int i = 0 ; i < n; i++)
System.out.print(A[i] + " " );
}
public static void main (String[] args)
{
int [] arr = { 4 , 8 , 12 , 16 };
int K = 4 ;
int N = arr.length;
printArray(arr, N, K);
}
}
|
Python3
def printArray(A, n, K):
minEle = 10 * * 9
maxEle = - 10 * * 9
for i in range (n):
minEle = min (minEle, A[i])
maxEle = max (maxEle, A[i])
if (K ! = 0 ):
if (K % 2 = = 1 ):
for i in range (n):
A[i] = maxEle - A[i]
else :
for i in range (n):
A[i] = A[i] - minEle
for i in A:
print (i, end = " " )
if __name__ = = '__main__' :
arr = [ 4 , 8 , 12 , 16 ]
K = 4
N = len (arr)
printArray(arr, N, K)
|
C#
using System;
class GFG{
static void printArray( int [] A, int n, int K)
{
int minEle = Int32.MaxValue,
maxEle = Int32.MinValue;
for ( int i = 0; i < n; i++) {
minEle = Math.Min(minEle, A[i]);
maxEle = Math.Max(maxEle, A[i]);
}
if (K != 0) {
if (K % 2 == 1) {
for ( int i = 0; i < n; i++)
A[i] = maxEle - A[i];
}
else {
for ( int i = 0; i < n; i++)
A[i] = A[i] - minEle;
}
}
for ( int i = 0; i < n; i++)
Console.Write(A[i] + " " );
}
static public void Main ()
{
int [] arr = { 4, 8, 12, 16 };
int K = 4;
int N = arr.Length;
printArray(arr, N, K);
}
}
|
Javascript
<script>
function printArray(A, n, K)
{
var minEle = 100000000, maxEle = -100000000;
for ( var i = 0; i < n; i++) {
minEle = Math.min(minEle, A[i]);
maxEle = Math.max(maxEle, A[i]);
}
if (K != 0) {
if (K % 2 == 1) {
for ( var i = 0; i < n; i++)
A[i] = maxEle - A[i];
}
else {
for ( var i = 0; i < n; i++)
A[i] = A[i] - minEle;
}
}
for ( var i = 0; i < n; i++)
document.write(A[i] + " " );
}
arr = [ 4, 8, 12, 16 ];
var K = 4;
var N = arr.length;
printArray(arr, N, K);
</script>
|
Time Complexity: O(N) where N is the size of the array.
Auxiliary Space: O(1), As constant extra space is used.
Share your thoughts in the comments
Please Login to comment...