-1

When i run the source code on sample cases of their example it runs fine but when I submit the question It says runtime error. Here is my Source code and link of the question. Question link - https://www.codechef.com/SEPT21C/submit/MNDIGSM2 Below is the code.

#include <iostream>
#include <vector>
// #include <bits/stdc++.h>
using namespace std;
// #define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

int converter(int n , int b){
    vector<int> vec;
    int sum = 0;
    while(n>0){
        vec.push_back(n%b);
        n = n / b;
        }
    int vecSize = vec.size();
        for(int i = 0;i<vecSize;i++){
            // cout<<
            sum = sum + vec[i];
        }
        return sum;
}
int minVal(vector<int> arr , int len){
    int min =  arr[0], c = 0;
    // if(arr)
    for(int i = 1 ; i< len;i++){
        if (arr[i] < min){
        min = arr[i];
        c = i;
        }
    }
    return c;
    
}
int main() {
    // your code goes here
    // fast;
    int test;
    cin>>test;
    while(test--){
        int n ,r;
        cin>>n>>r;
        int l = 2;
        // ll copy = l;
        int arSize = (r-2)+1;
        vector<int> arr(arSize);
        for(int i = 0;i< arSize ;i++){
            arr[i] = converter(n,l);
            l++;
        }
        int tobe = minVal(arr , arSize);
        cout<<tobe + 2<<endl;
    }
 
    return 0;
}
  • And when you used your debugger to run this program, one line at a time, step by step, what did you see? – Sam Varshavchik Sep 04 '21 at 19:03
  • this code is of a contest, There was no debugger on the platform but I debugged it using Vs code and it says thread exited with code 0. – 78_Harsh Prasad Sep 04 '21 at 19:13
  • @78_HarshPrasad `int arr[arSize];` -- This is not valid C++. Arrays in C++ must have their size denoted by a compile-time expression, not a runtime value. Use `std::vector arr(arSize);`, since you are already using `std::vector`. Then this: `typedef long long int ll;` -- There is no need for this obfuscation in C++. In C++, you have `int64_t`, so if you have an issue typing `long long int ` everywhere, use the shorter, more descriptive type of `int64_t`. Then you have the `#include ` non-standard header, when all you need is ``, `` – PaulMcKenzie Sep 04 '21 at 19:32
  • Also, that "codechef" link goes to a blank screen with no question. Also going back to `int arr[arrSize]`, if `arrSize` is large enough, it will exhaust the stack memory. That's another reason why you should change all of those fake arrays to `std::vector`, where the stack memory exhaustion doesn't become an issue. – PaulMcKenzie Sep 04 '21 at 19:41
  • 3
    This "contest", as you call it, is designed for people who are already experienced C++ programmers, and who have some spare time to spend on solving random coding puzzles. They are not for people who are trying to learn C++. C++ is the most complicated general purpose programming language in use today, and the only way to learn it is with a [good textbook](https://stackoverflow.com/questions/388242/) that explains C++'s fundamentals as part of an organized, methodical, study course that teaches C++ and explains how to use C++ tools such as debuggers. – Sam Varshavchik Sep 04 '21 at 19:44
  • 1
    @78_HarshPrasad Just the fact that you include `` in one context, but didn't use it for what `vector` is intended for in the `int arr[arrSize]`, gives the impression that you're just copying poor coding habits without knowing exactly what you're actually copying. The reason for runtime errors should *never* be caused by bad coding due to not knowing the C++ language properly, but I wouldn't be surprised if this is the case here. – PaulMcKenzie Sep 04 '21 at 19:51
  • Can you say more about the runtime error and what you've done to try to diagnose it? – Paul Floyd Sep 09 '21 at 08:12
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Sep 09 '21 at 10:26
  • Please NEVER post here code that expects some input values that are read by `std::cin` without providing exemplary input files that mimic the required input or without explaining in *great detail* how this input is to be constructed. Otherwise you're wasting time of many, many people that wish you well. – zkoza Sep 09 '21 at 10:31

1 Answers1

1

Maybe I do not understand the question fully. There is not enough information available.

It could be that the program slows down because the usage of the std::vector. First you calculate, then store the values and then iterate again over all values.

This is not necessary. You can do all calculations inline without the need for additional storage.

And, additionally, all these "contest" questions do not have the intention, to improve your programming skills.

Basically the language doesn't matter. The important thing is the algorithm. They want you to find a "good" algorithm.

Bruteforcing is nearly never a feasible solution. If you read about big numbers like 10^12, then you know already in advance that you will get a TLE with the brute force solution.

Regarding this horrible and non compliant C++ slang that is used on this "competetion" sides, please note that this is nearly never necessary. You have no time contraints to submit a solution. So, you could use also real C++ code.

Anyway. I corrected your code and added meaningful varibale names, comments and formatting. So, logically, the approach is the same, but it is readable.

Of course it may fail as well, because it is still brute forcing . . .

#include <iostream>
#include <limits>

constexpr unsigned long long BaseStart = 2ull;

int main() {

   // Get number of test cases
   unsigned int numberOfTestCases{};
   std::cin >> numberOfTestCases;

   // Work on all test cases 
   for (unsigned int testCase{}; testCase < numberOfTestCases; ++testCase) {

       // Get the value to check and the upper limit for the base 
       unsigned long long valueToCheck{}, upperLimitBase{};
       std::cin >> valueToCheck >> upperLimitBase;

       // Here we will store the minimum sum to check
       unsigned long long minimumsumOfDigits{std::numeric_limits<unsigned long long>::max()};

       // Here we will store the result
       unsigned long long minimumBase{};

       for (unsigned long long base{BaseStart}; base <= upperLimitBase; ++base) {

           // And this will be the running sumOfDigits
           unsigned long long runningSumOfDigits{};

           // get the digits of the value and calculate the running sum
           unsigned long long value{valueToCheck};
           
           while (value > 0) {

               // Get digits via modulo division and add up
               runningSumOfDigits += value % base;
               value /= base;
           }
           
           // Get current minimum
           if (runningSumOfDigits < minimumsumOfDigits) {
               minimumsumOfDigits = runningSumOfDigits;
               minimumBase = base;
           }
        }
        std::cout << minimumBase << '\n';
    }
    return 0;
}

This code can of course be optimized further, but for this I would need more information . . .

A M
  • 14,694
  • 5
  • 19
  • 44
  • thankyou so much , the link of the question might not be opening because it requires sign in. Anyway the question says it will provide you with a number and an upper limit (x).You have to calculate the sum of all the digits of number in every base from 2 to x. and return the base with minimum sum. For eg say n = 216 x = 5 . Then f(216,2)=f(216,3)=4 , f(216,4)=6, f(216,5)=8, here sum of digit with base 3 and 2 is minimum hence anyone of this can be a answer. I will implement your solution and learn . Thankyou – 78_Harsh Prasad Sep 05 '21 at 15:44
  • Good answer deserves an upvote, even though the question is somewhat sloppy. – zkoza Sep 09 '21 at 10:47
  • If you (= OP) post the contents problem, always quote it verbatim. The problem may be hidden in a tiny detail that you consider unimportant. Here @Armin_Montigny used 64-bit integers, which suggests that the problem has constraints that may exceed the range of integers. In this case the bug is in your too narrow datatypes, that may lead to division by zero. But this can be known for sure only when the full, complete information is available. – zkoza Sep 09 '21 at 10:53