Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
std::vector<int>nums = { 1,2,3,4,5 };
int target = 7 ;
for (int i = 0; i < nums.size()-1; i++)
{
for (int j = i + 1; j <= nums.size(); i++)
{
if (nums[i]+nums[j]==target)
{
cout << " [ " << i << " ," << j-1 << " ] ";
}
}
}
return 0;
}
When I run the program I have two errors:
Debug Assertion Failed , Expression:vector subscript out of range.
Unhandled exception at 0x7938E906 (ucrtbased.dll) in problems.exe: An invalid parameter was passed to a function that considers invalid parameters fatal. ( at line 18 --if(nums[i]+nums[j]==target))
Can someone help me, please?