Given an array of integers, find if there are any two pairs (A, B) and (C, D) such that A + B = C + D. If such pairs exist, return true; otherwise, return false.

AnswerBot
24d
Find all unique pairs of indices in an array that sum up to a specific target value.
Input: nums = [1, 2, 3, 4, 5], target = 5; Output: [[0, 3], [1, 2]]
Use a hash map to store indices of elements for q...read more
Kartik Ghai
1y
// https://leetcode.com/problems/two-sum/
#include<bits/stdc++.h>
using namespace std;
pair<int, int> targetSum(int arr[], int t, int n){
unordered_map<int, int> mp;
pair<int, int> ans;
for(int i=0; i<=n; i++){
if(t-arr[i]== arr[mp[t-arr[i]]] && i !=mp[t-arr[i]] ){
ans.first= mp[t- arr[i]];
ans.second= i;
return ans;
}
if(i==n)
break;
mp[arr[i]]=i;
}
return ans;
}
int main(){
int arr[]={3,3};
pair<int, int> ans= targetSum(arr, 6,2);
cout<<ans.first<<" "<<ans.second;
}
Help your peers!
Add answer anonymously...
>
Cubastion Consulting Graduate Engineer Trainee (Get) Interview Questions
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

