Bank X contains a database of account numbers and debit cards mapped to each other. Now due to some technical issues, the data suddenly got deleted. But now the bank is left with a list of pairs of integers but they don’t know which of them is the debit card no. and which one is the account number.
It is clear that an account can have multiple debit cards linked to it, but it is not possible that a debit card is linked to more than one accounts.
You need to check if the given data is recoverable or not. If it is consistent then you need to also find what is the account number and what is the debit card number. It is guaranteed that the input data is designed in a way that there is only one unique correct answer.
If for a pair it is impossible to find, you need to print -1 -1.
Example:
Input: v = {{1000, 2000}, {1500, 2000}, {1400, 1600}, {2000, 18000}, {-1,-1}}
Output:
0 1 0 1 -1 -1 0 1 1 0
Approach:
C++
#include <bits/stdc++.h>using namespace std;void bankDataConsistency(vector<pair<int, int>> &v){vector<int> arr, brr;map<int, int> mp;int n = 0;for (int i = 0; i < v.size(); i++){mp[v[i].first]++;arr.push_back(v[i].first);brr.push_back(v[i].second);mp[v[i].second]++;n++;}for (int i = 0; i < n; i++){if (mp[arr[i]] == 1 && mp[brr[i]] == 1)cout << -1 << " " << -1 << "\n";else if (arr[i] > 1)cout << 0 << " " << 1 << "\n";elsecout << 1 << " " << 0 << "\n";}}int main(){vector<pair<int, int>> v = {{1000, 2000},{1500, 2000},{1400, 1600},{2000, 18000},{-1,-1}};bankDataConsistency(v);return 0;}
No comments:
Post a Comment