➤ Problem Link : 1168A. Increasing by Modulo
✅ C++ Solution :
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
ll n,m;
ll func(ll val, ll arr[])
{
ll prev=0;
for(ll i=1;i<=n;i++)
{
if(arr[i]==prev)
continue;
if(arr[i]<prev)
{
if(prev-arr[i]>val)
return 0;
}
else
{
if(arr[i]+val<m || (arr[i]+val)%m < prev)
{
prev=arr[i];
}
}
}
return 1;
}
int main()
{
cin>>n>>m;
ll arr[n+1];
for(ll i=1;i<=n;i++)
cin>>arr[i];
ll low=0,high=m+1,mid;
while(low<high)
{
mid=low+(high-low)/2;
if(func(mid,arr))
high=mid;
else
low=mid+1;
}
cout<<low;
}
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!!
