# Subtraction in the Array

Given an integer k and an array arr[], the task is to repeat the following operation exactly k times:
Find the minimum non-zero element in the array, print it and then subtract this number from all the non-zero elements of the array. If all the elements of the array are < 0, just print 0.

Examples:

Input: arr[] = {3, 6, 4, 2}, k = 5
Output: 2 1 1 2 0
k = 1; Pick 2 and update arr[] = {1, 4, 2, 0}
k = 2; Pick 1, arr[] = {0, 3, 1, 0}
k = 3; Pick 1, arr[] = {0, 2, 0, 0}
k = 4; Pick 2, arr[] = {0, 0, 0, 0}
k = 5; Nothing to pick so print 0

Input: arr[] = {1, 2}, k = 3
Output: 1 1 0

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: Sort the array and take an extra variable named sum which will store previous element which became 0.
Taking arr[] = {3, 6, 4, 2} and initially sum = 0 after sorting the array, it becomes arr[] = {2, 3, 4, 6}.
Now sum = 0, and we print first nonzero element i.e. 2 and assign sum = 2.
In the next iteration, pick second element i.e. 3 and print 3 – sum i.e. 1 as 2 has already been subtracted from all the other non-zero elements. Repeat these steps exactly k times.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` `#define ll long long int ` ` `  `// Function to perform the given operation on arr[] ` `void` `operations(``int` `arr[], ``int` `n, ``int` `k) ` `{ ` `    ``sort(arr, arr + n); ` `    ``ll i = 0, sum = 0; ` `    ``while` `(k--) { ` ` `  `        ``// Skip elements which are 0 ` `        ``while` `(i < n && arr[i] - sum == 0) ` `            ``i++; ` ` `  `        ``// Pick smallest non-zero element ` `        ``if` `(i < n && arr[i] - sum > 0) { ` `            ``cout << arr[i] - sum << ``" "``; ` `            ``sum = arr[i]; ` `        ``} ` ` `  `        ``// If all the elements of arr[] are 0 ` `        ``else` `            ``cout << 0 << endl; ` `    ``} ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `k = 5; ` `    ``int` `arr[] = { 3, 6, 4, 2 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` `    ``operations(arr, n, k); ` `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach ` `import` `java.util.*; ` ` `  `class` `GFG  ` `{ ` ` `  `// Function to perform the given operation on arr[] ` `static` `void` `operations(``int` `arr[], ``int` `n, ``int` `k) ` `{ ` `    ``Arrays.sort(arr); ` `    ``int` `i = ``0``, sum = ``0``; ` `    ``while` `(k-- > ``0``)  ` `    ``{ ` ` `  `        ``// Skip elements which are 0 ` `        ``while` `(i < n && arr[i] - sum == ``0``) ` `            ``i++; ` ` `  `        ``// Pick smallest non-zero element ` `        ``if` `(i < n && arr[i] - sum > ``0``)  ` `        ``{ ` `            ``System.out.print(arr[i] - sum + ``" "``); ` `            ``sum = arr[i]; ` `        ``} ` ` `  `        ``// If all the elements of arr[] are 0 ` `        ``else` `            ``System.out.println(``"0"``); ` `    ``} ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String args[])  ` `{ ` `    ``int` `k = ``5``; ` `    ``int` `arr[] = { ``3``, ``6``, ``4``, ``2` `}; ` `    ``int` `n = arr.length; ` `    ``operations(arr, n, k); ` `} ` `} ` ` `  `// This code is contributed by Princi Singh `

## Python3

 `     `  `# Python implementation of the approach ` ` `  `# Function to perform the given operation on arr[] ` `def` `operations(arr, n, k): ` `    ``arr.sort(); ` `    ``i ``=` `0``; ``sum` `=` `0``; ` `    ``while` `(k > ``0``): ` ` `  `        ``# Skip elements which are 0 ` `        ``while` `(i < n ``and` `arr[i] ``-` `sum` `=``=` `0``): ` `            ``i``+``=``1``; ` ` `  `        ``# Pick smallest non-zero element ` `        ``if` `(i < n ``and` `arr[i] ``-` `sum` `> ``0``): ` `            ``print``(arr[i] ``-` `sum``, end``=` `" "``); ` `            ``sum` `=` `arr[i]; ` ` `  `        ``# If all the elements of arr[] are 0 ` `        ``else``: ` `            ``print``(``0``); ` `        ``k``-``=``1``; ` `         `  `# Driver code ` `k ``=` `5``; ` `arr ``=` `[ ``3``, ``6``, ``4``, ``2` `]; ` `n ``=` `len``(arr); ` `operations(arr, n, k); ` ` `  `# This code is contributed by PrinciRaj1992 `

## C#

 `// C# implementation of the approach ` `using` `System; ` `     `  `class` `GFG  ` `{ ` ` `  `// Function to perform the given operation on arr[] ` `static` `void` `operations(``int` `[]arr, ``int` `n, ``int` `k) ` `{ ` `    ``Array.Sort(arr); ` `    ``int` `i = 0, sum = 0; ` `    ``while` `(k-- > 0)  ` `    ``{ ` ` `  `        ``// Skip elements which are 0 ` `        ``while` `(i < n && arr[i] - sum == 0) ` `            ``i++; ` ` `  `        ``// Pick smallest non-zero element ` `        ``if` `(i < n && arr[i] - sum > 0)  ` `        ``{ ` `            ``Console.Write(arr[i] - sum + ``" "``); ` `            ``sum = arr[i]; ` `        ``} ` ` `  `        ``// If all the elements of arr[] are 0 ` `        ``else` `            ``Console.WriteLine(``"0"``); ` `    ``} ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main(String []args)  ` `{ ` `    ``int` `k = 5; ` `    ``int` `[]arr = { 3, 6, 4, 2 }; ` `    ``int` `n = arr.Length; ` `    ``operations(arr, n, k); ` `} ` `} ` ` `  `// This code has been contributed by 29AjayKumar `

Output:

```2 1 1 2 0
```

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.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.