Doubt in Hackerearth question

So i was solving this question


And this is my code:

Can somebody explain why am i getting WA?

So, I am not gonna debug your code, but straight away what I could see the obvious issue is the concept of two pointer is correct , but you started x(I suppose Motu’s) initial as arr[0], shouldn’t it be arr[0]/2, and also when you update the value of x it should be (double)arr[i]/2, otherwise it will only take the integer part.

Here’s my solution:

I made to vectors for motu and patlu, which will have the time taken at each step i.e, for motu, element/2 and sum of these at each step and for patlu just sum of array elements from n-1. Then till i==j, check who takes longer at each step, and increase or decrease i and j.

few boundaries: if(n==1) motu will win.
after the calc. of i and j, if(j-i-1==1) increase motu by 1.

count of motu i+1
count of patlu n-j.
Hope this helps as your logic was somewhat similar i think.

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const int MOD	= 1e9 + 7;

#define fastio			std::ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define test long long t;cin>>t;while(t--)

double tick(){static clock_t oldt,newt=clock();double diff=1.0*(newt-oldt)/CLOCKS_PER_SEC;oldt=newt;return diff;}

int main() {
#ifndef ONLINE_JUDGE
	freopen("inp.txt", "r", stdin);
	freopen("output.txt","w", stdout);
#endif
fastio;

test{

	int n;cin>>n;
	vector<long long > arr(n);

	for(int i=0;i<n;i++) cin>>arr[i];

		if(n==1) {cout<<"1 0\nMotu\n";continue;}

	long long cntMotu=0,cntPatlu=0;

	vector<long double> mots(n); //for motu

	mots[0]=(long double)arr[0]/2; //motu eats twice fast

	for(int i=1;i<n;i++)
		mots[i]=mots[i-1]+(double)arr[i]/2;

	vector<long long > pats(n); //for patlu
	pats[n-1]=arr[n-1];

	for(int i=n-2;i>=0;i--)
		pats[i]=pats[i+1]+arr[i];

	int i=0,j=n-1; //intialise

	while(i<j)
	{
		//cout<<mots[i]<<" "<<pats[j]<<"\n";
		if(mots[i]<pats[j] && j-i>1)
			i++;
		else if(pats[j]<mots[i] && j-i>1)
			j--;

		else if(pats[j]==mots[i] && j-i-1>1)
			{i++;j--;}
		else 
			break;

	}

	

	if(j-i-1==1)
		cntMotu=i+2;
	else
		cntMotu=i+1;
	
	cntPatlu=n-j;

	cout<<cntMotu<<" "<<cntPatlu<<"\n";
	if(cntMotu>cntPatlu) cout<<"Motu\n";
	else if(cntPatlu>cntMotu) cout<<"Patlu\n";
	else cout<<"Tie\n";
}

//cout<<tick()<<"\n";
return 0;
}
2 Likes

Hey man!
thanks i just checked this out :slight_smile:
yeah our logic is similar
i mean i got what to do
i just lack practise i guess to solve such questions

I Understood your code’s logic.
Please View my code and see what error did i make. I successfully passed the test case, but failed while submitting the code.

Here in the code i have made two arrays am and ap, with ap being twice of am (motu eats twice as patlu).

import java.util.*;
   class TestClass {
    public static void main(String args[] ) throws Exception {
   
    Scanner sc = new Scanner(System.in);
    int t=sc.nextInt();
    for(int q=0;q<t;q++){
        int n=sc.nextInt();
        int [] am=new int[n];
        int [] ap=new int [n];
        for(int i=0;i<n;i++){
            am[i]=sc.nextInt();
            ap[i]=am[i]*2;
        }           
        int s=0;
        int l=n-1;
        int m=0;
        int p=0;
        while(s<l){
            if(am[s]>ap[l]  ){
                am[s]=am[s]-ap[l];
                l--;
                p++;
            }
            else if(am[s]<ap[l]  ){
                ap[l]=ap[l]-am[s];
                s++;
                m++;
            }
            else if(am[s]==ap[l]  ){
                s++;
                m++;
                l--;
                p++;
            }
        }
        if(m>p){
            p=n-m;
        }
        else if(p>m){
            m=n-p;
        }
        System.out.println(m+" "+p);
        if(p>m){
            System.out.println("Patlu");
        }
        else if(p<m){
            System.out.println("Motu");
        }
        else if(p==m){
            System.out.println("Tie");
        }
    }
 }
}