Sherlock and Array

  • + 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;
    }
    }