title: Elements To Be Added So That All Elements Of A Range Are Present In Array
signature: elements_to_be_added_so_that_all_elements_of_a_range_are_present_in_array <- function(arr, n) {
test_code: 
  
  
  # SOLUTION CODE
  # ============================================
  PLACEHOLDER_CODE_BODY
  
  # TESTING CODE 
  # ============================================
  validate_solution <- function(actual, expected) {
      toString(actual)==toString(expected)
  }
  
  driver <- function(arr, n, expected){
      if (validate_solution(PLACEHOLDER_FN_NAME(arr, n), expected)){
          "PASSED"
      } else{
          "FAILED"
      }
  }
  
  main <- function() {
      cat(sprintf("TEST-0...%s\n",driver(
          list(4L, 4L, 5L, 7L, 7L, 9L, 13L, 15L, 18L, 19L, 25L, 27L, 27L, 29L, 32L, 36L, 48L, 51L, 53L, 53L, 55L, 65L, 66L, 67L, 72L, 74L, 74L, 76L, 77L, 79L, 80L, 81L, 82L, 83L, 83L, 86L, 87L, 97L, 98L, 98L, 99L), 30L, 
          51L
      )))
  
      cat(sprintf("TEST-1...%s\n",driver(
          list(-90L, -90L, -88L, -84L, -84L, -84L, -80L, -74L, -52L, -26L, -26L, -16L, -8L, 6L, 14L, 16L, 34L, 64L, 72L, 92L, 94L), 17L, 
          112L
      )))
  
      cat(sprintf("TEST-2...%s\n",driver(
          list(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), 24L, 
          0L
      )))
  
      cat(sprintf("TEST-3...%s\n",driver(
          list(10L, 12L, 12L, 13L, 13L, 23L, 25L, 26L, 29L, 32L, 39L, 43L, 43L, 54L, 62L, 79L, 92L), 14L, 
          34L
      )))
  
      cat(sprintf("TEST-4...%s\n",driver(
          list(-94L, -86L, -72L, -64L, -64L, -58L, -56L, -56L, -56L, -56L, -54L, -54L, -52L, -42L, -42L, -40L, -36L, -32L, -28L, -22L, -20L, -18L, -12L, -8L, -6L, -4L, 0L, 2L, 4L, 10L, 16L, 30L, 32L, 48L, 48L, 60L, 70L, 74L, 76L, 84L), 35L, 
          115L
      )))
  
      cat(sprintf("TEST-5...%s\n",driver(
          list(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), 29L, 
          0L
      )))
  
      cat(sprintf("TEST-6...%s\n",driver(
          list(4L, 5L, 8L, 12L, 16L, 16L, 17L, 20L, 20L, 23L, 26L, 26L, 27L, 28L, 32L, 34L, 40L, 40L, 41L, 41L, 44L, 45L, 47L, 49L, 51L, 52L, 54L, 57L, 60L, 62L, 63L, 64L, 66L, 68L, 69L, 70L, 71L, 76L, 77L, 80L, 80L, 80L, 90L, 91L, 92L, 94L, 96L, 98L, 99L), 42L, 
          42L
      )))
  
      cat(sprintf("TEST-7...%s\n",driver(
          list(-98L, -92L, -92L, -92L, -92L, -86L, -78L, -74L, -72L, -68L, -60L, -52L, -48L, -48L, -46L, -46L, -44L, -40L, -38L, -26L, -18L, 2L, 4L, 4L, 10L, 14L, 14L, 16L, 20L, 38L, 48L, 48L, 50L, 56L, 60L, 66L, 74L, 76L, 80L, 84L, 88L, 96L, 96L, 96L), 37L, 
          144L
      )))
  
      cat(sprintf("TEST-8...%s\n",driver(
          list(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), 17L, 
          0L
      )))
  
      cat(sprintf("TEST-9...%s\n",driver(
          list(49L, 66L, 84L), 2L, 
          16L
      )))
  
  }
  
  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;
  }