title: Subsequences Size Three Array Whose Sum Divisible M 1
signature: subsequences_size_three_array_whose_sum_divisible_m_1 <- function(a, n, m) {
test_code: 
  
  
  # SOLUTION CODE
  # ============================================
  PLACEHOLDER_CODE_BODY
  
  # TESTING CODE 
  # ============================================
  validate_solution <- function(actual, expected) {
      abs(actual - expected) < 1e-06
  }
  
  driver <- function(a, n, m, expected){
      if (validate_solution(PLACEHOLDER_FN_NAME(a, n, m), expected)){
          "PASSED"
      } else{
          "FAILED"
      }
  }
  
  main <- function() {
      cat(sprintf("TEST-0...%s\n",driver(
          list(6L, 7L, 13L, 16L, 19L, 20L, 0L, 4L, 7L, 10L, 15L, 17L, 0L, 2L, 8L, 12L, 13L, 14L, 0L, 0L, 0L, 2L, 2L, 2L, 4L, 8L, 10L, 73L, 76L, 78L, 87L, 90L, 91L, 99L), 27L, 21L, 
          75.0
      )))
  
      cat(sprintf("TEST-1...%s\n",driver(
          list(7L, 13L, 19L, 15L, 14L, 11L, 19L, 4L, 1L, 18L, 7L, 5L, 15L, 9L, 5L, 11L, 6L, 18L, 5L, 19L, 9L, 7L, 9L, 19L, 9L, 12L, 20L, 12L, 2L, 66L, 94L, 12L, 10L, 4L, 40L, -72L, 20L, -2L, -90L, -90L), 29L, 21L, 
          0.0
      )))
  
      cat(sprintf("TEST-2...%s\n",driver(
          list(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), 29L, 43L, 
          1140.0
      )))
  
      cat(sprintf("TEST-3...%s\n",driver(
          list(1L, 2L, 3L, 37L, 91L), 3L, 4L, 
          0.0
      )))
  
      cat(sprintf("TEST-4...%s\n",driver(
          list(8L, 6L, 10L, 10L, 4L, 2L, 10L, 2L, 2L, 10L, 0L, 8L, 10L, 86L, 88L), 13L, 12L, 
          17.0
      )))
  
      cat(sprintf("TEST-5...%s\n",driver(
          list(1L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 1L, 1L, 1L, 0L, 1L, 1L), 11L, 15L, 
          56.0
      )))
  
      cat(sprintf("TEST-6...%s\n",driver(
          list(1L, 2L, 0L, 2L, 3L, 0L, 1L, 58L, 78L), 7L, 4L, 
          6.0
      )))
  
      cat(sprintf("TEST-7...%s\n",driver(
          list(0L, 2L, 2L, 12L, 4L, 8L, 8L, 6L, 10L, 12L, 10L, 4L, 4L, -2L, -4L), 13L, 14L, 
          12.0
      )))
  
      cat(sprintf("TEST-8...%s\n",driver(
          list(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), 37L, 34L, 
          286.0
      )))
  
      cat(sprintf("TEST-9...%s\n",driver(
          list(27L, 1L, 31L, 14L, 20L, 8L, 23L, 12L, 15L, 0L, 15L, 26L, 3L, 8L, 3L, 26L, 15L, 31L, 97L, 90L, 1L, 69L, 77L, 20L, 68L, 11L, 2L, 92L, 50L, 8L, 23L, 83L, 76L, 6L, 32L, 43L, 92L), 18L, 35L, 
          6.0
      )))
  
  }
  
  main()
source_py: def subsequences_size_three_array_whose_sum_divisible_m_1(A, N, M):
      ans = 0
      h = ([0] * M)
      for i in range(0, N):
          A[i] = (A[i] % M)
          h[A[i]] = (h[A[i]] + 1)
      for i in range(0, M):
          for j in range(i, M):
              rem = ((M - ((i + j) % M)) % M)
              if (rem < j):
                  continue
              if ((i == j) and (rem == j)):
                  ans = (ans + (((h[i] * (h[i] - 1)) * (h[i] - 2)) / 6))
              elif (i == j):
                  ans = (ans + (((h[i] * (h[i] - 1)) * h[rem]) / 2))
              elif (i == rem):
                  ans = (ans + (((h[i] * (h[i] - 1)) * h[j]) / 2))
              elif (rem == j):
                  ans = (ans + (((h[j] * (h[j] - 1)) * h[i]) / 2))
              else:
                  ans = (ans + ((h[i] * h[j]) * h[rem]))
          return ans
source_cpp: int subsequences_size_three_array_whose_sum_divisible_m_1(vector<int> A, int N, int M) {
    int ans = 0;
    int h [ M ] = {
      0 };
      for ( int i = 0;
      i < N;
      i ++ ) {
        A [ i ] = A [ i ] % M;
        h [ A [ i ] ] ++;
      }
      for ( int i = 0;
      i < M;
      i ++ ) {
        for ( int j = i;
        j < M;
        j ++ ) {
          int rem = ( M - ( i + j ) % M ) % M;
          if ( rem < j ) continue;
          if ( i == j && rem == j ) ans += h [ i ] * ( h [ i ] - 1 ) * ( h [ i ] - 2 ) / 6;
          else if ( i == j ) ans += h [ i ] * ( h [ i ] - 1 ) * h [ rem ] / 2;
          else if ( i == rem ) ans += h [ i ] * ( h [ i ] - 1 ) * h [ j ] / 2;
          else if ( rem == j ) ans += h [ j ] * ( h [ j ] - 1 ) * h [ i ] / 2;
          else ans = ans + h [ i ] * h [ j ] * h [ rem ];
        }
      }
      return ans;
    }