Lang: C++, Complexity[Time, Space]: [O(n), O(1) if returning array isn't considered]


#1

Relatable comments

vector<int> Solution::solve(int A, vector<int> &B) 
{
    // Index of minimum value in array -: O(n)
    int n=B.size(), mnIdx=min_element(B.begin(),B.end())-B.begin(); 

    // Max number of kicks friends in vector B can give, without killing him
    int kicks=A/B[mnIdx]; 
    vector<int> ans(kicks, mnIdx);
    
    // Hence Least pain they can give
    // This is actually sum of vector ans
    int pain=B[mnIdx]*kicks; 
    
    
    // Iterate till mnIdx & find if pain can Increase, given that no. of kicks are fixed 
    for(int i=0,j=0; i<mnIdx && j<kicks;) // -: O(n)
    {
        int temp = pain-B[mnIdx]+B[i];

        if(pain<temp && temp<=A) // If temp lies in (pain, A]
        {
            pain = temp;
            ans[j] = i;
            j++;
        }
        else
            i++;
    }
    
    return ans;
}

#2

Your Solution is wrong as it always returns a vector of size A / B[minIndex] even when not needed. Consider the testcase : A : 10, B : [ 10, 8, 6, 5, 4 ].
Solution is clearly [ 0 ] but your code returns [2, 4] which is not lexicograhically smaller.


#3

The number of kicks have to be maximised, read the question carefully. [2,4] is the correct answer for the given testcase.