title: Subsequences Size Three Array Whose Sum Divisible M 1
signature: pub fn subsequences_size_three_array_whose_sum_divisible_m_1(a: Vec<i32>, n: i32, m: i32) -> f32 {
test_code: use std::collections::HashMap;
  use std::collections::HashSet;
  use std::cmp::*;
  use std::collections::*;
  use std::fmt::*; 
  
  
  // SOLUTION CODE
  // ============================================
  PLACEHOLDER_CODE_BODY
  
  // TESTING CODE 
  // ============================================
  pub fn validate_solution(actual: f32, expected: f32)->bool{
  return (actual - expected).abs() < 1e-06
  }
  pub fn driver(a: Vec<i32>, n: i32, m: i32, expected: f32)->String{
      if validate_solution(PLACEHOLDER_FN_NAME(a, n, m), expected) {
        return "PASSED".to_string();
      };
      return "FAILED".to_string();
  
  }
  
  fn main() {
      println!("TEST-0...{}", driver(
        Vec::from([6, 7, 13, 16, 19, 20, 0, 4, 7, 10, 15, 17, 0, 2, 8, 12, 13, 14, 0, 0, 0, 2, 2, 2, 4, 8, 10, 73, 76, 78, 87, 90, 91, 99]), 27, 21, 
        75.0
      ));
      
      println!("TEST-1...{}", driver(
        Vec::from([7, 13, 19, 15, 14, 11, 19, 4, 1, 18, 7, 5, 15, 9, 5, 11, 6, 18, 5, 19, 9, 7, 9, 19, 9, 12, 20, 12, 2, 66, 94, 12, 10, 4, 40, -72, 20, -2, -90, -90]), 29, 21, 
        0.0
      ));
      
      println!("TEST-2...{}", driver(
        Vec::from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]), 29, 43, 
        1140.0
      ));
      
      println!("TEST-3...{}", driver(
        Vec::from([1, 2, 3, 37, 91]), 3, 4, 
        0.0
      ));
      
      println!("TEST-4...{}", driver(
        Vec::from([8, 6, 10, 10, 4, 2, 10, 2, 2, 10, 0, 8, 10, 86, 88]), 13, 12, 
        17.0
      ));
      
      println!("TEST-5...{}", driver(
        Vec::from([1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1]), 11, 15, 
        56.0
      ));
      
      println!("TEST-6...{}", driver(
        Vec::from([1, 2, 0, 2, 3, 0, 1, 58, 78]), 7, 4, 
        6.0
      ));
      
      println!("TEST-7...{}", driver(
        Vec::from([0, 2, 2, 12, 4, 8, 8, 6, 10, 12, 10, 4, 4, -2, -4]), 13, 14, 
        12.0
      ));
      
      println!("TEST-8...{}", driver(
        Vec::from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]), 37, 34, 
        286.0
      ));
      
      println!("TEST-9...{}", driver(
        Vec::from([27, 1, 31, 14, 20, 8, 23, 12, 15, 0, 15, 26, 3, 8, 3, 26, 15, 31, 97, 90, 1, 69, 77, 20, 68, 11, 2, 92, 50, 8, 23, 83, 76, 6, 32, 43, 92]), 18, 35, 
        6.0
      ));
      
  }
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;
    }