-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDistributeCandies.java
More file actions
40 lines (32 loc) · 819 Bytes
/
DistributeCandies.java
File metadata and controls
40 lines (32 loc) · 819 Bytes
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
package HashTable;
import java.util.HashSet;
import java.util.Set;
/**
* Author - archit.s
* Date - 23/08/18
* Time - 12:43 PM
*/
public class DistributeCandies {
// Less optimised
// public int distributeCandies(int[] candies) {
// Set<Integer> s = new HashSet<>();
//
// for(int i=0;i<candies.length; i++){
// s.add(candies[i]);
// }
//
// return Math.min(candies.length/2, s.size());
// }
public int distributeCandies(int[] candies) {
Set<Integer> s = new HashSet<>();
for(int candy: candies){
if(!s.contains(candy)){
s.add(candy);
if(s.size() >= candies.length / 2){
return candies.length / 2;
}
}
}
return s.size();
}
}