jayati

Two sum(O(n^2))\

Sep 13th, 2022
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. class Solution {
  2. public:
  3. vector<int> twoSum(vector<int>& nums, int target) {
  4. int sum=target;
  5. vector<int> v;
  6. int n=nums.size();
  7. int i,j;
  8. int f=0;
  9. for(i=0;i<n;i++)
  10. {
  11. j=i+1;
  12. while(j<n)
  13. {
  14. int s=(nums[i]+nums[j]);
  15. if(s==target)
  16. {
  17. v.push_back(i);
  18. v.push_back(j);
  19. f=1;
  20. }
  21. j++;
  22. }
  23. if(f)
  24. {
  25. break;
  26. }
  27.  
  28. }
  29. return v;
  30.  
  31. }
  32. };
Advertisement
Add Comment
Please, Sign In to add comment