TOWIN - Editorial

i misunderstood the question now got my mistake @ssjgz :grinning:

1 Like

#include <bits/stdc++.h>

using namespace std;

int main()
{
int t,n;
cin>>t;

while(t–)
{ cin>>n;

   int arr[n], even =0,odd=0;
   for(int i=0;i<n;i++)
   {
       cin>>arr[i];
   }
   sort(arr,arr+n,greater<int>());
   odd += arr[0];
   even  +=arr[1]+arr[2];
   if(n>=3)
   {
   for(int i =3;i<n;i++)
   {
       if (i % 2 == 0) 
        even += arr[i]; 
    else
        odd += arr[i]; 
   }
   if(even == odd)
   cout<<"draw"<<endl;
   else if (odd > even)
   cout<<"first"<<endl;
   else
   cout<<"second"<<endl;
   }

}
}

please correct me where am I wrong!

//Remeber choose the turns wisely so that P1 wins(we r on P1s side)

string whoWillWin(int n,int a[]){

int P1=0,P2=0;

if(n==1)

    P1+=a[0];

if(n==2){

    P1+=a[0];

    P2+=a[1];

}

 if(n>=3){

     P1+=a[0];

     P2+=a[1];

     P2+=a[2];

     for(int i=3;i<n;i+=2){

         P1+=a[i];

         if(i>3)

         P2+=a[i-1];

     }

     

 }

if(P1>P2)

    return "first";

else if(P1<P2)

    return "second";

else 

    return "draw";

}

int main(){

int T;

cin>>T;

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

    int n;

    cin>>n;

    int a[n];

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

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

    cin>>a[j];

}

  string st=whoWillWin(n,a);

    cout<<st<<endl;

}

return 0;

}

Please Let me know where i am wrong???

//Remeber choose the turns wisely so that P1 wins(we r on P1s side)

string whoWillWin(int n,int a[]){

int P1=0,P2=0;

if(n==1)

    P1+=a[0];

if(n==2){

    P1+=a[0];

    P2+=a[1];

}

 if(n>=3){

     P1+=a[0];

     P2+=a[1];

     P2+=a[2];

     for(int i=3;i<n;i+=2){

         P1+=a[i];

         if(i>3)

         P2+=a[i-1];

     }

     

 }

if(P1>P2)

    return "first";

else if(P1<P2)

    return "second";

else 

    return "draw";

}

int main(){

int T;

cin>>T;

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

    int n;

    cin>>n;

    int a[n];

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

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

    cin>>a[j];

}

  string st=whoWillWin(n,a);

    cout<<st<<endl;

}

return 0;

}

Please let me know where i am wrong???

/* package codechef; // don’t place package name! */

import java.util.Scanner;
import java.lang.;
import java.io.
;

/* Name of the class has to be “Main” only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt(); //testcase input
int i;
int c1,c2;
while(t>0){
c1=0; c2=0; //chance 1, chance 2
int n=sc.nextInt();
int a[]=new int[n];
for(i=0;i<n;i++){
a[i]=sc.nextInt();
}
c1=c1+a[0];
for(i=3;i<n;i=i+2){
c1=c1+a[i];
}
c2=c2+a[1];
for(i=2;i<n;i=i+2){
c2=c2+a[i];
}
if(c1>c2){
System.out.println(“first”);
}
else if(c1<c2){
System.out.println(“second”);
}
else if(c1==c2){
System.out.println(“draw”);
}
t=t-1;
}
}
}

I am getting a NZEC runtime error, can someone pls tell me what is wrong?

Please either format your code or (better!) link to your submission - the forum software has mangled it and it won’t compile! :slight_smile:

1 Like

Here is the link to my submission
https://www.codechef.com/viewsolution/37374791

And thanks a lot for taking the initiative to check my code.

Consider the test input:

1
1
1
1 Like

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: