title: Position Element Stable Sort
signature: position_element_stable_sort <- function(arr, n, idx) {
test_code: 
  
  
  # SOLUTION CODE
  # ============================================
  PLACEHOLDER_CODE_BODY
  
  # TESTING CODE 
  # ============================================
  validate_solution <- function(actual, expected) {
      toString(actual)==toString(expected)
  }
  
  driver <- function(arr, n, idx, expected){
      if (validate_solution(PLACEHOLDER_FN_NAME(arr, n, idx), expected)){
          "PASSED"
      } else{
          "FAILED"
      }
  }
  
  main <- function() {
      cat(sprintf("TEST-0...%s\n",driver(
          list(4L, 8L, 9L, 12L, 15L, 16L, 18L, 28L, 28L, 31L, 33L, 36L, 36L, 37L, 40L, 41L, 44L, 44L, 46L, 50L, 50L, 50L, 52L, 52L, 54L, 55L, 60L, 61L, 65L, 68L, 71L, 75L, 75L, 78L, 81L, 84L, 87L, 89L, 90L, 92L, 94L, 97L, 97L, 98L, 98L, 99L), 37L, 32L, 
          32L
      )))
  
      cat(sprintf("TEST-1...%s\n",driver(
          list(-16L, 86L, 94L, -86L, -38L, 64L, 96L, -64L, 94L, 10L, -10L, -62L, -50L, -46L, -62L, -32L, -4L, 72L, 14L, 36L, 74L, -66L, 46L, 82L, -44L, -22L, -26L, 16L, -8L, 0L, -90L, 94L, -50L, 22L, -82L, 8L, 92L, -84L, -34L, -36L, -66L), 31L, 27L, 
          20L
      )))
  
      cat(sprintf("TEST-2...%s\n",driver(
          list(0L, 0L, 0L, 0L, 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), 30L, 34L, 
          30L
      )))
  
      cat(sprintf("TEST-3...%s\n",driver(
          list(66L, 8L, 30L, 84L, 36L, 96L, 45L, 63L, 23L, 23L, 14L, 34L, 86L, 51L, 18L, 97L, 21L, 39L, 96L, 70L, 28L, 96L, 78L, 68L, 88L, 66L, 13L, 24L, 74L, 94L), 26L, 21L, 
          24L
      )))
  
      cat(sprintf("TEST-4...%s\n",driver(
          list(-94L, -90L, -86L, -86L, -72L, -72L, -58L, -50L, -32L, -22L, -18L, -10L, -4L, -2L, -2L, 0L, 0L, 6L, 14L, 22L, 22L, 36L, 36L, 40L, 44L, 58L, 60L, 70L, 70L, 76L, 82L, 82L, 84L, 88L, 96L), 17L, 31L, 
          17L
      )))
  
      cat(sprintf("TEST-5...%s\n",driver(
          list(1L, 1L, 1L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 1L, 0L, 1L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 1L, 1L), 30L, 36L, 
          14L
      )))
  
      cat(sprintf("TEST-6...%s\n",driver(
          list(3L, 5L, 6L, 7L, 8L, 10L, 17L, 20L, 20L, 26L, 27L, 27L, 27L, 32L, 32L, 38L, 40L, 44L, 45L, 45L, 45L, 45L, 47L, 50L, 57L, 57L, 57L, 58L, 62L, 63L, 63L, 67L, 68L, 73L, 75L, 76L, 77L, 79L, 79L, 80L, 85L, 88L, 89L, 89L, 89L, 94L, 96L, 98L), 42L, 35L, 
          35L
      )))
  
      cat(sprintf("TEST-7...%s\n",driver(
          list(98L, -92L, 18L, -18L, 44L, -88L, -90L, -66L, -38L, 78L, -22L, -46L, -20L, 64L, -10L, 54L), 14L, 12L, 
          7L
      )))
  
      cat(sprintf("TEST-8...%s\n",driver(
          list(0L, 0L, 0L, 0L, 0L, 0L, 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), 19L, 31L, 
          19L
      )))
  
      cat(sprintf("TEST-9...%s\n",driver(
          list(14L, 17L), 1L, 1L, 
          1L
      )))
  
  }
  
  main()
source_py: def position_element_stable_sort(arr, n, idx):
      result = 0
      for i in range(n):
          if (arr[i] < arr[idx]):
              result += 1
          if ((arr[i] == arr[idx]) and (i < idx)):
              result += 1
      return result
source_cpp: int position_element_stable_sort(vector<int> arr, int n, int idx) {
    int result = 0;
    for ( int i = 0;
    i < n;
    i ++ ) {
      if ( arr [ i ] < arr [ idx ] ) result ++;
      if ( arr [ i ] == arr [ idx ] && i < idx ) result ++;
    }
    return result;
  }