Hello There,
I was solving the CSES problem Sum of Two Values
. it is the same as like leet code problem named Two Sum
.
I code it that way, buuuuut.
I got TLE. Then I checked USACO and then I copy pasted the code given on the website and it worked. although they are the same.
My code:
#include <bits/stdc++.h>
typedef long long ll;
#define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL)
#define f(i, a, b) for (int i = a; i < b; i++)
#define vi vector<int>
#define vl vector<ll>
#define vc vector<char>
#define vs vector<string>
#define inp cin >>
#define out cout <<
#define mii map<int, int>
#define mll map<ll, ll>
#define mci map<char, int>
#define mcc map<char, char>
#define pll pair<ll,ll>
#define uset unordered_set<ll>
#define sset set<ll>
const ll N = 2e5+10;
using namespace std;
int main(){
fast_cin();
ll n,x;
cin >> n >> x;
vector<int> v(n);
unordered_map<int,int> map;
for(int i=0;i<n;i++){
cin >> v[i];
}
for(int i=0;i<n;i++){
int get = x - v[i];
if(map.count(get)){
cout << i+1 << " " << map[get]+1 << endl;
return 0;
}
map[v[i]] = i;
}
cout << "IMPOSSIBLE" << endl;
}
USACO code:
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, x;
cin >> n >> x;
vector<int> values(n);
for (int i = 0; i < n; i++) { cin >> values[i]; }
// use a map to avoid using a very large array
map<int, int> val_to_ind;
for (int i = 0; i < n; i++) {
// target minus a number is the other number
if (val_to_ind.count(x - values[i])) {
cout << i + 1 << " " << val_to_ind[x - values[i]] << endl;
return 0;
}
val_to_ind[values[i]] = i + 1;
}
cout << "IMPOSSIBLE" << endl;
}
Can Someone Explain me, What the heck in happening here?