Advertisement
llsumitll

return_index_pair_sum_is_equal_to_target.cpp

Nov 15th, 2023
34
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.87 KB | Source Code | 0 0
  1. #include <vector>
  2. #include <iostream>
  3. using namespace std;
  4. /*
  5.  this fucntion will return the pair of index which addation is equal to target value,
  6.  is sum of the any pair of index is not matches with the target value <-1, -1> pair will be return as an info.
  7.  */
  8. pair<int , int> f(vector<int>& nums , int target)
  9. {
  10.     size_t size = nums.size();
  11.     for (int i= 0; i <= size ; i++)
  12.     {
  13.         for (int j = i ; j< size; j++)
  14.             if (nums[i] + nums[j] == target)
  15.                 return make_pair(i, j) ;
  16.     }
  17.     cout << "no sum is equal to the target number :" << target << endl;
  18.     return make_pair(-1,-1);
  19. }
  20.  
  21. int main()
  22. {
  23.     vector<int> n = {2,7,11,15, 55,33,36,89,45,75,65} ;
  24.     int target = 105;
  25.     pair<int, int> index_position = f(n, target);
  26.     cout << index_position.first << endl;
  27.     cout << index_position.second << endl;
  28.     return 0;
  29. }
  30.  
Advertisement
Comments
  • llsumitll
    1 year
    # text 0.11 KB | 0 0
    1. Input: nums = [2,5,7,13], target = 9 Output: [0,2] Explanation: Because nums[0] + nums[2] == 9, we return [0, 2].
Add Comment
Please, Sign In to add comment
Advertisement