• + 0 comments

    O(n) no dynamic programming, no induction. this question is stupid because it makes u think about it with dynamic programming/induction, pointing u in the wrong direction, wasted so much of my time. but the answer has nothing to do with either

    long calculate (long n) {
        long x = n % 5;
        return (n / 5) + ((x > 2) ? 2 : (x > 0) ? 1 : 0);
    }
    
    long equal(vector<int> arr) {
        int minimum = *min_element(arr.begin(), arr.end());
        long answer = LONG_MAX;
        for (int k=0; k < 5; k++) {
            long temp = 0;
            for (int i=0; i < arr.size(); i++) temp = temp + calculate(arr[i] - minimum + k);
            answer = min(answer, temp);
        }
        return answer;
    }