GETTING Time Limit Exceeded Error for some Cases ,what am i missing or any corner case

Election for a party in different phase and states

Sample TestCase 1

Input

phase=3

P[0]=2 P[1]=1 P[2]=0

state=2

S[0]=1 S[1]=2

similarly another ex:

3 3

3 2 1

1 2 2

Output

YES NO

Explanation

Test Case 1:

As per the theory, to form the government,party should win 2 seats in the first phase, 1 seat in second phase and 0 in last phase.party also needs to follow the number of wins in states column wise. There should be a single win in the seats of first state and 2 wins in the seats of second state.

My Attempt Code:

/*Enter your code here. Read input from STDIN. Print your output to STDOUT*/
#include <bits/stdc++.h>
using namespace std;
int main()
{
int T;
cin>>T;
for(int i=0;i<T;i++){
int P,S;
cin>>P;
cin>>S;

int C[P],s1=0;
int R[S],s2=0;

for(int k=0;k<P;k++){cin>>C[k]; s1+=C[k];}
for(int k=0;k<S;k++){cin>>R[k];s2+=R[k];}

if((s1==s2)){

int A[P][S];
for(int k=0;k<P;k++)for(int j=0;j<S;j++)A[k][j]=0;

for(int k=0;k<P;k++)
{int c=C[k],s=0;
for(int j=0;j<S,s<c;j++){ s++; A[k][j]=1;}
}

for(int k=0;k<P;k++)sort(A[k],A[k]+S);


int sum[S]={0};
for(int k=0;k<S;k++)for(int j=0;j<P;j++){sum[k]+=A[j][k];}



int c1=0,c2=0;
for(int j=0;j<S;j++){if(sum[j]==R[j])c1++;}

if(c1==S)cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
else cout<<"NO"<<endl;

}
}```



TC0 i am passing but for some case its giving TLE
TLE means your algorithm is wrong.

It means you need to think of a SMART algorithm which exploits some property of mathematics to give you the direct answer.

The DUMB answer is to simply search every possible combination.
Topic archived. No new replies allowed.