Where have I gone wrong, in 'Prefix Sums' problem?

#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t–)
{
int n,sum=0,s=0;
cin>>n;
if(n%4==0)
{
cout<<“YES”<<endl;
int a[n/2],b[n/2];
// auto entering of digits using for loop
for(int i=0;i<n/2;i+=2)
{
a[i]=n-i;
a[i+1]=i+1;
}
for(int i=0;i<n/2;i++)
cout<<a[i]<<" “;
cout<<endl;
//array b.
for(int i=0;i<n/2;i+=2)
{
b[i]=n-i-1;
b[i+1]=i+2;
}
for(int i=0;i<n/2;i++)
cout<<b[i]<<” ";
cout<<endl;
}
else if(n%4!=0)
{
cout<<“NO”<<endl;
}
}
}

I challenge that I am right.

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

int find(int m, vector<pair<int,int>> q)
{
int mx = 0;
vector pre(5,0);

for (int i = 0; i < m; i++)
{
	// take input a and b
	int a = q[i].first, b = q[i].second;

	// add 100 at first index and
	// subtract 100 from last index

	// pre[1] becomes 100
	pre[a-1] += 100;
	
	// pre[4] becomes -100 and this
	pre[b] -=100;
	// continues m times as we input diff. values of a and b
}
for (int i = 1; i < 5; i++)
{
	// add all values in a cumulative way
	pre[i] += pre[i - 1];
	
	// keep track of max value
	mx = max(mx, pre[i]);
}

							
return mx;

}

// Driver Code
int main()
{

int m = 3;
vector<pair<int,int>> q = {{2,4},{1,3},{1,2}};


// Function call
cout<< find(m,q);
return 0;

}

That’s the right code.
Use that and your problem will gonna solved.
Good Luck !!

1 Like

thank you