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 0Input: arr[] = {1, 2}, k = 3
Output: 1 1 0
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 <bits/stdc++.h> 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[0]); 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 |
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.
Recommended Posts:
- Check whether an array can be fit into another array rearranging the elements in the array
- Find whether an array is subset of another array | Added Method 3
- Sort an array according to the order defined by another array
- Check if reversing a sub array make the array sorted
- Given a sorted array and a number x, find the pair in array whose sum is closest to x
- Check whether a given array is a k sorted array or not
- Why is it faster to process sorted array than an unsorted array ?
- Sorting an array according to another array using pair in STL
- Find Sum of all unique sub-array sum for a given array.
- Maximum in an array that can make another array sorted
- Minimum number greater than the maximum of array which cannot be formed using the numbers in the array
- Permutation of an array that has smaller values from another array
- Maximum subarray sum in array formed by repeating the given array k times
- Maximize the median of the given array after adding K elements to the same array
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
- Maximize the size of array by deleting exactly k sub-arrays to make array prime
- Print elements of an array according to the order defined by another array | set 2
- Find K such that changing all elements of the Array greater than K to K will make array sum N
- Sort an array by swapping elements of different type specified by another array
- Maximize Array sum by swapping at most K elements with another array
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.

