Problem :Motu and Patlu (hackerearth)
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).
Also I was getting error, so I formatted the code with
if(m>p){
p=n-m;
}
else if(p>m){
m=n-p;
}
Sample Test case:
3
5
2 6 2 1 7
4
15 2 1 3
5
2 4 12 4 7
Sample Output
4 1
Motu
1 3
Patlu
3 2
Motu
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");
}
}
}
}