-2

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?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770

2 Answers2

1

There are 2 mistakes in the second for loop in your code:

  1. The condition part should be j < nums.size().
  2. the increment operation should be j++ instead of i++.

Also, while printing the final indices, print j, instead of j-1.

Have a look at the following implementation:

#include <iostream>
#include <vector>

int main()
{
    std::vector<int>nums = { 1,2,3,4,5 };
    int target =  7 ;

    for (std::size_t i = 0; i < nums.size()-1; i++)
    {
        for (std::size_t j = i + 1; j < nums.size(); j++)
        {
            if (nums[i]+nums[j]==target)
            {
                std::cout << " [ " << i << " ," << j << " ] ";
            }
        }
    }

    return 0;
}

Output:

 [ 1 ,4 ]  [ 2 ,3 ] 
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Deepak Tatyaji Ahire
  • 4,883
  • 2
  • 13
  • 35
  • Unhandled exception at 0x7BCDE906 (ucrtbased.dll) in problems.exe: An invalid parameter was passed to a function that considers invalid parameters fatal at if.. – user11908629 Nov 27 '20 at 07:13
  • I 'm sorry,but i am beginner in programming – user11908629 Nov 27 '20 at 07:13
  • i have also 2 warnings (c4018) Warning C4018 '<': signed/unsigned mismatch at line 11 and 12 – user11908629 Nov 27 '20 at 07:15
  • i checked , and it is this error Unhandled exception at 0x7BCDE906 (ucrtbased.dll) in problems.exe: An invalid parameter was passed to a function that considers invalid parameters fatal. I use VS 2019 . i checked also in c++ online compiler and it is fine. – user11908629 Nov 27 '20 at 07:18
  • Can you try to update the Visual Studio 2019 and reinstall the C++ compiler and then try? – Deepak Tatyaji Ahire Nov 27 '20 at 07:20
  • 1
    Please avoid C style casts and use C++ static_cast or appropriate variable types. – Thomas Sablik Nov 27 '20 at 07:45
  • It is working , but why i should convert nums.size into int? – user11908629 Nov 27 '20 at 07:48
  • 1
    @user11908629 You shouldn't. Your `for` loops should be `for (std::size_t i = 0; i + 1 < nums.size(); ++i)` and `for (std::size_t j = i + 1; j < nums.size(); ++j)` – Thomas Sablik Nov 27 '20 at 08:02
  • using a c style cast to silence a warning is not good, because sometimes it really just silences the warning. If the warning was pointing to a real problem then that problem may be still there (not the case here, but c-style casts are to be avoided in general) – 463035818_is_not_an_ai Nov 27 '20 at 08:31
  • 1
    When `nums.size()` is 0 this will cause a bug `nums.size()-1`. – Thomas Sablik Nov 27 '20 at 08:44
-1
class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        seen = {}
        for j, num in enumerate(nums):
            i = seen.get(target-num, -1)
            if i != -1:
                return [i, j]
            seen[num] = j
        return [-1, -1]
Blastfurnace
  • 18,411
  • 56
  • 55
  • 70