Problem in declaring vector,Runtime error

The link to my code is http://ideone.com/mwwBjl

Why am i getting runtime error. the above code is solution of question https://www.interviewbit.com/problems/smallest-multiple-with-0-and-1/

Thanks in advance.

the problem is in your multiple function not in declaration…
if you try to run your main only by commenting your multiple function you will see the output “in main”

  1. Your multiple function returns an std::string but you haven’t used the return value. Though this is not the reason of the runtime error, still it will give a warning.

  2. In line number 16 and 18, you are doing flag[val] = 1, where val = 1 according to your code. However, your vectors are still of size 0, so you are accessing memory out of bounds, which gives you RTE.

Have a look at this code of yours. I have removed the RTE, but have not touched your solution: Corrected code

on dev c++, I could even see some chk0 printed.chk0 which are before the assignment flag[val]=1 get printed and after that code stops working.

Got it. Thanks a lot.

#include <bits/stdc++.h>

using namespace std;

int main () {
int t;
cin >>t;
while(t–) {
long long int n,k;
cin >>n>>k;
vector< int> v(k+1);
for(long long int i =0;i<n;i++) {
long long int x;
cin >>x;
v[k - x%k]++;
}
pair <long long int ,long long int> p = {0,0};
for (long long int i=1;i<k;i++) {
if(p.first < v[i]) {
p.first = v[i];
p.second = i;
}
else if (p.first ==v[i]) {
p.second =i;
}
}
if(p.first ==0) cout <<“0” <<endl;
else cout << p.first * k -(k-p.second) + 1<<endl;
}
}

why this code is giving runtime error in testcase 5.
https://codeforces.com/problemset/problem/1374/D