Skip to content

Commit

Permalink
added explanation and soultion code
Browse files Browse the repository at this point in the history
  • Loading branch information
Sarvesh0955 committed Dec 16, 2024
1 parent 3d5c650 commit ed75593
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Debugs-and-Optimizations/Task1/explanation.txt
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
6 3
1 5 3 4 2 6

Expected Output : 5
Code Output : 4

The given test case gives wrong output on provided code.

Reason:
The code does not cover all possible coverage of range removal. In given code we can remove the subarray [3,4,2] which leads to final array to be [1,5,6] whose median is 5 largest possible, while the given solution does not considers this case, it checks for final array [1,5,3] and [4,2,6] only.
66 changes: 66 additions & 0 deletions Debugs-and-Optimizations/Task1/solution.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,67 @@
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define sz(x) ((long long)(x).size())
#define vl vector<ll>

#include<ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_multiset;
// *s.find_by_order, *s.order_of_key --> O(logn)
void myerase(indexed_multiset &t, int v){
int rank = t.order_of_key(v);
indexed_multiset::iterator it = t.find_by_order(rank);
t.erase(it);
}

ll n,k;
int ans;

void func(ll i,vl &a,indexed_multiset &s){
if(i>=sz(a)) return;
if(i==sz(a)-1)
{
s.insert(a[i]);
ll n=sz(s);
ans = max(ans,*s.find_by_order((n+1)/2 -1));
myerase(s,a[i]);

if(k==1){
n=sz(s);
ans = max(ans,*s.find_by_order((n+1)/2 -1));
}
return;
}

//choose cur
s.insert(a[i]);
func(i+1,a,s);
myerase(s,a[i]);

//choose after removing
func(i+k,a,s);
}

void solve()
{
cin>>n>>k;
vl a(n);
for(ll i=0;i<n;i++){
cin>>a[i];
}
indexed_multiset s;
func(0,a,s);
cout<<ans<<endl;
}

int main()
{
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);

int T=1;
// cin>>T;
while(T--)
solve();

return 0;
}

0 comments on commit ed75593

Please sign in to comment.