From a731c380d30158dae0c9f3b9bfb667a876caf131 Mon Sep 17 00:00:00 2001 From: bhavyagera10 <50918751+bhavyagera10@users.noreply.github.com> Date: Thu, 16 Apr 2020 03:10:17 +0530 Subject: [PATCH] Update Equal.cpp Proposed a shorter solution and easier solution. --- Hashing/Equal.cpp | 131 +++++++++++++++++++++++++++++----------------- 1 file changed, 83 insertions(+), 48 deletions(-) diff --git a/Hashing/Equal.cpp b/Hashing/Equal.cpp index c02b844..fe25916 100644 --- a/Hashing/Equal.cpp +++ b/Hashing/Equal.cpp @@ -1,55 +1,90 @@ -// https://www.interviewbit.com/problems/equal/ +// // https://www.interviewbit.com/problems/equal/ -vector Solution::equal(vector &A) { - // Do not write main() function. - // Do not read input, instead use the arguments to the function. - // Do not print the output, instead return values as specified - // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details +// vector Solution::equal(vector &A) { +// // Do not write main() function. +// // Do not read input, instead use the arguments to the function. +// // Do not print the output, instead return values as specified +// // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details - unordered_map > myMap; - vector sol; - vector temp; +// unordered_map > myMap; +// vector sol; +// vector temp; - int count = 0; +// int count = 0; - for(int i = 0; i < A.size()-1; i++){ - for(int j = i+1; j < A.size(); j++){ - int sum = A[i] + A[j]; - if(myMap.find(sum) == myMap.end()){ - myMap[sum].push_back(i); - myMap[sum].push_back(j); - } - else{ - temp.push_back(myMap[sum][0]); - temp.push_back(myMap[sum][1]); - temp.push_back(i); - temp.push_back(j); - for(int a = 0; a < temp.size()-1; a++){ - for(int b = a+1; b < temp.size(); b++){ - if(temp[a] == temp[b]){ - goto COND; - } - } - } - count++; - if(count == 1){ - sol = temp; - } - else{ - for(int z = 0; z < sol.size(); z++){ - if(sol[z] > temp[z]){ - sol = temp; - break; - } - else if(sol[z] < temp[z]){ - break; - } - } +// for(int i = 0; i < A.size()-1; i++){ +// for(int j = i+1; j < A.size(); j++){ +// int sum = A[i] + A[j]; +// if(myMap.find(sum) == myMap.end()){ +// myMap[sum].push_back(i); +// myMap[sum].push_back(j); +// } +// else{ +// temp.push_back(myMap[sum][0]); +// temp.push_back(myMap[sum][1]); +// temp.push_back(i); +// temp.push_back(j); +// for(int a = 0; a < temp.size()-1; a++){ +// for(int b = a+1; b < temp.size(); b++){ +// if(temp[a] == temp[b]){ +// goto COND; +// } +// } +// } +// count++; +// if(count == 1){ +// sol = temp; +// } +// else{ +// for(int z = 0; z < sol.size(); z++){ +// if(sol[z] > temp[z]){ +// sol = temp; +// break; +// } +// else if(sol[z] < temp[z]){ +// break; +// } +// } +// } +// COND:temp.erase(temp.begin(), temp.end()); +// } +// } +// } + +// return sol; +// } + + +vector Solution::equal(vector &A) { + map>mp; + int n=A.size(); + vector>vr; + + for(int i=0;i v; + return v; } +