Given an array arr[] and an integer K, the task is to reduce the value of K to 0 by performing the following operations. One operation is defined as choosing 2 indices i, j and subtracting the minimum of arr[i] and K ( i.e., X = min(arr[i], K) from arr[i] ( i.e., arr[i] = arr[i] – X) and adding the minimum value to arr[j] (arr[j] = arr[j] + X) and print the lexicographically smallest array. Please note that the elements of the array arr[] cannot be negative.
Examples:
Input: N = 4, K = 2, arr[] = {4, 3, 2, 1}
Output: 2 2 3 3
Explanation:
Operation 1: Select indices 0 and 3, then subtract min of arr[0](=4) and K(=2) from arr[0] and add the minimum value i.e K(=2) in arr[3](=1). Now, the modified array is {2, 3, 2, 3}
Now, sort the modified array and print it.Input: N = 3, K = 15, arr[] = {1, 2, 3}
Output: 0 0 6
Explanation:
Operation 1: Select indices 0 and 2, then subtract min of arr[0](=1) and K(=15) from arr[0] and add the minimum value i.e arr[0](=1) in arr[2](=3). Now the modified array is {0, 2, 4}.
Operation 2: Select indices 1 and 2, then subtract min of arr[1](=2) and K(=15) from arr[1] and add the minimum value i.e arr[1](=2) in arr[2](=4). Now the modified array is {0, 0, 6}.
Now, sort the modified array and print it.
Approach: This problem can be solved by iterating over the array arr[]. Follow the steps below to solve the problem:
-
Iterate over the range [0, N-1] using the variable i and perform the following steps:
- If arr[i] is less than K, then take the following steps.
- Subtract arr[i] from the variable K, add the value of arr[i] to arr[n-1] and set the value of arr[i] to 0.
- Else, subtract K from the value of arr[i], add the value of K to arr[n-1] and set the value of K to 0 and break the loop.
- Sort the array arr[].
- After performing the above steps, print the elements of the array arr[].
Below is the implementation of the above approach:
// C++ program for the above approach. #include <bits/stdc++.h> using namespace std;
// Function to find the resultant array. void solve( int n, int k, int arr[])
{ for ( int i = 0; i < n - 1; i++) {
// checking if aith value less than K
if (arr[i] < k)
{
// subtracting ai value from K
k = k - arr[i];
// Adding ai value to an-1
arr[n - 1]
= arr[n - 1]
+ arr[i];
arr[i] = 0;
}
// if arr[i] value is greater than K
else {
arr[i] = arr[i] - k;
arr[n - 1] = arr[n - 1] + k;
k = 0;
}
}
// sorting the given array
// to know about this function
// check gfg stl sorting article
sort(arr, arr + n);
// Displaying the final array
for ( int i = 0; i < n; i++)
cout << arr[i] << " " ;
} // Driver code int main()
{ int N = 6;
int K = 2;
int arr[N] = { 3, 1, 4, 6, 2, 5 };
solve(N, K, arr);
return 0;
} |
// Java program for the above approach import java.io.*;
import java.util.Arrays;
class GFG
{ // Function to find the resultant array.
static void solve( int n, int k, int arr[])
{ for ( int i = 0 ; i < n - 1 ; i++) {
// checking if aith value less than K
if (arr[i] < k)
{
// subtracting ai value from K
k = k - arr[i];
// Adding ai value to an-1
arr[n - 1 ]
= arr[n - 1 ]
+ arr[i];
arr[i] = 0 ;
}
// if arr[i] value is greater than K
else {
arr[i] = arr[i] - k;
arr[n - 1 ] = arr[n - 1 ] + k;
k = 0 ;
}
}
// sorting the given array
// to know about this function
// check gfg stl sorting article
Arrays.sort(arr);
// Displaying the final array
for ( int i = 0 ; i < n; i++)
System.out.print( arr[i] + " " );
} // Driver code public static void main (String[] args) {
int N = 6 ;
int K = 2 ;
int arr[] = { 3 , 1 , 4 , 6 , 2 , 5 };
solve(N, K, arr);
}
} // This code is contributed by Potta Lokesh |
# Python program for the above approach. # Function to find the resultant array. def solve( n, k, arr):
for i in range (n - 1 ):
# checking if aith value less than K
if (arr[i] < k):
# subtracting ai value from K
k = k - arr[i]
# Adding ai value to an-1
arr[n - 1 ] = arr[n - 1 ] + arr[i]
arr[i] = 0
# if arr[i] value is greater than K
else :
arr[i] = arr[i] - k
arr[n - 1 ] = arr[n - 1 ] + k
k = 0
# sorting the given array
# to know about this function
# check gfg stl sorting article
arr.sort()
# Displaying the final array
for i in range (n):
print (arr[i], end = " " )
# Driver code N = 6
K = 2
arr = [ 3 , 1 , 4 , 6 , 2 , 5 ]
solve(N, K, arr) # This code is contributed by shivanisinghss2110 |
// C# program for the above approach using System;
public class GFG
{ // Function to find the resultant array.
static void solve( int n, int k, int []arr)
{
for ( int i = 0; i < n - 1; i++) {
// checking if aith value less than K
if (arr[i] < k)
{
// subtracting ai value from K
k = k - arr[i];
// Adding ai value to an-1
arr[n - 1]
= arr[n - 1]
+ arr[i];
arr[i] = 0;
}
// if arr[i] value is greater than K
else {
arr[i] = arr[i] - k;
arr[n - 1] = arr[n - 1] + k;
k = 0;
}
}
// sorting the given array
// to know about this function
// check gfg stl sorting article
Array.Sort(arr);
// Displaying the readonly array
for ( int i = 0; i < n; i++)
Console.Write( arr[i] + " " );
}
// Driver code
public static void Main(String[] args)
{
int N = 6;
int K = 2;
int []arr = { 3, 1, 4, 6, 2, 5 };
solve(N, K, arr);
}
} // This code is contributed by shikhasingrajput |
<script> // javascript program for the above approach // Function to find the resultant array.
function solve(n , k , arr)
{ for ( var i = 0; i < n - 1; i++) {
// checking if aith value less than K
if (arr[i] < k)
{
// subtracting ai value from K
k = k - arr[i];
// Adding ai value to an-1
arr[n - 1]
= arr[n - 1]
+ arr[i];
arr[i] = 0;
}
// if arr[i] value is greater than K
else {
arr[i] = arr[i] - k;
arr[n - 1] = arr[n - 1] + k;
k = 0;
}
}
// sorting the given array
// to know about this function
// check gfg stl sorting article
arr.sort();
// Displaying the final array
for ( var i = 0; i < n; i++)
document.write( arr[i] + " " );
} // Driver code var N = 6;
var K = 2;
var arr = [ 3, 1, 4, 6, 2, 5 ];
solve(N, K, arr);
// This code contributed by shikhasingrajput </script> |
1 1 2 4 6 7
Time Complexity: O(N*log(N))
Auxiliary Space: O(1)