title: Elements To Be Added So That All Elements Of A Range Are Present In Array
signature: function elements_to_be_added_so_that_all_elements_of_a_range_are_present_in_array(arr, n)
test_code: using Printf
  
  # SOLUTION CODE
  # ============================================
  PLACEHOLDER_CODE_BODY
  
  # TESTING CODE 
  # ============================================
  function validate_solution(actual, expected)
      return actual == expected
  end
  
  function driver(arr, n, expected)
      try
  
          if validate_solution(PLACEHOLDER_FN_NAME(arr, n), expected)
              return "PASSED";
          end
          return "FAILED";
      catch exception_obj
          return string(Base.typename(typeof(exception_obj)).wrapper);
      end
  end
  
  function main()
      
      result = driver([4, 4, 5, 7, 7, 9, 13, 15, 18, 19, 25, 27, 27, 29, 32, 36, 48, 51, 53, 53, 55, 65, 66, 67, 72, 74, 74, 76, 77, 79, 80, 81, 82, 83, 83, 86, 87, 97, 98, 98, 99], 30, 51);
      @printf("TEST-0...%s\n",result);
      
      result = driver([-90, -90, -88, -84, -84, -84, -80, -74, -52, -26, -26, -16, -8, 6, 14, 16, 34, 64, 72, 92, 94], 17, 112);
      @printf("TEST-1...%s\n",result);
      
      result = driver([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], 24, 0);
      @printf("TEST-2...%s\n",result);
      
      result = driver([10, 12, 12, 13, 13, 23, 25, 26, 29, 32, 39, 43, 43, 54, 62, 79, 92], 14, 34);
      @printf("TEST-3...%s\n",result);
      
      result = driver([-94, -86, -72, -64, -64, -58, -56, -56, -56, -56, -54, -54, -52, -42, -42, -40, -36, -32, -28, -22, -20, -18, -12, -8, -6, -4, 0, 2, 4, 10, 16, 30, 32, 48, 48, 60, 70, 74, 76, 84], 35, 115);
      @printf("TEST-4...%s\n",result);
      
      result = driver([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], 29, 0);
      @printf("TEST-5...%s\n",result);
      
      result = driver([4, 5, 8, 12, 16, 16, 17, 20, 20, 23, 26, 26, 27, 28, 32, 34, 40, 40, 41, 41, 44, 45, 47, 49, 51, 52, 54, 57, 60, 62, 63, 64, 66, 68, 69, 70, 71, 76, 77, 80, 80, 80, 90, 91, 92, 94, 96, 98, 99], 42, 42);
      @printf("TEST-6...%s\n",result);
      
      result = driver([-98, -92, -92, -92, -92, -86, -78, -74, -72, -68, -60, -52, -48, -48, -46, -46, -44, -40, -38, -26, -18, 2, 4, 4, 10, 14, 14, 16, 20, 38, 48, 48, 50, 56, 60, 66, 74, 76, 80, 84, 88, 96, 96, 96], 37, 144);
      @printf("TEST-7...%s\n",result);
      
      result = driver([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], 17, 0);
      @printf("TEST-8...%s\n",result);
      
      result = driver([49, 66, 84], 2, 16);
      @printf("TEST-9...%s\n",result);
      
  end
  
  main()
source_py: def elements_to_be_added_so_that_all_elements_of_a_range_are_present_in_array(arr, n):
      count = 0
      arr.sort()
      for i in range(0, (n - 1)):
          if ((arr[i] != arr[(i + 1)]) and (arr[i] != (arr[(i + 1)] - 1))):
              count += ((arr[(i + 1)] - arr[i]) - 1)
      return count
source_cpp: int elements_to_be_added_so_that_all_elements_of_a_range_are_present_in_array(vector<int> arr, int n) {
    int count = 0;
    sort(arr.begin(), arr.end());
    for ( int i = 0;
    i < n - 1;
    i ++ ) if ( arr [ i ] != arr [ i + 1 ] && arr [ i ] != arr [ i + 1 ] - 1 ) count += arr [ i + 1 ] - arr [ i ] - 1;
    return count;
  }