Given an array arr[] of N integers and an integer K, the task is to do the below operations with this array K times. Operations are as follows:
- Find the maximum element (say M) from the array.
- Now replace every element with M-arri. where 1 ≤ i ≤ N.
Examples:
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}.
2nd operation => Maximum array element is 99. Subtract 99 from all elements. arr[] = {1, 34, 0, 92, 99, 37}
3rd operation => Maximum array element is 99. Subtract 99 from all elements. arr[] = {98, 65, 99, 7, 0, 62}.
This will be the last state.
Input: N = 5, K = 1, arr[] = {8, 4, 3, 10, 15}
Output: 7 11 12 5 0
Naive Approach: Simple approach is to perform the above operations K times. Every time find the maximum element in the array and then update all the elements with the difference from the maximum.
Time complexity: O(N*K).
Auxiliary Space: O(1).
Efficient Approach: The efficient solution to this problem is based on the below observation:
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.
Follow the below steps to implement the observation:
- Take one variable max and store the maximum element of arr.
- If K is odd
- Traverse the array and subtract every element from the maximum element.
- Else
- Traverse the arr and subtract every element from the maximum element and
store the maximum element in max1 variable.
- Again traverse the arr and subtract every element from the maximum element.
- Print the final array.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void find( int n, int k, int arr[])
{
int max = INT_MIN;
for ( int i = 0; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
if (k % 2 != 0) {
for ( int i = 0; i < n; i++) {
cout << max - arr[i] << " " ;
}
}
else {
int max1 = INT_MIN;
for ( int i = 0; i < n; i++) {
arr[i] = max - arr[i];
if (arr[i] > max1) {
max1 = arr[i];
}
}
for ( int i = 0; i < n; i++) {
cout << max1 - arr[i] << " " ;
}
}
}
int main()
{
int N = 6, K = 3;
int arr[] = { 5, 38, 4, 96, 103, 41 };
find(N, K, arr);
return 0;
}
|
C
#include <stdio.h>
#include<limits.h>
void find( int n, int k, int arr[])
{
int max = INT_MIN;
for ( int i = 0; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
if (k % 2 != 0) {
for ( int i = 0; i < n; i++) {
printf ( "%d " , max - arr[i]);
}
}
else {
int max1 = INT_MIN;
for ( int i = 0; i < n; i++) {
arr[i] = max - arr[i];
if (arr[i] > max1) {
max1 = arr[i];
}
}
for ( int i = 0; i < n; i++) {
printf ( "%d " , max1 - arr[i]);
}
}
}
void main()
{
int N = 6, K = 3;
int arr[] = { 5, 38, 4, 96, 103, 41 };
find(N, K, arr);
}
|
Java
import java.io.*;
class GFG
{
public static void find( int n, int k, int arr[])
{
int max = Integer.MIN_VALUE;
for ( int i = 0 ; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
if (k % 2 != 0 ) {
for ( int i = 0 ; i < n; i++) {
System.out.print((max - arr[i]) + " " );
}
}
else {
int max1 = Integer.MIN_VALUE;
for ( int i = 0 ; i < n; i++) {
arr[i] = max - arr[i];
if (arr[i] > max1) {
max1 = arr[i];
}
}
for ( int i = 0 ; i < n; i++) {
System.out.print((max1 - arr[i]) + " " );
}
}
}
public static void main(String[] args)
{
int N = 6 , K = 3 ;
int arr[] = { 5 , 38 , 4 , 96 , 103 , 41 };
find(N, K, arr);
}
}
|
Python3
def find(n, k, arr):
max = ( - 2147483647 - 1 )
for i in range ( 0 , n):
if (arr[i] > max ):
max = arr[i]
if (k % 2 ! = 0 ):
for i in range ( 0 , n):
print ( max - arr[i], end = " " )
else :
max1 = INT_MIN
for i in range ( 0 ,n):
arr[i] = max - arr[i]
if (arr[i] > max1):
max1 = arr[i]
for i in range ( 0 ,n):
print (max1 - arr[i],end = " " )
N = 6
K = 3
arr = [ 5 , 38 , 4 , 96 , 103 , 41 ]
find(N,K,arr)
|
C#
using System;
class GFG
{
static void find( int n, int k, int []arr)
{
int max = Int32.MinValue;
for ( int i = 0; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
if (k % 2 != 0) {
for ( int i = 0; i < n; i++) {
Console.Write((max - arr[i]) + " " );
}
}
else {
int max1 = Int32.MinValue;
for ( int i = 0; i < n; i++) {
arr[i] = max - arr[i];
if (arr[i] > max1) {
max1 = arr[i];
}
}
for ( int i = 0; i < n; i++) {
Console.Write((max1 - arr[i]) + " " );
}
}
}
public static void Main()
{
int N = 6, K = 3;
int []arr = { 5, 38, 4, 96, 103, 41 };
find(N, K, arr);
}
}
|
Javascript
<script>
const INT_MIN = -2147483648;
const find = (n, k, arr) => {
let max = INT_MIN;
for (let i = 0; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
if (k % 2 != 0) {
for (let i = 0; i < n; i++) {
document.write(`${max - arr[i]} `);
}
}
else {
let max1 = INT_MIN;
for (let i = 0; i < n; i++) {
arr[i] = max - arr[i];
if (arr[i] > max1) {
max1 = arr[i];
}
}
for (let i = 0; i < n; i++) {
document.write(`${max1 - arr[i]} `);
}
}
}
let N = 6, K = 3;
let arr = [5, 38, 4, 96, 103, 41];
find(N, K, arr);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
15 Jun, 2022
Like Article
Save Article