Problem Code: STUPMACH

Here’s the link to the problem: CodeChef: Practical coding for everyone
here’s my solution: CodeChef: Practical coding for everyone
I used recursion in this problem, used the correct approach and passed all the imaginable test cases.
But still WA. Can someone please provide some test cases which fails my solution else please tell me what’s wrong with the code.

Pay attention to compiler warnings:

[simon@simon-laptop][20:19:44]
[~/devel/hackerrank/otherpeoples]>./compile-latest-cpp.sh 
Compiling kaizen01-STUPMACH.cpp
+ g++ -std=c++14 kaizen01-STUPMACH.cpp -O3 -g3 -Wall -Wextra -Wconversion -DONLINE_JUDGE -D_GLIBCXX_DEBUG -fsanitize=undefined -ftrapv
kaizen01-STUPMACH.cpp: In function ‘long long int recursion(long long int*, int)’:
kaizen01-STUPMACH.cpp:33:18: warning: control reaches end of non-void function [-Wreturn-type]
         recursion(capacity,smallest_address);
         ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
+ set +x
Successful
[simon@simon-laptop][20:19:49]
[~/devel/hackerrank/otherpeoples]>echo "1                 
3
2 1 3" | ./a.out
kaizen01-STUPMACH.cpp:9:4: runtime error: execution reached the end of a value-returning function without returning a value

There may be other issues, but fix that one first :slight_smile:

But it’s running fine on the codechef compiler.

i wanted to ask that how can i ask a question on codechef?

i wanted to ask that how can i ask a question on codechef…like please tell me how did you ask this question?

Just go on codechef discuss → click ’ New Topic ’ → ask the question.

:man_shrugging:

Consider the testcase:

2                                                                 
3
2 1 3
3
2 1 3
2 Likes

@kaizen01 Try if the following works

Your Code, modified
#include <bits/stdc++.h>
using namespace std;
#define ll long long





ll recursion(ll capacity[], int n, ll totalTokens){
    
    
    int smallest_address=INT_MAX;
    ll smallest_capacity=INT_MAX;
       
    if(n==0) 
    {
        return totalTokens+capacity[0];
    }
    
    for(int i=0; i<n; i++)                           
    {
        if(capacity[i]<smallest_capacity){
            smallest_capacity=capacity[i];
            smallest_address=i;
        }
    } 
    
    for(int i=0; i<n; i++)
    {
        capacity[i]=capacity[i]-smallest_capacity;
    }
        totalTokens=totalTokens+(ll)n*smallest_capacity;    //size of var = S(i)*n
    
        recursion(capacity,smallest_address, totalTokens);
    
}

int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    //your code goes here
    
    int t; cin>>t;
    while(t--){                         //O(t)
        int n; cin>>n;
        ll capacity[n]{};
        for(int i=0; i<n; i++)          //O(n)
            cin>>capacity[i];
            
        cout<<recursion(capacity,n,0)<<endl;
        
    }
  
    return 0;
}
1 Like

thankyou so much boss

1 Like

you’re a legend, thanks