Three Equal Parts Solution Java
class Solution {\n  public int[] threeEqualParts(int[] A) {\n    int ones = 0;\n\n    for (int a : A)\n      if (a == 1)\n        ++ones;\n\n    if (ones == 0)\
class Solution {\n  public int[] threeEqualParts(int[] A) {\n    int ones = 0;\n\n    for (int a : A)\n      if (a == 1)\n        ++ones;\n\n    if (ones == 0)\n      return new int[] {0, A.length - 1};\n    if (ones % 3 != 0)\n      return new int[] {-1, -1};\n\n    int k = ones / 3;\n    int i = 0;\n    int j = 0;\n    int first = 0;\n    int second = 0;\n    int third = 0;\n\n    for (i = 0; i < A.length; ++i)\n      if (A[i] == 1) {\n        first = i;\n        break;\n      }\n\n    int gapOnes = k;\n\n    for (j = i + 1; j < A.length; ++j)\n      if (A[j] == 1 && --gapOnes == 0) {\n        second = j;\n        break;\n      }\n\n    gapOnes = k;\n\n    for (i = j + 1; i < A.length; ++i)\n      if (A[i] == 1 && --gapOnes == 0) {\n        third = i;\n        break;\n      }\n\n    while (third < A.length && A[first] == A[second] && A[second] == A[third]) {\n      ++first;\n      ++second;\n      ++third;\n    }\n\n    if (third == A.length)\n      return new int[] {first - 1, second};\n    return new int[] {-1, -1};\n  }\n}\n
['Array', 'Math']