From a17e62bdffc4cf4f6aaa134c4e71575a5fd54c6c Mon Sep 17 00:00:00 2001 From: Ankur-Dwivedi22 Date: Tue, 26 Mar 2024 20:04:08 +0530 Subject: [PATCH] chore: time complexity nlogn --- medium/day_5/solution.cpp | 63 +++++++++++++++++++++++++-------------- 1 file changed, 41 insertions(+), 22 deletions(-) diff --git a/medium/day_5/solution.cpp b/medium/day_5/solution.cpp index 2eaac7b..28b0c02 100644 --- a/medium/day_5/solution.cpp +++ b/medium/day_5/solution.cpp @@ -1,31 +1,50 @@ -//Write your code here -#include +// Write your code here +#include using namespace std; #define int long long -int32_t main(){ - int n , sum=0; - cin>>n; - vector a(n) , ans; - for(int i=0; i>a[i]; sum += a[i];} - for(int i=0; i> n; + vector a(n), ans; + vector> vp; + for (int i = 0; i < n; i++) + { + cin >> a[i]; + sum += a[i]; + vp.push_back({a[i], i}); + } + if (n <= 2) + { + cout << 0 << endl; + } + else + { + sort(vp.begin(), vp.end()); + for (int i = 0; i < n; i++) + { + if (i != n - 1) + { + int k = sum - vp[i].first; + if (2 * vp[n - 1].first == k) + ans.push_back(vp[i].second + 1); + } + else + { + int k = sum - vp[i].first; + if (2 * vp[n - 2].first == k) + ans.push_back(vp[i].second + 1); } } - } - if(ans.size()){ - cout<