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.
Approach: The approach to solving this problem is to sort the array and then find the number of minimum length sticks that are of the same length while traversing and update the sum accordingly at each step and in the end return the sum.
C++
// C++ program to find the sum of
// remaining sticks after each iterations
#include <bits/stdc++.h>
usingnamespacestd;
// Function to calculate
// sum of remaining sticks
// after each iteration
intsum(vector<int> &arr, intn)
{
intsum = 0;
sort(arr.begin(),arr.end());
intprev=0,count=1,s=arr.size();
inti=1;
while(i<arr.size()){
if(arr[i]==arr[prev]){
count++;
}else{
prev=i;
sum+=s-count;
s-=count;
count=1;
}
i++;
}
returnsum;
}
// Driver code
intmain()
{
intn = 6;
vector<int> ar{ 5, 4, 4, 2, 2, 8 };
intans = sum(ar, n);
cout << ans << '\n';
return0;
}
Java
// Java program to find the sum of
// remaining sticks after each iterations
importjava.io.*;
importjava.util.*;
classGFG{
// Function to calculate
// sum of remaining sticks
// after each iteration
publicstaticintsum(intarr[], intn)
{
intsum = 0;
Arrays.sort(arr);
intprev = 0, count = 1, s = n;
inti = 1;
while(i < n)
{
if(arr[i] == arr[prev])
{
count++;
}
else
{
prev = i;
sum += s - count;
s -= count;
count = 1;
}
i++;
}
returnsum;
}
// Driver code
publicstaticvoidmain(String[] args)
{
intn = 6;
intar[] = { 5, 4, 4, 2, 2, 8};
intans = sum(ar, n);
System.out.println(ans);
}
}
// This code is contributed by Manu Pathria
Python3
# Python program to find the sum of
# remaining sticks after each iterations
# Function to calculate
# sum of remaining sticks
# after each iteration
defsum(arr, n):
sum=0
arr.sort()
prev, count, s =0, 1, n
i =1
while(i < n):
if(arr[i] ==arr[prev]):
count +=1
else:
prev =i
sum+=s -count
s -=count
count =1
i +=1
returnsum
# Driver code
n =6
ar =[ 5, 4, 4, 2, 2, 8]
ans =sum(ar, n)
print(ans)
# This code is contributed by shinjanpatra
C#
// C# program to find the sum of
// remaining sticks after each iterations
usingSystem;
usingSystem.Collections.Generic;
classGFG
{
// Function to calculate
// sum of remaining sticks
// after each iteration
publicstaticintsum(int[] arr, intn)
{
intsum = 0;
Array.Sort(arr);
intprev = 0, count = 1, s = n;
inti = 1;
while(i < n)
{
if(arr[i] == arr[prev])
{
count++;
}
else
{
prev = i;
sum += s - count;
s -= count;
count = 1;
}
i++;
}
returnsum;
}
// Driver code
publicstaticvoidMain (String[] args)
{
intn = 6;
int[] ar = { 5, 4, 4, 2, 2, 8 };
intans = sum(ar, n);
Console.WriteLine(ans);
}
}
// This code is contributed by sanjoy_62.
Javascript
<script>
// Java Script program to find the sum of
// remaining sticks after each iterations
// Function to calculate
// sum of remaining sticks
// after each iteration
functionsum(arr,n)
{
let sum = 0;
arr.sort();
let prev = 0, count = 1, s = n;
let i = 1;
while(i < n)
{
if(arr[i] == arr[prev])
{
count++;
}
else
{
prev = i;
sum += s - count;
s -= count;
count = 1;
}
i++;
}
returnsum;
}
// Driver code
let n = 6;
let ar = [ 5, 4, 4, 2, 2, 8 ];
let ans = sum(ar, n);
document.write(ans);
// This code is contributed by manoj
</script>
Output
7
Time Complexity:O(Nlog(N)) where N is the number of sticks. Auxiliary Space: O(1), no extra space is required, so it is a constant.
Another 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>
usingnamespacestd;
// Function to calculate
// sum of remaining sticks
// after each iteration
intsum(intar[], intn)
{
map<int, int> mp;
// storing frequency of stick length
for(inti = 0; i < n; i++) {
mp[ar[i]]++;
}
intsum = 0;
for(autop : mp) {
n -= p.second;
sum += n;
}
returnsum;
}
// Driver code
intmain()
{
intn = 6;
intar[] = { 5, 4, 4, 2, 2, 8 };
intans = sum(ar, n);
cout << ans << '\n';
return0;
}
Java
// Java program to find the sum of
// remaining sticks after each iterations
importjava.util.HashMap;
importjava.util.Map;
classGFG
{
// Function to calculate
// sum of remaining sticks
// after each iteration
staticintsum(intar[], intn)
{
HashMap<Integer,
Integer> mp = newHashMap<>();
for(inti = 0; i < n; i++)
{
mp.put(ar[i], 0);
}
// storing frequency of stick length
for(inti = 0; i < n; i++)
{
mp.put(ar[i], mp.get(ar[i]) + 1) ;
}
intsum = 0;
for(Map.Entry p : mp.entrySet())
{
n -= (int)p.getValue();
sum += n;
}
returnsum;
}
// Driver code
publicstaticvoidmain (String[] args)
{
intn = 6;
intar[] = { 5, 4, 4, 2, 2, 8};
intans = sum(ar, n);
System.out.println(ans);
}
}
// This code is contributed by kanugargng
Python3
# Python program to find sum
# of remaining sticks
# Function to calculate
# sum of remaining sticks
# after each iteration
defsum(ar, n):
mp =dict()
fori inar:
ifi inmp:
mp[i]+=1
else:
mp[i] =1
mp =sorted(list(mp.items()))
sum=0
forpair inmp:
n-=pair[1]
sum+=n
returnsum
# Driver code
defmain():
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
usingSystem;
usingSystem.Collections.Generic;
classGFG
{
// Function to calculate
// sum of remaining sticks
// after each iteration
staticintsum(int[]ar, intn)
{
SortedDictionary<int,
int> mp = newSortedDictionary<int,
int>();
// storing frequency of stick length
for(inti = 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(inti = 0; i < n; i++)
{
if(!mp.ContainsKey(ar[i]))
mp.Add(ar[i], 1);
else
mp[ar[i]] = ++mp[ar[i]];
}
intsum = 0;
foreach(KeyValuePair<int, int> p inmp)
{
n -= p.Value;
sum += n;
}
returnsum;
}
// Driver code
publicstaticvoidMain (String[] args)
{
intn = 6;
int[]ar = { 5, 4, 4, 2, 2, 8 };
intans = sum(ar, n);
Console.WriteLine(ans);
}
}
// This code is contributed by 29AjayKumar
Javascript
<script>
// Javascript program to find the sum of
// remaining sticks after each iterations
// Function to calculate
// sum of remaining sticks
// after each iteration
functionsum(ar, n)
{
let mp = newMap();
for(let i = 0; i < n; i++)
{
mp.set(ar[i], 0);
}
// storing frequency of stick length
for(let i = 0; i < n; i++)
{
mp.set(ar[i], mp.get(ar[i]) + 1);
}
mp = newMap([...mp].sort((a, b) => String(a[0]).localeCompare(b[0])))
let sum = 0;
for(let p of mp)
{
n -= p[1]
sum += n;
}
returnsum;
}
// Driver code
let n = 6;
let ar = [5, 4, 4, 2, 2, 8];
let ans = sum(ar, n);
document.write(ans + '<br>');
// This code is contributed by _saurabh_jaiswal.
</script>
Output:
7
Time Complexity:where N is the number of sticks Auxiliary Space: O(N), where N is the number of sticks.
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!