title: Count Possible Groups Size 2 3 Sum Multiple 3
signature: float countPossibleGroupsSize23SumMultiple3(vector<int> arr, int n) {
test_code: #include <algorithm>
  #include <assert.h>
  #include <cstdlib>
  #include <iostream>
  #include <map>
  #include <set>
  #include <string>
  #include <tuple> 
  #include <unordered_map>
  #include <vector>
  #include <math.h>
  #include <cmath>
  #include <bits/stdc++.h>
  #include <numeric>
  #include <functional>
  
  using namespace std;
  
  // SOLUTION CODE
  // ============================================
  PLACEHOLDER_CODE_BODY
  
  // TESTING CODE 
  // ============================================
  bool validateSolution(float actual, float expected){
      return abs(actual - expected) < 1e-06;
  }
  
  string driver(vector<int> arr, int n, float expected){
      try
      {
          if (validateSolution(PLACEHOLDER_FN_NAME(arr, n), expected)){
              return "PASSED";
          }
          return "FAILED";
      }
      catch (const std::overflow_error& exception_obj)
      {
          return typeid(exception_obj).name();
      }
      catch (const std::runtime_error& exception_obj)
      {
          return typeid(exception_obj).name();
      }
      catch (const std::exception& exception_obj)
      {
          return typeid(exception_obj).name();
      }
      catch (...)
      {
          return "UNK_ERROR";
      }
  }
  
  int main() {
      string result = "";
  
      result = driver({1, 3, 6, 12, 18, 31, 36, 43, 47, 59, 64, 64, 69, 69, 94, 98, 98}, 12, 90.0); 
      cout << "TEST-" << 0 << "..."
           <<  result
           << "\n";
  
      result = driver({94, 46, 64, 18, 70, 22, -98, 52, -26, 34, -22, 22, 40, 66, -72, -66, 86, 84, 12, -38, 28, -60, -10, -30, -78, 76, 62, 74, 24, -64, 0, 92, -20, -66, -52}, 23, 655.0); 
      cout << "TEST-" << 1 << "..."
           <<  result
           << "\n";
  
      result = driver({0, 0, 0, 0, 0, 0, 0, 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}, 29, 3276.0); 
      cout << "TEST-" << 2 << "..."
           <<  result
           << "\n";
  
      result = driver({68, 96, 74, 38, 70, 70}, 5, 7.0); 
      cout << "TEST-" << 3 << "..."
           <<  result
           << "\n";
  
      result = driver({-88, -74, -70, -64, -64, -64, -58, -52, -18, -10, -6, 4, 4, 28, 44, 48, 52, 54, 64, 72, 76, 94}, 18, 329.0); 
      cout << "TEST-" << 4 << "..."
           <<  result
           << "\n";
  
      result = driver({0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1}, 36, 2310.0); 
      cout << "TEST-" << 5 << "..."
           <<  result
           << "\n";
  
      result = driver({9, 12, 14, 21, 27, 28, 45, 50, 54, 57, 58, 62, 78, 82, 91, 92, 95}, 9, 49.0); 
      cout << "TEST-" << 6 << "..."
           <<  result
           << "\n";
  
      result = driver({16, -46, -32, -36, -84, -14, -18, 16, 54, 90, -4}, 8, 24.0); 
      cout << "TEST-" << 7 << "..."
           <<  result
           << "\n";
  
      result = driver({0, 0, 1, 1}, 3, 1.0); 
      cout << "TEST-" << 8 << "..."
           <<  result
           << "\n";
  
      result = driver({53, 32, 54, 84, 79, 37, 44, 30, 92, 53, 89, 95}, 8, 29.0); 
      cout << "TEST-" << 9 << "..."
           <<  result
           << "\n";
  
      return 0;
  }
source_py: def count_possible_groups_size_2_3_sum_multiple_3(arr, n):
      c = [0, 0, 0]
      res = 0
      for i in range(0, n):
          c[(arr[i] % 3)] += 1
      res += ((c[0] * (c[0] - 1)) >> 1)
      res += (c[1] * c[2])
      res += (((c[0] * (c[0] - 1)) * (c[0] - 2)) / 6)
      res += (((c[1] * (c[1] - 1)) * (c[1] - 2)) / 6)
      res += (((c[2] * (c[2] - 1)) * (c[2] - 2)) / 6)
      res += ((c[0] * c[1]) * c[2])
      return res
source_cpp: int count_possible_groups_size_2_3_sum_multiple_3(vector<int> arr, int n) {
    int c [ 3 ] = {
      0 },
      i;
      int res = 0;
      for ( i = 0;
      i < n;
      i ++ ) c [ arr [ i ] % 3 ] ++;
      res += ( ( c [ 0 ] * ( c [ 0 ] - 1 ) ) >> 1 );
      res += c [ 1 ] * c [ 2 ];
      res += ( c [ 0 ] * ( c [ 0 ] - 1 ) * ( c [ 0 ] - 2 ) ) / 6;
      res += ( c [ 1 ] * ( c [ 1 ] - 1 ) * ( c [ 1 ] - 2 ) ) / 6;
      res += ( ( c [ 2 ] * ( c [ 2 ] - 1 ) * ( c [ 2 ] - 2 ) ) / 6 );
      res += c [ 0 ] * c [ 1 ] * c [ 2 ];
      return res;
    }