# Print lexicographically smallest array by reduce K to 0 in minimum number of operations

• Last Updated : 20 Aug, 2021

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:

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

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(=4) and K(=2) from arr and add the minimum value i.e K(=2) in arr(=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(=1) and K(=15) from arr and add the minimum value i.e arr(=1) in arr(=3). Now the modified array is {0, 2, 4}.
Operation 2: Select indices 1 and 2, then subtract min of arr(=2) and K(=15) from arr and add the minimum value i.e arr(=2) in arr(=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++

 `// C++ program for the above approach.``#include ``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)` `        ``{``            ``// substracting 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

 `// 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)` `        ``{``          ` `            ``// substracting 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`

## Python3

 `# 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):``            ` `            ``# substracting 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#

 `// 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)` `      ``{` `        ``// substracting 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`

## Javascript

 ``
Output
`1 1 2 4 6 7 `

Time Complexity: O(N*log(N))
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up