Skip to content

Commit

Permalink
chore: time complexity nlogn
Browse files Browse the repository at this point in the history
  • Loading branch information
Ankur-Dwivedi22 committed Mar 26, 2024
1 parent cb5792a commit a17e62b
Showing 1 changed file with 41 additions and 22 deletions.
63 changes: 41 additions & 22 deletions medium/day_5/solution.cpp
Original file line number Diff line number Diff line change
@@ -1,31 +1,50 @@
//Write your code here
#include<bits/stdc++.h>
// Write your code here
#include <bits/stdc++.h>
using namespace std;

#define int long long

int32_t main(){
int n , sum=0;
cin>>n;
vector<int> a(n) , ans;
for(int i=0; i<n; i++) {cin>>a[i]; sum += a[i];}
for(int i=0; i<n; i++){
int k = sum - a[i];
for(int j=0; j<n; j++){
if(j!=i && 2*(a[j]) == k){
ans.push_back(i+1);
break;
int32_t main()
{
int n, sum = 0;
cin >> n;
vector<int> a(n), ans;
vector<pair<int, int>> 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<<ans.size()<<endl;
for(auto it : ans){
cout<<it<<" ";
sort(ans.begin(), ans.end());
cout << ans.size() << "\n";
for (auto it : ans)
{
cout << it << " ";
}
cout<<endl;
}
else{
cout<<0<<endl;
cout << "\n";
}
return 0;
}

0 comments on commit a17e62b

Please sign in to comment.