Sort by

recency

|

60 Discussions

|

  • + 0 comments
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Collections;
    import java.util.List;
    import java.util.Scanner;
    import java.util.stream.Collectors;
    
    public class Solution {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
    
            int n = sc.nextInt();
            int[] a = new int[n];
            for (int i = 0; i < a.length; i++) {
                a[i] = sc.nextInt();
            }
            System.out.println(String.join(" ", solve(a).stream().map(Object::toString).collect(Collectors.toList())));
    
            sc.close();
        }
    
        static List<Integer> solve(int[] a) {
            int sum = Arrays.stream(a).sum();
    
            List<Integer> sizes = new ArrayList<Integer>();
            for (int i = 1; i * i <= sum; i++) {
                if (sum % i == 0) {
                    if (isValid(a, i)) {
                        sizes.add(i);
                    }
    
                    if (i * i != sum) {
                        if (isValid(a, sum / i)) {
                            sizes.add(sum / i);
                        }
                    }
                }
            }
            Collections.sort(sizes);
            return sizes;
        }
    
        static boolean isValid(int[] a, int size) {
            int remain = size;
            for (int oneA : a) {
                if (remain == oneA) {
                    remain = size;
                } else if (remain < oneA) {
                    return false;
                } else {
                    remain -= oneA;
                }
            }
            return remain == size;
        }
    }
    
  • + 0 comments

    This problem seems like an interesting puzzle! Thanks for sharing! 11xplay registration

  • + 0 comments
    import itertools
    def solve(a):
        sl = sum(a)
        acum = list(itertools.accumulate(a))
        aset = set(acum)
        return [a for a in acum if sl % a == 0 and all(b in aset for b in range(a, sl, a))]
    
  • + 0 comments

    But Solution is almost correct for all but only two, anyone can recode my code

    def solve(a):
        pl = [1]
        tot = sum(a)
        i = 1
        mid = tot // i
        while mid > 1:
            if tot % mid == 0:
                pl.append(mid)
            i += 1
            while mid == tot // i:
                i += 1
            mid = tot // i
        pl.sort()
        ml = []
        print(pl)
        for i in pl:
            tot = 0
            for _ in a:
                tot += _
                if tot == i:
                    tot = 0
                if tot > i:
                    break
            else:
                ml.append(i)
        return ml
    
  • + 1 comment
    def solve(a):
    	min_bus, max_bus = max(a), sum(a)
    	buses = {}
    	current = 0
    	for ai in a:
    		buses = {i: (bus+ai) % i  \
    			for i,bus in buses.items() \
    				if bus+ai <= i}
    		current += ai
    		if max_bus % current == 0 and current >= min_bus:
    			buses[current] = 0
    	return sorted(list(buses.keys()))