622C. Not Equal on a Segment - Codeforces Solution C++

  Problem Link : 622C. Not Equal on a Segment 


✅ C++ Solution :

 
#include<bits/stdc++.h>
using namespace std;

#define ll long long int

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	int n,m;
	cin>>n>>m;
	int arr[n+1];
	for(int i=1;i<=n;i++)
		cin>>arr[i];
	int next[n+1];
	next[n]=-1;
	for(int i=n-1;i>0;i--)
	{
		if(arr[i+1]==arr[i])
			next[i]=next[i+1];
		else
			next[i]=i+1;
	}
	int l,r,x;
	while(m--)
	{
		cin>>l>>r>>x;
		if(arr[l]!=x)
			cout<<l<<"\n";
		else if(next[l]!=-1 && next[l]<=r && arr[next[l]]!=x)
			cout<<next[l]<<"\n";
		else
			cout<<"-1\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!!