Skip to main content
deleted 25 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

This what my code looks like. If the array is length 1, I have put it in a ifan if statement, else the whole array needs to be traversed. That way save, saving some time.

Another thing important thing I have done is that while taking all the input, I have calculated the sum of them  , as I am left traversing, then the sum of the array is actually rightSumrightSum (rightSumrightSum = sum  - value of current Indexindex).

While I am traversing the left one increased value by adding previous index and right one decreases by the current Indexindex value.

Here is my code

 // TODO code application logic here
    Scanner lab = new Scanner(System.in);
    int leftSum = 0;
    int rightSum = 0;
    int testCases = lab.nextInt();
    for (int i = 0; i < testCases; i++) {
        int length = lab.nextInt();
        int temp[] = new int[length];

        for (int j = 0; j < length; j++) {
            temp[j] = lab.nextInt();
            rightSum += temp[j];
        }
        if (length == 1) {
            System.out.println("YES");
        } else {

            rightSum = rightSum - temp[0];
            for (int j = 1; j < length; j++) {
                if (j == length - 1) {
                    System.out.println("NO");
                    break;
                }
                rightSum = rightSum - temp[j];
                leftSum = leftSum + temp[j - 1];
                if (leftSum == rightSum) {
                    System.out.println("YES");
                    break;
                }
            }
        }
        rightSum = 0;
        leftSum = 0;

    }

This what my code looks like. If the array is length 1 I have put it in a if statement else the whole array needs to be traversed. That way save some time.

Another thing important I have done is that while taking all the input I have calculated the sum of them  , as I am left traversing then the sum of the array is actually rightSum (rightSum = sum- value of current Index).

While I am traversing the left one increased value by adding previous index and right one decreases by the current Index value.

Here is my code

 // TODO code application logic here
    Scanner lab = new Scanner(System.in);
    int leftSum = 0;
    int rightSum = 0;
    int testCases = lab.nextInt();
    for (int i = 0; i < testCases; i++) {
        int length = lab.nextInt();
        int temp[] = new int[length];

        for (int j = 0; j < length; j++) {
            temp[j] = lab.nextInt();
            rightSum += temp[j];
        }
        if (length == 1) {
            System.out.println("YES");
        } else {

            rightSum = rightSum - temp[0];
            for (int j = 1; j < length; j++) {
                if (j == length - 1) {
                    System.out.println("NO");
                    break;
                }
                rightSum = rightSum - temp[j];
                leftSum = leftSum + temp[j - 1];
                if (leftSum == rightSum) {
                    System.out.println("YES");
                    break;
                }
            }
        }
        rightSum = 0;
        leftSum = 0;

    }

This what my code looks like. If the array is length 1, I put it in an if statement, else the whole array needs to be traversed, saving some time.

Another important thing I have done is that while taking all the input, I have calculated the sum of them, as I am left traversing, then the sum of the array is actually rightSum (rightSum = sum  - value of current index).

While I am traversing the left one increased value by adding previous index and right one decreases by the current index value.

 // TODO code application logic here
    Scanner lab = new Scanner(System.in);
    int leftSum = 0;
    int rightSum = 0;
    int testCases = lab.nextInt();
    for (int i = 0; i < testCases; i++) {
        int length = lab.nextInt();
        int temp[] = new int[length];

        for (int j = 0; j < length; j++) {
            temp[j] = lab.nextInt();
            rightSum += temp[j];
        }
        if (length == 1) {
            System.out.println("YES");
        } else {

            rightSum = rightSum - temp[0];
            for (int j = 1; j < length; j++) {
                if (j == length - 1) {
                    System.out.println("NO");
                    break;
                }
                rightSum = rightSum - temp[j];
                leftSum = leftSum + temp[j - 1];
                if (leftSum == rightSum) {
                    System.out.println("YES");
                    break;
                }
            }
        }
        rightSum = 0;
        leftSum = 0;

    }
Source Link

This what my code looks like. If the array is length 1 I have put it in a if statement else the whole array needs to be traversed. That way save some time.

Another thing important I have done is that while taking all the input I have calculated the sum of them , as I am left traversing then the sum of the array is actually rightSum (rightSum = sum- value of current Index).

While I am traversing the left one increased value by adding previous index and right one decreases by the current Index value.

Here is my code

 // TODO code application logic here
    Scanner lab = new Scanner(System.in);
    int leftSum = 0;
    int rightSum = 0;
    int testCases = lab.nextInt();
    for (int i = 0; i < testCases; i++) {
        int length = lab.nextInt();
        int temp[] = new int[length];

        for (int j = 0; j < length; j++) {
            temp[j] = lab.nextInt();
            rightSum += temp[j];
        }
        if (length == 1) {
            System.out.println("YES");
        } else {

            rightSum = rightSum - temp[0];
            for (int j = 1; j < length; j++) {
                if (j == length - 1) {
                    System.out.println("NO");
                    break;
                }
                rightSum = rightSum - temp[j];
                leftSum = leftSum + temp[j - 1];
                if (leftSum == rightSum) {
                    System.out.println("YES");
                    break;
                }
            }
        }
        rightSum = 0;
        leftSum = 0;

    }