title: Elements To Be Added So That All Elements Of A Range Are Present In Array
signature: def elementsToBeAddedSoThatAllElementsOfARangeArePresentInArray(arr: List[Int], n: Int) = {
test_code: import scala.collection.mutable.HashMap
  import scala.collection.immutable.HashSet
  
  
  // SOLUTION CODE
  // ============================================
  PLACEHOLDER_CODE_BODY
  
  
  // TESTING CODE 
  // ============================================
  def validateSolution(actual: Int , expected: Int): Boolean = {
      actual.toString == expected.toString
  }
  
  def driver(arr: List[Int], n: Int, expected: Int): String = {
      try {
          if (validateSolution(PLACEHOLDER_FN_NAME(arr, n),expected)) {
              "PASSED"
          } else{
              "FAILED"
          }
      }catch{
          case e: Exception => {e.getClass.getName}
      }
  
  }
  object QuestionEvaluator extends App {
  
      println(s"TEST-0...${driver(
        List(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    
      )}")   
  
  
      println(s"TEST-1...${driver(
        List(-90, -90, -88, -84, -84, -84, -80, -74, -52, -26, -26, -16, -8, 6, 14, 16, 34, 64, 72, 92, 94), 17, 
        112    
      )}")   
  
  
      println(s"TEST-2...${driver(
        List(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    
      )}")   
  
  
      println(s"TEST-3...${driver(
        List(10, 12, 12, 13, 13, 23, 25, 26, 29, 32, 39, 43, 43, 54, 62, 79, 92), 14, 
        34    
      )}")   
  
  
      println(s"TEST-4...${driver(
        List(-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    
      )}")   
  
  
      println(s"TEST-5...${driver(
        List(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    
      )}")   
  
  
      println(s"TEST-6...${driver(
        List(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    
      )}")   
  
  
      println(s"TEST-7...${driver(
        List(-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    
      )}")   
  
  
      println(s"TEST-8...${driver(
        List(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    
      )}")   
  
  
      println(s"TEST-9...${driver(
        List(49, 66, 84), 2, 
        16    
      )}")   
  
  }
  
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;
  }