-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcutTheSticks.java
More file actions
71 lines (57 loc) · 1.44 KB
/
cutTheSticks.java
File metadata and controls
71 lines (57 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/* You are given N 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.
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*/
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static int x=0;
static int[] cutTheSticks(int[] arr) {
Arrays.sort(arr);
int i,j,prev=arr[0],count=1,ans=arr.length;
int [] result = new int [arr.length];
result[x++]=ans;
for(i=1;i<arr.length;i++)
{
if(prev==arr[i])
count++;
else
{
prev=arr[i];
ans=ans-count;
result[x++]=ans;
count=1;
}
}
return result;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] arr = new int[n];
for(int i = 0; i < n; i++){
arr[i] = in.nextInt();
}
int [] result = cutTheSticks(arr);
for(int i=0;i<x;i++)
System.out.println(result[i]);
in.close();
}
}