1175C. Electrification - Codeforces Solution C++

  Problem Link : 1175C. Electrification 


✅ C++ Solution :

 
#include<bits/stdc++.h>
using namespace std;
 
#define ll long long int
 
int main()
{
  ll t;
  cin>>t;
  while(t--)
  {
    ll n,k,ind=-1,ans=pow(10,12);
    cin>>n>>k;
    k++;
    ll arr[n+1];
    for(ll i=1;i<=n;i++)
      cin>>arr[i];
      
 
    for(ll i=1;i+k-1<=n;i++)
    {
      ll tmp = (arr[i+k-1]+arr[i])/2;
      ll dis = max(tmp-arr[i],arr[i+k-1]-tmp);
      if(dis < ans)
      {
        ans=dis;
        ind = tmp;
      }
 
    }
 
    cout<<ind<<"\n";
 
 
  }
}

 

Thank you for your patience reading. If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Whatsapp or Facebook. 

😇Happy Learning!!