Hexagonal Grid Discussions | Algorithms | HackerRank
  • + 1 comment

    No DP, No recursion, O(n) solution

    static boolean solve(int s, int e) {
        int c = 0;
        for (int i = s; i < e; i++) {
          if ((a[i] == 1 && b[i] == 1) || (i - 1 >= 0 && a[i] == 1 && b[i - 1] == 1)) {
            if (c % 2 != 0)
              return false;
            else
              c = 0;
          }
          if (a[i] != 1)
            c++;
          if (b[i] != 1)
            c++;
    
        }
        if (c % 2 != 0)
          return false;
        else
          return true;
      }