title: Elements To Be Added So That All Elements Of A Range Are Present In Array
signature: func elementsToBeAddedSoThatAllElementsOfARangeArePresentInArray(arr []int, n int) int {
test_code: package main
  
  import (
      "fmt"
      "reflect"
      "math"
      "sort"
  )
  
  // SOLUTION CODE
  // ===================================
  PLACEHOLDER_CODE_BODY
  
  // TESTING CODE
  // ===================================
  func validateSolution(actual int, expected int) bool {
      return reflect.DeepEqual(actual,expected)
  }
  
  func driver(arr []int, n int, expected int) string {
      if validateSolution(PLACEHOLDER_FN_NAME(arr, n),expected) {
              return "PASSED"
      }
      return "FAILED"
  
  }
  
  func main() {
  
      // Use imports to avoid undefined errors
      var _ = reflect.DeepEqual
      _ = math.Abs
      _ = sort.Sort
      result := ""
      result = driver([]int{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)
      fmt.Printf("TEST-0...%s\n", result)
      
      result = driver([]int{-90, -90, -88, -84, -84, -84, -80, -74, -52, -26, -26, -16, -8, 6, 14, 16, 34, 64, 72, 92, 94}, 
  	17, 112)
      fmt.Printf("TEST-1...%s\n", result)
      
      result = driver([]int{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)
      fmt.Printf("TEST-2...%s\n", result)
      
      result = driver([]int{10, 12, 12, 13, 13, 23, 25, 26, 29, 32, 39, 43, 43, 54, 62, 79, 92}, 
  	14, 34)
      fmt.Printf("TEST-3...%s\n", result)
      
      result = driver([]int{-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)
      fmt.Printf("TEST-4...%s\n", result)
      
      result = driver([]int{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)
      fmt.Printf("TEST-5...%s\n", result)
      
      result = driver([]int{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)
      fmt.Printf("TEST-6...%s\n", result)
      
      result = driver([]int{-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)
      fmt.Printf("TEST-7...%s\n", result)
      
      result = driver([]int{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)
      fmt.Printf("TEST-8...%s\n", result)
      
      result = driver([]int{49, 66, 84}, 
  	2, 16)
      fmt.Printf("TEST-9...%s\n", result)
      
          
  }
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;
  }