a
is declared a an empty vector so you cannot access elements of a
unless there are any elements in a
.
You want
a.push_back(nums[i]);
instead of
a[k] = nums1[i]; //a is declared as empty vector so you have to push elements to it.
OR
you can do
vector<int> a(num1.size());
instead of
vector<int> a;
solved why does this code gives runtime error?