1
\$\begingroup\$

Source: https://www.hackerrank.com/challenges/equal/problem

Problem Description:

Christy is interning at HackerRank. One day she has to distribute some chocolates to her colleagues. She is biased towards her friends and plans to give them more than the others. One of the program managers hears of this and tells her to make sure everyone gets the same number.

To make things difficult, she must equalize the number of chocolates in a series of operations. For each operation, she can give 1, 2 or 5 chocolates to all but one colleague. Everyone who gets chocolate in a round receives the same number of pieces. For example, assume the starting distribution is [1, 1, 5]. She can give 2 bars to the first two and the distribution will be [3, 3, 5]. On the next round, she gives the same two 2 bars each, and everyone has the same number: [5, 5, 5].

Given a starting distribution, calculate the minimum number of operations needed so that every colleague has the same number of chocolates. It should return an integer that represents the minimum number of operations required.

I have come up with two working solutions for the above problem which are presented below for review and further suggestions:

Solution One

static int equal(int[] arr){
    Arrays.sort(arr);
    int min = arr[0];
    int arrLen = arr.length;
    int finalSolution = -1;
    int subFinal;
    int delta;
    int[][] results = new int[arrLen][3];

    for (int j = 0; j < 3; j++){
        subFinal = 0;

        for (int i = 0; i < arrLen; i++){
            results[i][j] = 0;

            delta = ( arr[i] - min ) + j;

            while ( true ) {
                if( delta >=5 ) {
                    delta -= 5;
                }else if( delta >=2 ) {
                    delta -= 2;
                }else if ( delta >= 1 ) {
                    delta -= 1;
                } else {
                    break;
                }
                results[i][j]++;
            }

            subFinal += results[i][j];
        }

        if( finalSolution > subFinal || finalSolution < 0 ) finalSolution = subFinal;
    }

    return finalSolution;
}

Solution Two

static int equal( int[] arr) {
    Arrays.sort(arr);
    int lenArr = arr.length - 1;
    int operations = 0;
    int min = arr[0];
    int tempMin = arr[0];

    while ( lenArr >= 0 ) {

        int diffMaxMin = arr[lenArr] - min;

        if( diffMaxMin >= 5 ) {
            tempMin = arr[lenArr] - 5;
            operations++;

        } else if ( diffMaxMin >= 2 ) {
            tempMin = arr[lenArr] - 2;
            operations++;

        } else if ( diffMaxMin >= 1 ) {
            tempMin = arr[lenArr] - 1;
            operations++;
        }

        if ( tempMin < min ) min = tempMin;

        if ( tempMin == min ) {
            lenArr--;
        }

    }

    return operations;
}

Thank you.

\$\endgroup\$

closed as off-topic by Peter Taylor, Toby Speight, Grajdeanu Alex., esote, Graipher Apr 11 at 14:44

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Authorship of code: Since Code Review is a community where programmers improve their skills through peer review, we require that the code be posted by an author or maintainer of the code, that the code be embedded directly, and that the poster know why the code is written the way it is." – Peter Taylor, Toby Speight, Grajdeanu Alex., esote, Graipher
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    \$\begingroup\$ This site is about giving feedback on code you've written, not explaining how code that someone else wrote works. I'm not sure that this question would be on topic in any site in the Stack Exchange network. \$\endgroup\$ – Peter Taylor Apr 11 at 7:54
  • \$\begingroup\$ Hi @PeterTaylor! Well code analysis and giving feedback on the thinking process is something I believe will be very much helpful like code review. And it is a part in the code review process. Hence, that is the reason for the question being asked here. \$\endgroup\$ – Anit Shrestha Manandhar Apr 11 at 8:06
  • \$\begingroup\$ Code review usually starts with working code. feedback on the [thought] process discusses whether its documentation in the source code makes it easy to understand - I guess the code presented is lacking, here. \$\endgroup\$ – greybeard Apr 11 at 8:18
  • \$\begingroup\$ Hi @greybeard! I do not understand your comment - " I guess the code presented is lacking"! The presented code is fully functional. The only question is the analysis on the code and asking - How the a data structure 2D array is used to solve the DP problem. This is a part of code analysis, any dev can ask in the code review process. Thanks. \$\endgroup\$ – Anit Shrestha Manandhar Apr 11 at 8:23
  • 1
    \$\begingroup\$ (In revision 7, this question looks OK for me (sort of - see end of comment) - if only the question of authorship didn't come up.) Please describe how each solution arrives at a result - in the code or, if outside, with an indication of which description pertains to which implementation. It may do your question good to tag with a language tag such as java. And adding some code to exercise your solutions: what is the result of the 2nd solution for the second example (10 7 12) from HackerRank? \$\endgroup\$ – greybeard Apr 12 at 8:19