Find final Array after subtracting maximum from all elements K times
Last Updated :
15 Jun, 2022
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++
// C++ code for the approach
#include <bits/stdc++.h>
using namespace std;
// Function for converting the array
void find(int n, int k, int arr[])
{
// Find the maximum element in array
int max = INT_MIN;
for (int i = 0; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
// If k is odd
if (k % 2 != 0) {
for (int i = 0; i < n; i++) {
cout << max - arr[i] << " ";
}
}
// If k is even
else {
// Subtract the max from every
// element of array and store
// the next maximum element in max1
int max1 = INT_MIN;
for (int i = 0; i < n; i++) {
arr[i] = max - arr[i];
if (arr[i] > max1) {
max1 = arr[i];
}
}
// Print the output
for (int i = 0; i < n; i++) {
cout << max1 - arr[i] << " ";
}
}
}
// Driver code
int main()
{
int N = 6, K = 3;
int arr[] = { 5, 38, 4, 96, 103, 41 };
// Function call
find(N, K, arr);
return 0;
}
C
// C code for the approach
#include <stdio.h>
#include<limits.h>
// Function for converting the array
void find(int n, int k, int arr[])
{
// Find the maximum element in array
int max = INT_MIN;
for (int i = 0; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
// If k is odd
if (k % 2 != 0) {
for (int i = 0; i < n; i++) {
printf("%d ", max - arr[i]);
}
}
// If k is even
else {
// Subtract the max from every
// element of array and store
// the next maximum element in max1
int max1 = INT_MIN;
for (int i = 0; i < n; i++) {
arr[i] = max - arr[i];
if (arr[i] > max1) {
max1 = arr[i];
}
}
// Print the output
for (int i = 0; i < n; i++) {
printf("%d ", max1 - arr[i]);
}
}
}
// Driver code
void main()
{
int N = 6, K = 3;
int arr[] = { 5, 38, 4, 96, 103, 41 };
// Function call
find(N, K, arr);
}
// This code is contributed by ashishsingh13122000.
Java
// Java code for the approach
import java.io.*;
class GFG
{
// Function for converting the array
public static void find(int n, int k, int arr[])
{
// Find the maximum element in array
int max = Integer.MIN_VALUE;
for (int i = 0; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
// If k is odd
if (k % 2 != 0) {
for (int i = 0; i < n; i++) {
System.out.print((max - arr[i]) + " ");
}
}
// If k is even
else {
// Subtract the max from every
// element of array and store
// the next maximum element in max1
int max1 = Integer.MIN_VALUE;
for (int i = 0; i < n; i++) {
arr[i] = max - arr[i];
if (arr[i] > max1) {
max1 = arr[i];
}
}
// Print the output
for (int i = 0; i < n; i++) {
System.out.print((max1 - arr[i]) + " ");
}
}
}
// Driver Code
public static void main(String[] args)
{
int N = 6, K = 3;
int arr[] = { 5, 38, 4, 96, 103, 41 };
// Function call
find(N, K, arr);
}
}
// This code is contributed by Rohit Pradhan
Python3
# Python code
# Function for converting the array
def find(n, k, arr):
# Find the maximum element in array
max = (-2147483647 - 1)
for i in range(0, n):
if (arr[i] > max):
max = arr[i]
# If k is odd
if (k % 2 != 0):
for i in range (0, n):
print( max - arr[i], end = " ")
# If k is even
else:
max1 = INT_MIN
for i in range(0,n):
arr[i] = max - arr[i]
if (arr[i] > max1):
max1 = arr[i]
# Print the output
for i in range(0,n):
print(max1 - arr[i],end=" ")
# Driver code
N = 6
K = 3
arr = [5, 38, 4, 96, 103, 41]
# Function call
find(N,K,arr)
# This code is contributed by ashishsingh13122000.
C#
// C# code for the approach
using System;
class GFG
{
// Function for converting the array
static void find(int n, int k, int []arr)
{
// Find the maximum element in array
int max = Int32.MinValue;
for (int i = 0; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
// If k is odd
if (k % 2 != 0) {
for (int i = 0; i < n; i++) {
Console.Write((max - arr[i]) + " ");
}
}
// If k is even
else {
// Subtract the max from every
// element of array and store
// the next maximum element in max1
int max1 = Int32.MinValue;
for (int i = 0; i < n; i++) {
arr[i] = max - arr[i];
if (arr[i] > max1) {
max1 = arr[i];
}
}
// Print the output
for (int i = 0; i < n; i++) {
Console.Write((max1 - arr[i]) + " ");
}
}
}
// Driver Code
public static void Main()
{
int N = 6, K = 3;
int []arr = { 5, 38, 4, 96, 103, 41 };
// Function call
find(N, K, arr);
}
}
// This code is contributed by Samim Hossain Mondal.
JavaScript
<script>
// JavaScript code for the approach
const INT_MIN = -2147483648;
// Function for converting the array
const find = (n, k, arr) => {
// Find the maximum element in array
let max = INT_MIN;
for (let i = 0; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
// If k is odd
if (k % 2 != 0) {
for (let i = 0; i < n; i++) {
document.write(`${max - arr[i]} `);
}
}
// If k is even
else {
// Subtract the max from every
// element of array and store
// the next maximum element in max1
let max1 = INT_MIN;
for (let i = 0; i < n; i++) {
arr[i] = max - arr[i];
if (arr[i] > max1) {
max1 = arr[i];
}
}
// Print the output
for (let i = 0; i < n; i++) {
document.write(`${max1 - arr[i]} `);
}
}
}
// Driver code
let N = 6, K = 3;
let arr = [5, 38, 4, 96, 103, 41];
// Function call
find(N, K, arr);
// This code is contributed by rakeshsahni
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Find maximum array sum after making all elements same with repeated subtraction
Given an array of n elements, find out the maximum possible sum of all elements such that all the elements are equal. Only operation allowed is choosing any two elements and replacing the larger of them by the absolute difference of the two. Examples: Input : 9 12 3 6 Output : 12 Explanation : 9 12
6 min read
Find maximum sum taking every Kth element in the array
Given an array arr[] of integers and an integer K, the task is to find the maximum sum taking every Kth element i.e. sum = arr[i] + arr[i + k] + arr[i + 2 * k] + arr[i + 3 * k] + ....... arr[i + q * k] starting with any i. Examples: Input: arr[] = {3, -5, 6, 3, 10}, K = 3 Output: 10 All possible seq
11 min read
Maximize MEX by adding or subtracting K from Array elements
Given an arr[] of size N and an integer, K, the task is to find the maximum possible value of MEX by adding or subtracting K any number of times from the array elements. MEX is the minimum non-negative integer that is not present in the array Examples: Input: arr[]={1, 3, 4}, K = 2Output: 2Explanati
7 min read
Queries to find the maximum array element after removing elements from a given range
Given an array arr[] and an array Q[][] consisting of queries of the form of {L, R}, the task for each query is to find the maximum array element after removing array elements from the range of indices [L, R]. If the array becomes empty after removing the elements from given range of indices, then p
10 min read
Maximum sum of all elements of array after performing given operations
Given an array of integers. The task is to find the maximum sum of all the elements of the array after performing the given two operations once each. The operations are: 1. Select some(possibly none) continuous elements from the beginning of the array and multiply by -1. 2. Select some(possibly none
7 min read
Maximize Kth largest element after splitting the given Array at most C times
Given an array arr[] and two positive integers K and C, the task is to maximize the Kth maximum element obtained after splitting an array element arr[] into two parts(not necessarily an integer) C number of times. Print -1 if there doesn't exist Kth maximum element. Note: It is compulsory to do spli
7 min read
Find k maximum elements of array in original order
Given an array arr[] and an integer k, we need to print k maximum elements of given array. The elements should printed in the order of the input.Note : k is always less than or equal to n. Examples: Input : arr[] = {10 50 30 60 15} k = 2 Output : 50 60 The top 2 elements are printed as per their app
11 min read
Maximum value in an array after m range increment operations
Consider an array of size n with all initial values as 0. We need to perform the following m range increment operations.increment(a, b, k) : Increment values from 'a' to 'b' by 'k'. After m operations, we need to calculate the maximum of the values in the array.Examples:Input : n = 5 m = 3 a = 0, b
10 min read
Minimize the product of Array elements after K increments
Given an array of non-negative integers arr[] and an integer K, the task is to minimize the product of array elements by performing at K increment operations on the array elements. Examples: Input: arr[] = [0, 9], K = 5Output: 0Explanation: Since 0 is present the, minimum product that can be obtaine
9 min read
Find maximum element after K increments
Given an array arr[] of size N, in one operation you can increase arr[i] by 1 if arr[i] <= arr[i+1]. The task is to find the maximum value of the array if you can perform the above operation K number of times. Examples: Input: n = 3, K = 4, arr[] = {2, 4, 4}Output: 5Explanation: The maximum eleme
9 min read