We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Loading...
  • Practice
  • Compete
  • Jobs
  • Leaderboard
  • Hiring developers?
  1. Practice
  2. Algorithms
  3. Search
  4. Sherlock and Array
  5. Discussions

Sherlock and Array

  • Problem
  • Submissions
  • Leaderboard
  • Discussions
  • Editorial

    You are viewing a single comment's thread. Return to all comments →

  • kanval87 3 years ago+ 3 comments

    its really embarrasing. my code passes against the first test and then on submission it passes against 0 , 1 ,2 , 5 while it gets timed out on 3 , 4 and fails on 6. i tried your hack as well but no success. can anybody point out whats wrong i am doing here.

    public class SharlockArraySum {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (true) {
            if (in.hasNextLine()) {
                String s = in.nextLine();
                if (s.contentEquals("")) {
                    break;
                } else {
                    String[] strings = s.split(" ");
                    if (strings.length > 1) {
                        int[] numbers = new int[strings.length];
                        for (int i = 0; i < strings.length; i++) {
                            numbers[i] = Integer.parseInt(strings[i]);
                        }
                        checkIfLeftEqualToRight(numbers);
                    }else if(strings.length == 1){
    

    // if(Integer.parseInt(strings[0]) == 1){ // System.out.println("YES"); // } } } } else { break; } }

    }
    
    private static void checkIfLeftEqualToRight(int[] integers) {
        int totalIntArrayToLoop = integers.length - 1;
        int sum = getTotalSum(integers, 0, totalIntArrayToLoop);
        for (int i = 0; i <= totalIntArrayToLoop; i++) {
            if (i == 0 || i == totalIntArrayToLoop) {
                int remained = sum - integers[i];
                if (remained == 0) {
                    System.out.println("YES");
                    return;
                }
            } else {
                int toSubtract = getTotalSum(integers, 0, i - 1);
                int resultToCheck = sum - (toSubtract * 2) - integers[i ];
                if (resultToCheck == 0) {
                    System.out.println("YES");
                    return;
                }
            }
        }
        System.out.println("NO");
        return;
    }
    
    private static int getTotalSum(int[] num, int start, int stop) {
    
        int returnSum = 0;
        for (int i = start; i <= stop; i++) {
            returnSum = returnSum + num[i];
        }
    
        return returnSum;
    }
    

    }

    0|
    ParentPermalink
    • phoenixRises 3 years ago+ 1 comment

      I would suggest making the functions inline. You are passing an array to a function in checkIfLeftEqualToRight, which is as it is expensive, and on top of it the maximum array size is 10^5, adding all the more to the running time of the program. Tell me if it works.

      1|
      ParentPermalink
      • kanval87 3 years ago+ 1 comment

        Hi , Thanks for the reply.

        the code has been written in java and sorry for my lack of knowledge but i never heard inline function in java. i tried to look up but didnt find much answere to that. apart from it from my understanding of java , we are passing the refrence of the array object but not the object itself so method is just extension of seprated code. i might be wrong.. in such case i would welcome updated information.

        apart from it the logic that i have written it works in two 4 cases while fails in 3. in two timed out is there while in 1 it fails. i am trying to understand what am i doing wrong here logically.

        0|
        ParentPermalink
        • phoenixRises 3 years ago+ 1 comment

          I apologize for not being clear enough. I meant substitute the function call with the function itself. You might have to make slight modifications in the code, which you should be able to do with a little effort. In the else part of your checkifleftequaltoright, you are calling the gettotalsum function, and the call is within the for loop. I think that could be a cause of problem. Try substituting the function call with the function code and see if it works. Have you asked the experts while posting your question?

          Tip: Always format your code while you ask a question. The greater the readability of your code, the more likely you get an answer. I have formatted it this time for you. I hope somebody else answers, if my response is not good enough.

          public class SharlockArraySum 
          {
           public static void main(String[] args) 
           {
              Scanner in = new Scanner(System.in);
              while (true) 
              {
                  if (in.hasNextLine()) 
                  {
                      String s = in.nextLine();
                      if (s.contentEquals("")) 
                      {
                          break;
                      } 
                      else 
                      {
                          String[] strings = s.split(" ");
          
                          if (strings.length > 1) 
                          {
                              int[] numbers = new int[strings.length];
                              for (int i = 0; i < strings.length; i++)                             {
                                  numbers[i] = Integer.parseInt(strings[i]);
                              }
                              checkIfLeftEqualToRight(numbers);
                          }
                          else if(strings.length == 1)
                          {
                              // if(Integer.parseInt(strings[0]) == 1)
                              //{ 
                                  // System.out.println("YES");
                              //}
                           } 
                       } 
          
                  } 
                  else 
                  { 
                      break; 
                  } 
              }
          }
          
          private static void checkIfLeftEqualToRight(int[] integers) 
          {
              int totalIntArrayToLoop = integers.length - 1;
              int sum = getTotalSum(integers, 0, totalIntArrayToLoop);
              for (int i = 0; i <= totalIntArrayToLoop; i++) 
              {
                  if (i == 0 || i == totalIntArrayToLoop) 
                  {
                      int remained = sum - integers[i];
                      if (remained == 0) 
                      {
                          System.out.println("YES");
                          return;
                      }
                   } 
                   else 
                   {
                      int toSubtract = getTotalSum(integers, 0, i - 1);
                      int resultToCheck = sum - (toSubtract * 2) - integers[i];
                      if (resultToCheck == 0) 
                      {
                          System.out.println("YES");
                          return;
                      }
                   }
              }
          
              System.out.println("NO");
              return;
          }
          
          private static int getTotalSum(int[] num, int start, int stop) 
          {
          
              int returnSum = 0;
              for (int i = start; i <= stop; i++) 
              {
                  returnSum = returnSum + num[i];
              }
          
              return returnSum;
          }
          }
          
          0|
          ParentPermalink
          • kanval87 3 years ago+ 1 comment

            Please accept my thanks for answering to my queries as well as helping me out :)

            0|
            ParentPermalink
            • phoenixRises 3 years ago+ 1 comment

              You're welcome. Is your solution now passing all test cases?

              0|
              ParentPermalink
              • kanval87 3 years ago+ 0 comments

                unfortunately no , the issue is with logic i think. i am solving other puzzles in the mean while and letting my mind cool down for this logic :).

                0|
                ParentPermalink
    • i_am_malav 3 years ago+ 0 comments

      Here If you see n : 1<= n <= 10 ^ 5 So it is not right way to call getSum method everytime for given array which has lenght of 10 ^ 5. Try not to call getSum method many times as it is too expensive. Seel my code below if it can help you out: import java.io.; import java.util.;

      public class Solution { int n; int[] nums;

      public static void main(String[] args) {
          Scanner sc = new Scanner(System.in);
          int t =sc.nextInt();
          Solution[] s = new Solution[t]; 
      
            for(int i = 0 ; i < t ; i++ )
             {
               s[i] = new Solution();
               s[i].n = sc.nextInt();
               s[i].nums = new int[s[i].n];
      
               for(int j =0 ; j < s[i].n ;j++)
                 {
                   s[i].nums[j] = sc.nextInt();  
                 }  
      
                boolean match = false;
      
                int totalSum = getSum(0, s[i].n - 1, s[i].nums);
                int previousSum = 0, leftSum =0, rightSum = 0; 
      
                for( int k = 0 ; k < s[i].n ; k++ )
                  {    
                   previousSum = previousSum + s[i].nums[k];
                   leftSum = previousSum - s[i].nums[k];
                   rightSum = totalSum - leftSum - s[i].nums[k];
      
                    if(leftSum == rightSum)
                    {
                       match = true;
                       break; 
                    }
                  } 
      
                if(match)
                   System.out.println("YES");
                else
                    System.out.println("NO");
             } 
      }
      
      public static int getSum(int startIndex, int endIndex, int[] arr)
       {
         int sum =0;
          for(int i = startIndex; i<= endIndex; i++)
             sum = sum + arr[i];
          return sum;
      }
      

      }

      2|
      ParentPermalink
    • Anwar_Siddiqui 2 years ago+ 0 comments
      static String solve(int[] a){
              int len =a.length;
              int j=len-1;
              int i=0, sum1=0, sum2=0;
              while(i<len && j>=0){
                  if(sum1==sum2 && ((i-j)==0)){
                      return "YES";
                  }else if(sum1<sum2){
                      sum1+=a[i++];
                  }else{
                      sum2+=a[j--];
                  }
              }
              return "NO";
          }
      
      3|
      ParentPermalink
  • Contest Calendar
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature