title: Minimum Operations Make Gcd Array Multiple K
signature: int minimumOperationsMakeGcdArrayMultipleK(vector<int> a, int n, int k) {
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(int actual, int expected){
      return actual == expected;
  }
  
  string driver(vector<int> a, int n, int k, int expected){
      try
      {
          if (validateSolution(PLACEHOLDER_FN_NAME(a, n, k), 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({3, 7, 27, 32, 36, 37, 44, 48, 50, 64, 86}, 5, 10, 19); 
      cout << "TEST-" << 0 << "..."
           <<  result
           << "\n";
  
      result = driver({-22, 6, -20, 60, -74, 98, 52, -22}, 5, 4, 130); 
      cout << "TEST-" << 1 << "..."
           <<  result
           << "\n";
  
      result = driver({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, 1}, 23, 29, 661); 
      cout << "TEST-" << 2 << "..."
           <<  result
           << "\n";
  
      result = driver({77, 11, 51, 11, 84, 79, 43, 12, 14, 50, 15, 6, 85, 32, 74, 49, 7, 2, 58}, 9, 17, 43); 
      cout << "TEST-" << 3 << "..."
           <<  result
           << "\n";
  
      result = driver({-90, -66, -64, -58, -46, -44, -32, -30, -30, -22, -18, -14, 12, 12, 18, 34, 44, 60, 70, 70, 74, 76, 86, 98, 98}, 12, 22, 778); 
      cout << "TEST-" << 4 << "..."
           <<  result
           << "\n";
  
      result = driver({1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1}, 36, 31, 1096); 
      cout << "TEST-" << 5 << "..."
           <<  result
           << "\n";
  
      result = driver({9, 22, 27, 27, 37, 53, 53, 56, 63, 73, 76, 81, 82}, 10, 11, 28); 
      cout << "TEST-" << 6 << "..."
           <<  result
           << "\n";
  
      result = driver({-46, 60, 80, 80, 42, -98, 30, -48, 4, -32, -78, 40, 52, 26, 88, 4, 22, 62, 88, -94, 2, 0, 58, 38, 52, -50, -52, 58, -62, 30, -38, -8, -82, -66}, 18, 19, 479); 
      cout << "TEST-" << 7 << "..."
           <<  result
           << "\n";
  
      result = driver({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}, 19, 22, 413); 
      cout << "TEST-" << 8 << "..."
           <<  result
           << "\n";
  
      result = driver({42, 69, 93, 82, 8, 23, 73, 1, 77, 39, 49, 4, 95, 85}, 12, 13, 51); 
      cout << "TEST-" << 9 << "..."
           <<  result
           << "\n";
  
      return 0;
  }
source_py: def minimum_operations_make_gcd_array_multiple_k(a, n, k):
      result = 0
      for i in range(n):
          if ((a[i] != 1) and (a[i] > k)):
              result = (result + min((a[i] % k), (k - (a[i] % k))))
          else:
              result = ((result + k) - a[i])
      return result
source_cpp: int minimum_operations_make_gcd_array_multiple_k(vector<int> a, int n, int k) {
    int result = 0;
    for ( int i = 0;
    i < n;
    ++ i ) {
      if ( a [ i ] != 1 && a [ i ] > k ) {
        result = result + min ( a [ i ] % k, k - a [ i ] % k );
      }
      else {
        result = result + k - a [ i ];
      }
    }
    return result;
  }