title: Minimum Insertions Sort Array
signature: class Solution {
  
      public Integer minimumInsertionsSortArray(ArrayList<Integer> arr, Integer n) {
test_code: import static java.util.Map.entry;  
  import java.util.*;
  import java.lang.*;
  // TESTING CODE 
  // ============================================
  class QuestionEvaluator {
      public static String validateSolution(ArrayList<Integer> arr, Integer n, Integer expected){
          try{
              Driver driver = new Driver();
              if (driver.run(arr, n,expected)){
                  return "PASSED";
              }
              return "FAILED";
              
          } catch (Exception exception_obj){
              return exception_obj.getClass().getSimpleName();
          }
  
      }
      public static void main(String[] args) {
          String result;
          result = validateSolution(new ArrayList<>(Arrays.asList(4, 7, 20, 22, 23, 31, 33, 36, 47, 61, 63, 63, 71, 74, 82, 91, 95, 99)), 15, 0);
          System.out.println(String.format("%s%s","TEST-0...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(-84, 12, -42, -78, 22, 72, 56, 70, 28, -72)), 7, 3);
          System.out.println(String.format("%s%s","TEST-1...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(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)), 16, 0);
          System.out.println(String.format("%s%s","TEST-2...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(29, 41, 4, 62, 77, 74, 30, 2, 14, 90, 93, 10, 78, 36, 66, 22, 48, 89, 36, 73, 70, 23, 90)), 13, 7);
          System.out.println(String.format("%s%s","TEST-3...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(-80, -72, -68, -66, -58, -50, -48, -32, -28, -24, -22, -18, 0, 2, 6, 10, 12, 14, 14, 18, 24, 24, 24, 28, 28, 28, 34, 38, 42, 42, 46, 46, 46, 58, 80, 82, 82, 84, 84, 86, 88, 90, 92, 96)), 36, 0);
          System.out.println(String.format("%s%s","TEST-4...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(1, 1, 0, 1, 0, 1, 0, 0, 1, 0)), 8, 4);
          System.out.println(String.format("%s%s","TEST-5...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(26, 36, 58, 64, 69, 72, 79, 82, 82, 87, 89, 90, 95)), 8, 0);
          System.out.println(String.format("%s%s","TEST-6...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(-52, -40, 98, 40, 42, -50, 60, -64, -92, 36, -88, 72, -72, 38, -80, -52, 68, 70, 16, 22, -30, -74, 56, -80, 62, -54, -32, -22, -86, -70, 88, -76, -46, 28, 40, -2, -84, 68, -98, -16, 90, 36, -38, -86, 20, -40, 82, 98, 54)), 41, 30);
          System.out.println(String.format("%s%s","TEST-7...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1)), 10, 0);
          System.out.println(String.format("%s%s","TEST-8...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(36, 62, 58, 24, 99, 12, 46, 3, 4, 40, 54, 97, 48, 94, 98, 7, 17, 5, 3, 36, 3)), 16, 10);
          System.out.println(String.format("%s%s","TEST-9...",result));    
      
      }
  }
  
  class Driver{
      public boolean run(ArrayList<Integer> arr, Integer n, Integer expected){
  
          PLACEHOLDER_CLS_NAME sol = new PLACEHOLDER_CLS_NAME();
          return validateSolution(sol.PLACEHOLDER_FN_NAME(arr, n),expected);
      }
      
      public boolean validateSolution(Integer actual, Integer expected){
          return actual.equals(expected);
      }
  }
  
  
  // SOLUTION CODE
  // ============================================
  PLACEHOLDER_CODE_BODY
source_py: def minimum_insertions_sort_array(arr, N):
      lis = ([0] * N)
      for i in range(N):
          lis[i] = 1
      for i in range(1, N):
          for j in range(i):
              if ((arr[i] >= arr[j]) and (lis[i] < (lis[j] + 1))):
                  lis[i] = (lis[j] + 1)
      max = 0
      for i in range(N):
          if (max < lis[i]):
              max = lis[i]
      return (N - max)
source_cpp: int minimum_insertions_sort_array(vector<int> arr, int N) {
    int lis [ N ];
    for ( int i = 0;
    i < N;
    i ++ ) lis [ i ] = 1;
    for ( int i = 1;
    i < N;
    i ++ ) for ( int j = 0;
    j < i;
    j ++ ) if ( arr [ i ] >= arr [ j ] && lis [ i ] < lis [ j ] + 1 ) lis [ i ] = lis [ j ] + 1;
    int max = 0;
    for ( int i = 0;
    i < N;
    i ++ ) if ( max < lis [ i ] ) max = lis [ i ];
    return ( N - max );
  }