Cut The Sticks || Codechef Solution

 Cut The Sticks

You are given  sticks, where the length of each stick is a positive integer. A cut operation is performed on the sticks such that all of them are reduced by the length of the smallest stick.

Suppose we have six sticks of the following lengths:

5 4 4 2 2 8

Then, in one cut operation we make a cut of length 2 from each of the six sticks. For the next cut operation four sticks are left (of non-zero length), whose lengths are the following:

3 2 2 6

The above step is repeated until no sticks are left.

Given the length of  sticks, print the number of sticks that are left before each subsequent cut operations.

Note: For each cut operation, you have to recalcuate the length of smallest sticks (excluding zero-length sticks).

Input Format
The first line contains a single integer .
The next line contains  integers: a0, a1,...aN-1 separated by space, where  represents the length of the  stick.

Output Format
For each operation, print the number of sticks that are cut, on separate lines.

Constraints

Sample Input 0

6
5 4 4 2 2 8

Sample Output 0

6
4
2
1

Sample Input 1

8
1 2 3 4 3 3 2 1

Sample Output 1

8
6
4
1
  • Solution:
  1. Pyhton3.6
n=int(input())
a=list(map(int,input().split()))
while(a!=[]):
    b=[]
    print(len(a))
    k=min(a)
    for i in range(len(a)):
        z=a[i]-k
        if(z>0):
            b.append(z)
    a=b
2. In C++
#include <bits/stdc++.h> 
#define lli long long int
#define size 1005
using namespace std;
int main() { 
	ios_base::sync_with_stdio(false); cin.tie(NULL);
	int N; cin>>N;
	int temp;
	int arr[size] {0};
	for(int i=1; i<=N; i++) {
	    cin>>temp;
	    arr[temp]++;
	}
	for(int i=1; i<=size; i++) {
	    if(arr[i] > 0)
	    {
	        cout<<N<<"\n";
	        N -=arr[i];
	    }
	    if(N==0) break;
	}
	return 0;
}
                            
🙏THANKS FOR VISIT🙏

Cut The Sticks || Codechef Solution Cut The Sticks || Codechef Solution Reviewed by CodexRitik on November 06, 2020 Rating: 5

No comments:

Powered by Blogger.