TOWIN - Editorial

Thanks a lot for your help, I could succesfully debug my code.
:smile:

1 Like

In the for loop starting with i=3 you have incremented cnt1 and cnt2 with 1 … you have to increment both these variables with the vector values like cnt1+=v[i] and cnt2+=v[i]

Thanks …

1 Like

#include
#include <bits/stdc++.h>

using namespace std;

int main() {
int test,n,p1=0,p2=0;
cin>>test;

while(test--){
cin>>n;
int a[n];
    for(int i=0;i<n;i++)
        cin>>a[i];
    sort(a, a + n, greater<int>());


        for(int i=0;i<n;i++){
           if(i==1||i==2)
               p2+=a[i];
           else if(i==0||i%2!=0)
               p1+=a[i];
           else
           p2+=a[i];

    }
    if(p1>p2)
    cout<<"first"<<endl;
    else if(p2==p1)
    cout<<"draw"<<endl;
    else if(p2>p1)
    cout<<"second"<<endl;

}


// your code goes here
return 0;

}

help please…

#include <stdio.h>

void sort(long a[],int n){
	int i,j;
	long temp;
	for(i=0;i<n-1;i++){
		for(j=0;j<n-i-1;j++){
			if(a[j]<a[j+1]){
				temp = a[j];
				a[j] = a[j+1];
				a[j+1] = temp;
			}
		}
	}
}

int main(void) {
	int t,i;
	int n;
	 long p1,p2;
	scanf("%d",&t);
	while(t--){
	    scanf("%d",&n);
	    long a[10000];
	    for(i=0;i<n;i++){
	        scanf("%ld",&a[i]);
	    }
	    sort(a,n);
	    p1=0;
	    p2=0;
	    for(i=0;i<n;i++){
	    	if(i%2!=0){
	    		if(i==1)
	    			p2+=a[i];
	    		else
	    	 		p1+=a[i];
	    	}
	    	else{
	    		if(i==0)
	    			p1+=a[i];
	    		else
					p2+=a[i];
	    }
	    }
	    if(p1>p2)
	        printf("first\n");
	    else if(p1==p2)
	        printf("draw\n");
	    else
	    	printf("second\n");
	}
}

Dont know whats wrong , please help
edit- i got it i made silly mistake in the sorting :fp:

hey you didnt checked the n <3
like for input

1
2
1 2

the answer should be 2 but you code says 1

https://www.codechef.com/viewsolution/37842826
kindly help I cant understand which test case my code is failing.

https://www.codechef.com/viewsolution/37921105
I dont understand which test case i am failing here Please help out

in first chance player1 will choose 2 and player2 will choose 1. Then the answer has to be first. Can u explain your point of view probably i haven’t undestood the question properly

we want to make 1st player win . and there is a condition that in second chance we can take 2 steps . so to make max profit we will let 1st person chose the second chance so that he gets more points and 2nd one take the first. thats the logic

Can anyone help me out?? Where am I going wrong?

#include <bits/stdc++.h>

using namespace std;

int first(int a[], int n){

int score=0;

for(int i=0;i<n;i++){

    if(i==0 || (i%2==1 && i!=1)){

        score+=a[i];

    }

}

return score;

}

int second(int a[], int n){

int score=0;

for(int i=0;i<n;i++){

    if(i==1 || (i%2==0 && i!=0)){

        score+=a[i];

    }

}

return score;

}

void solve()

{

int n;

cin>>n;

int a[n];



for(int i=0;i<n;i++){

    cin>>a[i];

    

}

sort(a, a+n, greater<int>());

string best = first(a,n)>second(a,n)?"first":(first(a,n)==second(a,n)?"draw":"second");

cout<<best<<endl;

}

int main()

{

    int t;

    cin >> t;

    while (t--)

    {

        solve();

    }

    return 0;

}

why on submitting this code gives wrong answer even my output is correct for every case
#include <stdio.h>

    int main()
    {
        int t;
        scanf("%d", &t);
        while (t--)
        {
            int first = 0, second = 0, sign = -1, i, j, n, temp;
            scanf("%d", &n);
            int a[n];
            for (i = 0; i < n; i++)
            {
                scanf("%d", &a[i]);
            }
            for (i = 0; i < n - 1; i++)
            {
                for (j = 0; j < n - 1 - i; j++)
                {
                    if (a[j] < a[j + 1])
                    {
                        temp = a[j];
                        a[j] = a[j + 1];
                        a[j + 1] = temp;
                    }
                }
            }
            for (i = 0; i < n; i++)
            {
                if (i == 2)
                {
                    second = second + a[i];
                }
                else if (sign == -1)
                {
                    first = first + a[i];
                    sign = sign * (-1);
                }
                else if (sign == 1)
                {
                    second = second + a[i];
                    sign = sign * (-1);
                }
            }
            if (first > second)
                printf("first\n");
            else if (first == second)
                printf("draw\n");
            else if (first < second)
                printf("second\n");
        }
        return 0;
    }

I can’t understand why we should sort array??
please help :neutral_face: