View difference between Paste ID: UpBZMgSk and QEeYYCrA
SHOW: | | - or go back to the newest paste.
1
// Author : Saurav Kalsoor
2-
// Duplicate Bogies - JAVA
2+
// Special Sum - JAVA
3
4
5
import java.util.*;
6
7
public class Test {
8
    
9
    static Scanner sc = new Scanner(System.in);
10
 
11
    public static void main(String[] args) {
12
    	int n = sc.nextInt();
13
		System.out.println(specialSum(n));
14-
		ArrayList<Integer> arr = new ArrayList<>();
14+
15-
		for(int i=0; i < n ;i++){
15+
16-
			arr.add(sc.nextInt());
16+
	public static int specialSum(int n){
17
		ArrayList<Integer> fact = new ArrayList<>();
18-
		System.out.println(duplicateBogies(n, arr));
18+
		TreeMap<Integer, Integer> sums = new TreeMap<>();
19
20
		int i = 2, x = 1;
21-
	public static int duplicateBogies(int n, ArrayList<Integer> arr){
21+
		int maxLimit = 100000000;
22-
		HashSet<Integer> st = new HashSet<>();
22+
		while(x < maxLimit){
23-
		int res = n;
23+
			fact.add(x);
24-
		for(int x : arr){
24+
			x *= i;
25-
			if(st.contains(x)){
25+
			i++;
26-
				res = (n - st.size());
26+
27-
				break;
27+
28
		int limit = (int)Math.pow(2, 11);
29-
			st.add(x);
29+
30
		for(i = 1; i < limit; i++){
31
			int sum = 0;
32-
		Collections.reverse(arr);
32+
			for(int j = 0; j < 11; j++){
33-
		st.clear();
33+
				if((i & (1 << j)) > 0){
34-
		
34+
					sum += fact.get(j);
35-
		for(int x : arr){
35+
				}
36-
			if(st.contains(x)){
36+
37-
				res = Math.min(res, (n - st.size()));
37+
			sums.put(sum, Math.min(bitCount(i), sums.containsKey(sum) ? sums.get(sum) : limit));
38-
				break;
38+
39
40-
			st.add(x);
40+
		int res = bitCount(n);
41
		for(Map.Entry<Integer, Integer> sum : sums.entrySet()){
42
			if(sum.getKey() > n) break;
43-
		if(res == n) res = 0;
43+
44
			res = Math.min(res, bitCount(n - sum.getKey()) + sum.getValue());
45
		}
46
		return res;
47
	}
48
	
49
	public static int bitCount(int n){
50
		int res = 0;
51
		while(n > 0){
52
			res += n%2;
53
			n /= 2;
54
		}
55
		return res;
56
	}
57
}
58