I solved using Segment Tree but got an WA .
Code Seems to work right .
Anybody can help me with the mistakes ?
#include <iostream>
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define pii pair<ll,ll>
#define vi vector<ll>
#define all(a) (a).begin(),(a).end()
#define endl '\n'
#define rep(i,a,b) for(long long i=a;i<b;i++)
#define repr(i,a,b) for(long long i=a;i>=b;i--)
using namespace std;
long long getMid(long long s, long long e)
{
return s + (e - s) / 2;
}
/* A recursive function to get the sum of
values in given range of the array.
The following are parameters for this
function.
st -> Polong longer to segment tree
node -> Index of current node in
the segment tree .
ss & se -> Starting and ending indexes
of the segment represented
by current node, i.e., st[node]
l & r -> Starting and ending indexes
of range query */
long long MaxUtil(long long* st, long long ss, long long se, long long l,
long long r, long long node)
{
// If segment of this node is completely
// part of given range, then return
// the max of segment
if (l <= ss && r >= se)
return st[node];
// If segment of this node does not
// belong to given range
if (se < l || ss > r)
return -1;
// If segment of this node is partially
// the part of given range
long long mid = getMid(ss, se);
return max(MaxUtil(st, ss, mid, l, r,
2 * node + 1),
MaxUtil(st, mid + 1, se, l,
r, 2 * node + 2));
}
long long getMax(long long* st, long long n, long long l, long long r)
{
// Check for erroneous input values
if (l < 0 || r > n - 1 || l > r)
{
printf("Invalid Input");
return -1;
}
return MaxUtil(st, 0, n - 1, l, r, 0);
}
// A recursive function that constructs Segment
// Tree for array[ss..se]. si is index of
// current node in segment tree st
void updateValue(long long arr[], long long* st, long long ss, long long se,
long long index, long long value, long long node)
{
if (index < ss || index > se)
{
cout << "Invalid Input" << endl;
return;
}
if (ss == se)
{
// update value in array and in segment tree
arr[index] = value;
st[node] = value;
}
else {
long long mid = getMid(ss, se);
if (index >= ss && index <= mid)
updateValue(arr, st, ss, mid, index,
value, 2 * node + 1);
else
updateValue(arr, st, mid + 1, se,
index, value, 2 * node + 2);
st[node] = max(st[2 * node + 1],
st[2 * node + 2]);
}
return;
}
long long constructSTUtil(long long arr[], long long ss, long long se,
long long* st, long long si)
{
// If there is one element in array, store
// it in current node of segment tree and return
if (ss == se)
{
st[si] = arr[ss];
return arr[ss];
}
// If there are more than one elements, then
// recur for left and right subtrees and
// store the max of values in this node
long long mid = getMid(ss, se);
st[si] = max(constructSTUtil(arr, ss, mid, st,
si * 2 + 1),
constructSTUtil(arr, mid + 1, se,
st, si * 2 + 2));
return st[si];
}
/* Function to construct segment tree from given array.
This function allocates memory for segment tree.*/
long long* constructST(long long arr[], long long n)
{
// Height of segment tree
long long x = (long long)(ceil(log2(n)));
// Maximum size of segment tree
long long max_size = 2 * (long long)pow(2, x) - 1;
// Allocate memory
long long* st = new long long[max_size];
// Fill the allocated memory st
constructSTUtil(arr, 0, n - 1, st, 0);
// Return the constructed segment tree
return st;
}
int main()
{
// ios::sync_with_stdio(0);
// cin.tie(0);
// cout.tie(0);
long long tc;
cin>>tc;
while(tc--)
{
long long n,k;
cin>>n>>k;
long long arr[n];
long long arrc[n];
for(long long i=0;i<n;i++)
{
cin>>arr[i];
arrc[i]=arr[i];
}
long long* st = constructST(arr, n);
long long max_size=0;
for(long long a=0;a<n;a++)
{
//arr=arrc;
copy(arrc, arrc + n, arr);
long long* st = constructST(arr, n);
for(long long b=a;b<n;b++)
{
copy(arrc, arrc + n, arr);
long long* st = constructST(arr, n);
long long val =getMax(st, n, a, b) ;
if(val>k)
{
for(long long i=a;i<=b;i++)
{
if(arr[i]==val)
{
updateValue(arr, st, 0, n - 1, i, -1, 0);
}
}
long long val1 =getMax(st, n, a, b) ;
if(val1<=k && val1!=-1)
{
// cout<<"yeyeye"<<endl;
// cout<<a+1<<" "<<b+1<<endl;
// cout<<val<<" "<<val1<<endl;
max_size=max(max_size,(b-a+1));
// cout<<max_size<<endl;
}
}
}
}
cout<<max_size<<endl;
}
}