Print lexicographically smallest array by reduce K to 0 in minimum number of operations
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++
#include <bits/stdc++.h>
using namespace std;
void solve( int n, int k, int arr[])
{
for ( int i = 0; i < n - 1; i++) {
if (arr[i] < k)
{
k = k - arr[i];
arr[n - 1]
= arr[n - 1]
+ arr[i];
arr[i] = 0;
}
else {
arr[i] = arr[i] - k;
arr[n - 1] = arr[n - 1] + k;
k = 0;
}
}
sort(arr, arr + n);
for ( int i = 0; i < n; i++)
cout << arr[i] << " " ;
}
int main()
{
int N = 6;
int K = 2;
int arr[N] = { 3, 1, 4, 6, 2, 5 };
solve(N, K, arr);
return 0;
}
|
Java
import java.io.*;
import java.util.Arrays;
class GFG
{
static void solve( int n, int k, int arr[])
{
for ( int i = 0 ; i < n - 1 ; i++) {
if (arr[i] < k)
{
k = k - arr[i];
arr[n - 1 ]
= arr[n - 1 ]
+ arr[i];
arr[i] = 0 ;
}
else {
arr[i] = arr[i] - k;
arr[n - 1 ] = arr[n - 1 ] + k;
k = 0 ;
}
}
Arrays.sort(arr);
for ( int i = 0 ; i < n; i++)
System.out.print( arr[i] + " " );
}
public static void main (String[] args) {
int N = 6 ;
int K = 2 ;
int arr[] = { 3 , 1 , 4 , 6 , 2 , 5 };
solve(N, K, arr);
}
}
|
Python3
def solve( n, k, arr):
for i in range (n - 1 ):
if (arr[i] < k):
k = k - arr[i]
arr[n - 1 ] = arr[n - 1 ] + arr[i]
arr[i] = 0
else :
arr[i] = arr[i] - k
arr[n - 1 ] = arr[n - 1 ] + k
k = 0
arr.sort()
for i in range (n):
print (arr[i], end = " " )
N = 6
K = 2
arr = [ 3 , 1 , 4 , 6 , 2 , 5 ]
solve(N, K, arr)
|
C#
using System;
public class GFG
{
static void solve( int n, int k, int []arr)
{
for ( int i = 0; i < n - 1; i++) {
if (arr[i] < k)
{
k = k - arr[i];
arr[n - 1]
= arr[n - 1]
+ arr[i];
arr[i] = 0;
}
else {
arr[i] = arr[i] - k;
arr[n - 1] = arr[n - 1] + k;
k = 0;
}
}
Array.Sort(arr);
for ( int i = 0; i < n; i++)
Console.Write( arr[i] + " " );
}
public static void Main(String[] args)
{
int N = 6;
int K = 2;
int []arr = { 3, 1, 4, 6, 2, 5 };
solve(N, K, arr);
}
}
|
Javascript
<script>
function solve(n , k , arr)
{
for ( var i = 0; i < n - 1; i++) {
if (arr[i] < k)
{
k = k - arr[i];
arr[n - 1]
= arr[n - 1]
+ arr[i];
arr[i] = 0;
}
else {
arr[i] = arr[i] - k;
arr[n - 1] = arr[n - 1] + k;
k = 0;
}
}
arr.sort();
for ( var i = 0; i < n; i++)
document.write( arr[i] + " " );
}
var N = 6;
var K = 2;
var arr = [ 3, 1, 4, 6, 2, 5 ];
solve(N, K, arr);
</script>
|
Time Complexity: O(N*log(N))
Auxiliary Space: O(1)
Last Updated :
03 Feb, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...