fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. // your code goes here
  13. int arr[]={3,6,2,3,5};
  14. int start=0;
  15. int end=arr.length-1;;
  16. int p1=0;
  17. int p2=0;
  18. int turn=1;
  19. boolean rev=false;
  20. while(start<=end){
  21. int selected=0;
  22. if(turn%2!=0){
  23. if(rev==false){
  24. p1=p1+arr[start];
  25. selected=arr[start];
  26. start++;
  27. }
  28. else{
  29. p1=p1+arr[end];
  30. selected=arr[end];
  31. end--;
  32. } turn++;}
  33. else{
  34.  
  35. if(rev==false){
  36. p2=p2+arr[start];
  37. selected=arr[start];
  38. start++;
  39. }
  40. else{
  41. p2=p2+arr[end];
  42. selected=arr[end];
  43. end--;
  44. }
  45. turn++;
  46. }
  47. if(selected%2==0){
  48. if(rev==false){
  49. rev=true;
  50. }
  51. else{
  52. rev=false;
  53. }
  54. }
  55. }
  56. System.out.println(p1);
  57. System.out.println(p2);
  58.  
  59.  
  60. }}
Success #stdin #stdout 0.07s 54628KB
stdin
Standard input is empty
stdout
10
9