Given N number of sticks of varying lengths in an array arr, the task is to determine the sum of the count of sticks that are left after each iteration. At each iteration, cut the length of the shortest stick from remaining sticks.
Examples:
Input: N = 6, arr = {5, 4, 4, 2, 2, 8}
Output: 7
Explanation:
Iteration 1:
Initial arr = {5, 4, 4, 2, 2, 8}
Shortest stick = 2
arr with reduced length = {3, 2, 2, 0, 0, 6}
Remaining sticks = 4
Iteration 2:
arr = {3, 2, 2, 4}
Shortest stick = 2
Left stick = 2
Iteration 3:
arr = {1, 2}
Shortest stick = 1
Left stick = 1
Iteration 4:
arr = {1}
Min length = 1
Left stick = 0
Input: N = 8, arr = {1, 2, 3, 4, 3, 3, 2, 1}
Output: 11
Approach:
- Store the frequency of stick lengths in a map
- In each iteration,
- Find the frequency of min length’s stick
- Decrease the frequency of min length’s stick from each stick’s frequency
- Add the count of non-zero sticks to the resultant stick.
Below is the implementation of above approach:
C++
// C++ program to find the sum of // remaining sticks after each iterations #include <bits/stdc++.h> using namespace std; // Function to calculate // sum of remaining sticks // after each iteration int sum(int ar[], int n) { map<int, int> mp; // storing frequency of stick length for (int i = 0; i < n; i++) { mp[ar[i]]++; } int sum = 0; for (auto p : mp) { n -= p.second; sum += n; } return sum; } // Driver code int main() { int n = 6; int ar[] = { 5, 4, 4, 2, 2, 8 }; int ans = sum(ar, n); cout << ans << '\n'; return 0; } |
Java
// Java program to find the sum of // remaining sticks after each iterations import java.util.HashMap; import java.util.Map; class GFG { // Function to calculate // sum of remaining sticks // after each iteration static int sum(int ar[], int n) { HashMap<Integer, Integer> mp = new HashMap<>(); for (int i = 0; i < n; i++) { mp.put(ar[i], 0); } // storing frequency of stick length for (int i = 0; i < n; i++) { mp.put(ar[i], mp.get(ar[i]) + 1) ; } int sum = 0; for(Map.Entry p : mp.entrySet()) { n -= (int)p.getValue(); sum += n; } return sum; } // Driver code public static void main (String[] args) { int n = 6; int ar[] = { 5, 4, 4, 2, 2, 8 }; int ans = sum(ar, n); System.out.println(ans); } } // This code is contributed by kanugargng |
Python 3
# Python proagram to find sum # of remaining sticks # Function to calculate # sum of remaining sticks # after each iteration def sum(ar, n): mp = dict() for i in ar: if i in mp: mp[i]+= 1 else: mp[i] = 1 mp = sorted(list(mp.items())) sum = 0 for pair in mp: n-= pair[1] sum+= n return sum# Driver code def main(): n = 6 ar = [5, 4, 4, 2, 2, 8] ans = sum(ar, n) print(ans) main() |
C#
// C# program to find the sum of // remaining sticks after each iterations using System; using System.Collections.Generic; class GFG { // Function to calculate // sum of remaining sticks // after each iteration static int sum(int []ar, int n) { SortedDictionary<int, int> mp = new SortedDictionary<int, int>(); // storing frequency of stick length for (int i = 0; i < n; i++) { if(!mp.ContainsKey(ar[i])) mp.Add(ar[i], 0); else mp[ar[i]] = 0; } // storing frequency of stick length for (int i = 0; i < n; i++) { if(!mp.ContainsKey(ar[i])) mp.Add(ar[i], 1); else mp[ar[i]] = ++mp[ar[i]]; } int sum = 0; foreach(KeyValuePair<int, int> p in mp) { n -= p.Value; sum += n; } return sum; } // Driver code public static void Main (String[] args) { int n = 6; int []ar = { 5, 4, 4, 2, 2, 8 }; int ans = sum(ar, n); Console.WriteLine(ans); } } // This code is contributed by 29AjayKumar |
7
Time Complexity:
where N is the number of sticks
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:
- Find i’th index character in a binary string obtained after n iterations | Set 2
- Find last remaining element after reducing the Array
- Find the last remaining Character in the Binary String according to the given conditions
- Dividing Sticks Problem
- Find last two remaining elements after removing median of any 3 consecutive elements repeatedly
- Area of the largest square that can be formed from the given length sticks using Hashing
- Fill array with 1's using minimum iterations of filling neighbors
- Minimize total cost without repeating same task in two consecutive iterations
- Element equal to the sum of all the remaining elements
- Remove k corner elements to maximize remaining sum
- Check if the array has an element which is equal to XOR of remaining elements
- Check if the array has an element which is equal to sum of all the remaining elements
- Count the subarray with sum strictly greater than the sum of remaining elements
- Delete odd and even numbers at alternate step such that sum of remaining elements is minimized
- Check if the array has an element which is equal to product of remaining elements
- Last element remaining by deleting two largest elements and replacing by their absolute difference if they are unequal
- Average of remaining elements after removing K largest and K smallest elements from array
- Minimum number of elements to be removed such that the sum of the remaining elements is equal to k
- Minimum Circles needed to be removed so that all remaining circles are non intersecting
- Sort first k values in ascending order and remaining n-k values in descending order
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.

