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
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
Recommended Posts:
- Find maximum array sum after making all elements same with repeated subtraction
- Print modified array after executing the commands of addition and subtraction
- Find original array from encrypted array (An array of sums of other elements)
- Find an element in array such that sum of left array is equal to sum of right array
- Check whether an array can be fit into another array rearranging the elements in the array
- Minimum cost to reach end of array array when a maximum jump of K index is allowed
- Maximize the size of array by deleting exactly k sub-arrays to make array prime
- Generate original array from an array that store the counts of greater elements on right
- Minimum number greater than the maximum of array which cannot be formed using the numbers in the array
- Print modified array after multiple array range increment operations
- Count number of permutation of an Array having no SubArray of size two or more from original Array
- Find minimum value to assign all array elements so that array product becomes greater
- Check if there exist two elements in an array whose sum is equal to the sum of rest of the array
- Kth smallest element in the array using constant space when array can't be modified
- Maximum subarray sum in array formed by repeating the given array k times
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.