Actually I think problem statement is somewhat misleading. No need to mention range [L, R] @ all.
The intention is a variation to "largest Rectangle" which are a classic stack problem on Leetcode.
But you need to run largest Rectangle twice:increased and decreased.
#include <cmath>#include<stack>#include<cstdio>#include<vector>#include<iostream>#include<algorithm>using namespacestd;intMain () {intN CIN >>N; Vector<int>inch(n); for(inti =0; I < n; i + +) Cin>>inch[i]; intRET =0; { inch. push_back (0); Stack<int>Stk; for(inti =0; I <inch. Size (); i++) { if(Stk.empty () | |inch[I] >Stk.top ()) {Stk.push (inch[i]); } Else { intLastv =stk.top (); Stk.pop (); if(!Stk.empty ()) {ret= Std::max (ret, (lastv ^stk.top ())); //cout << Lastv << "-" << stk.top () << "=" << (lastv ^ stk.top ()) << Endl;} i--; } } inch. Pop_back (); } {Stack<int>Stk; inch. Insert (inch. Begin (),0); for(inti =inch. Size ()-1; I >=0; I--) { if(Stk.empty () | |inch[I] >Stk.top ()) {Stk.push (inch[i]); } Else { intLastv =stk.top (); Stk.pop (); if(!Stk.empty ()) {ret= Std::max (ret, (lastv ^stk.top ())); //cout << Lastv << "-" << stk.top () << "=" << (lastv ^ stk.top ()) << Endl;} i++; } }} cout<< ret <<Endl; return 0;}
View Code
Hackerrank "and Xor OR"