package com.radix2.algorithms.week3

// Merging with smaller auxiliary array.
// Suppose that the sub array 𝚊[𝟶] to 𝚊[𝚗−𝟷] is sorted and the subarray 𝚊[𝚗] to 𝚊[𝟸 ∗ 𝚗 − 𝟷] is sorted.
// How can you merge the two sub arrays so that 𝚊[𝟶] to 𝚊[𝟸 ∗ 𝚗−𝟷] is sorted using an auxiliary array of length n (instead of 2n)?

fun mergeWithSmallerAux(array: IntArray, aux: IntArray = IntArray(array.size / 2), lo: Int, mid: Int, hi: Int) {
    System.arraycopy(array, lo, aux, lo, aux.size)

    var i = lo
    var j = mid + 1

    var k = 0

    while(i <= mid && j <= hi) {
        if (aux[i] > array[j]) {
            array[k++] = array[j++]
        } else {
            array[k++] = aux[i++]
        }
    }

    while(i <= mid)
        array[k++] = aux[i++]

    while(j <= hi)
        array[k++] = array[j++]
}

fun main(args: Array<String>) {

    val array = intArrayOf(1, 3, 5, 7, 9, 11, 0, 2, 4, 6, 8, 10)

    mergeWithSmallerAux(array, lo = 0, mid = (array.size - 1) / 2, hi = array.size - 1)

    println(array.joinToString())
}

Compiled from "HalfSizedAuxArray.kt"
public final class com.radix2.algorithms.week3.HalfSizedAuxArrayKt {
  public static final void mergeWithSmallerAux(int[], int[], int, int, int);
    Code:
       0: aload_0
       1: ldc           #9                  // String array
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_1
       7: ldc           #17                 // String aux
       9: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      12: aload_0
      13: iload_2
      14: aload_1
      15: iload_2
      16: aload_1
      17: arraylength
      18: invokestatic  #23                 // Method java/lang/System.arraycopy:(Ljava/lang/Object;ILjava/lang/Object;II)V
      21: iload_2
      22: istore        5
      24: iload_3
      25: iconst_1
      26: iadd
      27: istore        6
      29: iconst_0
      30: istore        7
      32: iload         5
      34: iload_3
      35: if_icmpgt     90
      38: iload         6
      40: iload         4
      42: if_icmpgt     90
      45: aload_1
      46: iload         5
      48: iaload
      49: aload_0
      50: iload         6
      52: iaload
      53: if_icmple     73
      56: aload_0
      57: iload         7
      59: iinc          7, 1
      62: aload_0
      63: iload         6
      65: iinc          6, 1
      68: iaload
      69: iastore
      70: goto          32
      73: aload_0
      74: iload         7
      76: iinc          7, 1
      79: aload_1
      80: iload         5
      82: iinc          5, 1
      85: iaload
      86: iastore
      87: goto          32
      90: iload         5
      92: iload_3
      93: if_icmpgt     113
      96: aload_0
      97: iload         7
      99: iinc          7, 1
     102: aload_1
     103: iload         5
     105: iinc          5, 1
     108: iaload
     109: iastore
     110: goto          90
     113: iload         6
     115: iload         4
     117: if_icmpgt     137
     120: aload_0
     121: iload         7
     123: iinc          7, 1
     126: aload_0
     127: iload         6
     129: iinc          6, 1
     132: iaload
     133: iastore
     134: goto          113
     137: return

  public static void mergeWithSmallerAux$default(int[], int[], int, int, int, int, java.lang.Object);
    Code:
       0: iload         5
       2: iconst_2
       3: iand
       4: ifeq          14
       7: aload_0
       8: arraylength
       9: iconst_2
      10: idiv
      11: newarray       int
      13: astore_1
      14: aload_0
      15: aload_1
      16: iload_2
      17: iload_3
      18: iload         4
      20: invokestatic  #35                 // Method mergeWithSmallerAux:([I[IIII)V
      23: return

  public static final void main(java.lang.String[]);
    Code:
       0: aload_0
       1: ldc           #39                 // String args
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: bipush        12
       8: newarray       int
      10: astore_2
      11: aload_2
      12: iconst_0
      13: iconst_1
      14: iastore
      15: aload_2
      16: iconst_1
      17: iconst_3
      18: iastore
      19: aload_2
      20: iconst_2
      21: iconst_5
      22: iastore
      23: aload_2
      24: iconst_3
      25: bipush        7
      27: iastore
      28: aload_2
      29: iconst_4
      30: bipush        9
      32: iastore
      33: aload_2
      34: iconst_5
      35: bipush        11
      37: iastore
      38: aload_2
      39: bipush        6
      41: iconst_0
      42: iastore
      43: aload_2
      44: bipush        7
      46: iconst_2
      47: iastore
      48: aload_2
      49: bipush        8
      51: iconst_4
      52: iastore
      53: aload_2
      54: bipush        9
      56: bipush        6
      58: iastore
      59: aload_2
      60: bipush        10
      62: bipush        8
      64: iastore
      65: aload_2
      66: bipush        11
      68: bipush        10
      70: iastore
      71: aload_2
      72: astore_1
      73: aload_1
      74: aconst_null
      75: iconst_0
      76: aload_1
      77: arraylength
      78: iconst_1
      79: isub
      80: iconst_2
      81: idiv
      82: aload_1
      83: arraylength
      84: iconst_1
      85: isub
      86: iconst_2
      87: aconst_null
      88: invokestatic  #41                 // Method mergeWithSmallerAux$default:([I[IIIIILjava/lang/Object;)V
      91: aload_1
      92: aconst_null
      93: aconst_null
      94: aconst_null
      95: iconst_0
      96: aconst_null
      97: aconst_null
      98: bipush        63
     100: aconst_null
     101: invokestatic  #47                 // Method kotlin/collections/ArraysKt.joinToString$default:([ILjava/lang/CharSequence;Ljava/lang/CharSequence;Ljava/lang/CharSequence;ILjava/lang/CharSequence;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Ljava/lang/String;
     104: getstatic     #51                 // Field java/lang/System.out:Ljava/io/PrintStream;
     107: swap
     108: invokevirtual #57                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     111: return
}


package com.radix2.algorithms.week3

import java.util.*

// Counting inversions. An inversion in an array a[] is a pair of entries a[i] and a[j] such that i < j but a[i] > a[j].
// Given an array, design a linear arithmetic algorithm to count the number of inversions.

fun sort(array: IntArray, aux: IntArray, lo: Int, hi: Int): Long {
    if (lo >= hi) return 0

    val mid = lo + (hi - lo) / 2

    var inversions: Long

    inversions = sort(array, aux, lo, mid)

    inversions += sort(array, aux, mid + 1, hi)

    return inversions + merge(array, aux, lo, mid, hi)
}

fun merge(array: IntArray, aux: IntArray, lo: Int, mid: Int, hi: Int): Long {
    System.arraycopy(array, lo, aux, lo, (hi - lo) + 1)

    var i = lo
    var j = mid + 1

    val nextToMid = j
    var inversions = 0L

    for (k in lo..hi) {
        when {
            i > mid -> array[k] = aux[j++]
            j > hi -> array[k] = aux[i++]
            aux[i] > aux[j] -> {
                array[k] = aux[j++]
                inversions += nextToMid - i
            }
            else -> array[k] = aux[i++]
        }
    }

    return inversions
}

fun countInversions(array: IntArray): Long {
    return sort(array, IntArray(array.size), 0, array.size - 1)
}

fun main(args: Array<String>) {
    val scan = Scanner(System.`in`)

    val t = scan.nextLine().trim().toInt()

    for (tItr in 1..t) {
        val n = scan.nextLine().trim().toInt()

        val arr = scan.nextLine().split(" ").map{ it.trim().toInt() }.toIntArray()

        val result = countInversions(arr)

        println(result)
    }
}

Compiled from "CountingInversionsV2.kt"
public final class com.radix2.algorithms.week3.CountingInversionsV2Kt {
  public static final long sort(int[], int[], int, int);
    Code:
       0: aload_0
       1: ldc           #9                  // String array
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_1
       7: ldc           #17                 // String aux
       9: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      12: iload_2
      13: iload_3
      14: if_icmplt     19
      17: lconst_0
      18: lreturn
      19: iload_2
      20: iload_3
      21: iload_2
      22: isub
      23: iconst_2
      24: idiv
      25: iadd
      26: istore        4
      28: lconst_0
      29: lstore        5
      31: aload_0
      32: aload_1
      33: iload_2
      34: iload         4
      36: invokestatic  #19                 // Method sort:([I[III)J
      39: lstore        5
      41: lload         5
      43: aload_0
      44: aload_1
      45: iload         4
      47: iconst_1
      48: iadd
      49: iload_3
      50: invokestatic  #19                 // Method sort:([I[III)J
      53: ladd
      54: lstore        5
      56: lload         5
      58: aload_0
      59: aload_1
      60: iload_2
      61: iload         4
      63: iload_3
      64: invokestatic  #23                 // Method merge:([I[IIII)J
      67: ladd
      68: lreturn

  public static final long merge(int[], int[], int, int, int);
    Code:
       0: aload_0
       1: ldc           #9                  // String array
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_1
       7: ldc           #17                 // String aux
       9: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      12: aload_0
      13: iload_2
      14: aload_1
      15: iload_2
      16: iload         4
      18: iload_2
      19: isub
      20: iconst_1
      21: iadd
      22: invokestatic  #36                 // Method java/lang/System.arraycopy:(Ljava/lang/Object;ILjava/lang/Object;II)V
      25: iload_2
      26: istore        5
      28: iload_3
      29: iconst_1
      30: iadd
      31: istore        6
      33: iload         6
      35: istore        7
      37: lconst_0
      38: lstore        8
      40: iload_2
      41: istore        10
      43: iload         10
      45: iload         4
      47: if_icmpgt     152
      50: nop
      51: iload         5
      53: iload_3
      54: if_icmple     71
      57: aload_0
      58: iload         10
      60: aload_1
      61: iload         6
      63: iinc          6, 1
      66: iaload
      67: iastore
      68: goto          139
      71: iload         6
      73: iload         4
      75: if_icmple     92
      78: aload_0
      79: iload         10
      81: aload_1
      82: iload         5
      84: iinc          5, 1
      87: iaload
      88: iastore
      89: goto          139
      92: aload_1
      93: iload         5
      95: iaload
      96: aload_1
      97: iload         6
      99: iaload
     100: if_icmple     128
     103: aload_0
     104: iload         10
     106: aload_1
     107: iload         6
     109: iinc          6, 1
     112: iaload
     113: iastore
     114: lload         8
     116: iload         7
     118: iload         5
     120: isub
     121: i2l
     122: ladd
     123: lstore        8
     125: goto          139
     128: aload_0
     129: iload         10
     131: aload_1
     132: iload         5
     134: iinc          5, 1
     137: iaload
     138: iastore
     139: iload         10
     141: iload         4
     143: if_icmpeq     152
     146: iinc          10, 1
     149: goto          50
     152: lload         8
     154: lreturn

  public static final long countInversions(int[]);
    Code:
       0: aload_0
       1: ldc           #9                  // String array
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: aload_0
       8: arraylength
       9: newarray       int
      11: iconst_0
      12: aload_0
      13: arraylength
      14: iconst_1
      15: isub
      16: invokestatic  #19                 // Method sort:([I[III)J
      19: lreturn

  public static final void main(java.lang.String[]);
    Code:
       0: aload_0
       1: ldc           #47                 // String args
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: new           #49                 // class java/util/Scanner
       9: dup
      10: getstatic     #53                 // Field java/lang/System.in:Ljava/io/InputStream;
      13: invokespecial #57                 // Method java/util/Scanner."<init>":(Ljava/io/InputStream;)V
      16: astore_1
      17: aload_1
      18: invokevirtual #61                 // Method java/util/Scanner.nextLine:()Ljava/lang/String;
      21: dup
      22: ldc           #63                 // String nextLine(...)
      24: invokestatic  #66                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
      27: checkcast     #68                 // class java/lang/CharSequence
      30: invokestatic  #74                 // Method kotlin/text/StringsKt.trim:(Ljava/lang/CharSequence;)Ljava/lang/CharSequence;
      33: invokevirtual #77                 // Method java/lang/Object.toString:()Ljava/lang/String;
      36: invokestatic  #83                 // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
      39: istore_2
      40: iconst_1
      41: istore_3
      42: iload_3
      43: iload_2
      44: if_icmpgt     253
      47: aload_1
      48: invokevirtual #61                 // Method java/util/Scanner.nextLine:()Ljava/lang/String;
      51: dup
      52: ldc           #63                 // String nextLine(...)
      54: invokestatic  #66                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
      57: checkcast     #68                 // class java/lang/CharSequence
      60: invokestatic  #74                 // Method kotlin/text/StringsKt.trim:(Ljava/lang/CharSequence;)Ljava/lang/CharSequence;
      63: invokevirtual #77                 // Method java/lang/Object.toString:()Ljava/lang/String;
      66: invokestatic  #83                 // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
      69: istore        4
      71: aload_1
      72: invokevirtual #61                 // Method java/util/Scanner.nextLine:()Ljava/lang/String;
      75: dup
      76: ldc           #63                 // String nextLine(...)
      78: invokestatic  #66                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
      81: checkcast     #68                 // class java/lang/CharSequence
      84: iconst_1
      85: anewarray     #85                 // class java/lang/String
      88: astore        6
      90: aload         6
      92: iconst_0
      93: ldc           #87                 // String
      95: aastore
      96: aload         6
      98: iconst_0
      99: iconst_0
     100: bipush        6
     102: aconst_null
     103: invokestatic  #91                 // Method kotlin/text/StringsKt.split$default:(Ljava/lang/CharSequence;[Ljava/lang/String;ZIILjava/lang/Object;)Ljava/util/List;
     106: checkcast     #93                 // class java/lang/Iterable
     109: astore        6
     111: iconst_0
     112: istore        7
     114: aload         6
     116: astore        8
     118: new           #95                 // class java/util/ArrayList
     121: dup
     122: aload         6
     124: bipush        10
     126: invokestatic  #101                // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
     129: invokespecial #104                // Method java/util/ArrayList."<init>":(I)V
     132: checkcast     #106                // class java/util/Collection
     135: astore        9
     137: iconst_0
     138: istore        10
     140: aload         8
     142: invokeinterface #110,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     147: astore        11
     149: aload         11
     151: invokeinterface #116,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
     156: ifeq          213
     159: aload         11
     161: invokeinterface #120,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     166: astore        12
     168: aload         9
     170: aload         12
     172: checkcast     #85                 // class java/lang/String
     175: astore        13
     177: astore        15
     179: iconst_0
     180: istore        14
     182: nop
     183: aload         13
     185: checkcast     #68                 // class java/lang/CharSequence
     188: invokestatic  #74                 // Method kotlin/text/StringsKt.trim:(Ljava/lang/CharSequence;)Ljava/lang/CharSequence;
     191: invokevirtual #77                 // Method java/lang/Object.toString:()Ljava/lang/String;
     194: invokestatic  #83                 // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
     197: nop
     198: invokestatic  #124                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     201: aload         15
     203: swap
     204: invokeinterface #128,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     209: pop
     210: goto          149
     213: aload         9
     215: checkcast     #130                // class java/util/List
     218: nop
     219: checkcast     #106                // class java/util/Collection
     222: invokestatic  #134                // Method kotlin/collections/CollectionsKt.toIntArray:(Ljava/util/Collection;)[I
     225: astore        5
     227: aload         5
     229: invokestatic  #136                // Method countInversions:([I)J
     232: lstore        6
     234: getstatic     #140                // Field java/lang/System.out:Ljava/io/PrintStream;
     237: lload         6
     239: invokevirtual #146                // Method java/io/PrintStream.println:(J)V
     242: iload_3
     243: iload_2
     244: if_icmpeq     253
     247: iinc          3, 1
     250: goto          47
     253: return
}


package com.radix2.algorithms.week3

import java.util.*

fun less(lhs: Int, rhs: Int) = lhs < rhs

fun exch(array: IntArray, i: Int, j: Int) {
    val temp = array[i]
    array[i] = array[j]
    array[j] = temp
}

fun shuffle(array: IntArray) {
    val rnd = Random()

    for (i in 1 until array.size) {
        val randomIndex = rnd.nextInt(i)
        exch(array, i, randomIndex)
    }
}

fun quickSort(array: IntArray) {
    shuffle(array)

    quickSort(array, 0, array.size - 1)
}

fun quickSort(array: IntArray, lo: Int, hi: Int) {
    if (hi <= lo) return

    val k = partition(array, lo, hi)

    quickSort(array, lo, k - 1)
    quickSort(array, k + 1, hi)
}

fun partition(array: IntArray, lo: Int, hi: Int): Int {
    val pivot = array[lo]
    var i = lo
    var j = hi + 1

    while (true) {

        while (less(array[++i], pivot))
            if (i == hi) break

        while (less(pivot, array[--j]))
            if (j == lo) break

        if (i >= j) break

        exch(array, i, j)
    }

    exch(array, lo, j)

    return j
}

// 0, 1, 2, 3, 4, 5, 6, 7, 9
fun main(args: Array<String>) {
    val array = intArrayOf(6, 10, 1, 3, 2, 6, 8, 9, 5, 15)

    quickSort(array)

    println(array.joinToString(", "))
}

Compiled from "TraditionalQuickSort.kt"
public final class com.radix2.algorithms.week3.TraditionalQuickSortKt {
  public static final boolean less(int, int);
    Code:
       0: iload_0
       1: iload_1
       2: if_icmpge     9
       5: iconst_1
       6: goto          10
       9: iconst_0
      10: ireturn

  public static final void exch(int[], int, int);
    Code:
       0: aload_0
       1: ldc           #14                 // String array
       3: invokestatic  #20                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: iload_1
       8: iaload
       9: istore_3
      10: aload_0
      11: iload_1
      12: aload_0
      13: iload_2
      14: iaload
      15: iastore
      16: aload_0
      17: iload_2
      18: iload_3
      19: iastore
      20: return

  public static final void shuffle(int[]);
    Code:
       0: aload_0
       1: ldc           #14                 // String array
       3: invokestatic  #20                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: new           #28                 // class java/util/Random
       9: dup
      10: invokespecial #32                 // Method java/util/Random."<init>":()V
      13: astore_1
      14: iconst_1
      15: istore_2
      16: aload_0
      17: arraylength
      18: istore_3
      19: iload_2
      20: iload_3
      21: if_icmpge     44
      24: aload_1
      25: iload_2
      26: invokevirtual #36                 // Method java/util/Random.nextInt:(I)I
      29: istore        4
      31: aload_0
      32: iload_2
      33: iload         4
      35: invokestatic  #38                 // Method exch:([III)V
      38: iinc          2, 1
      41: goto          19
      44: return

  public static final void quickSort(int[]);
    Code:
       0: aload_0
       1: ldc           #14                 // String array
       3: invokestatic  #20                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: invokestatic  #44                 // Method shuffle:([I)V
      10: aload_0
      11: iconst_0
      12: aload_0
      13: arraylength
      14: iconst_1
      15: isub
      16: invokestatic  #46                 // Method quickSort:([III)V
      19: return

  public static final void quickSort(int[], int, int);
    Code:
       0: aload_0
       1: ldc           #14                 // String array
       3: invokestatic  #20                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: iload_2
       7: iload_1
       8: if_icmpgt     12
      11: return
      12: aload_0
      13: iload_1
      14: iload_2
      15: invokestatic  #50                 // Method partition:([III)I
      18: istore_3
      19: aload_0
      20: iload_1
      21: iload_3
      22: iconst_1
      23: isub
      24: invokestatic  #46                 // Method quickSort:([III)V
      27: aload_0
      28: iload_3
      29: iconst_1
      30: iadd
      31: iload_2
      32: invokestatic  #46                 // Method quickSort:([III)V
      35: return

  public static final int partition(int[], int, int);
    Code:
       0: aload_0
       1: ldc           #14                 // String array
       3: invokestatic  #20                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: iload_1
       8: iaload
       9: istore_3
      10: iload_1
      11: istore        4
      13: iload_2
      14: iconst_1
      15: iadd
      16: istore        5
      18: nop
      19: aload_0
      20: iinc          4, 1
      23: iload         4
      25: iaload
      26: iload_3
      27: invokestatic  #55                 // Method less:(II)Z
      30: ifeq          42
      33: iload         4
      35: iload_2
      36: if_icmpne     19
      39: goto          42
      42: iload_3
      43: aload_0
      44: iinc          5, -1
      47: iload         5
      49: iaload
      50: invokestatic  #55                 // Method less:(II)Z
      53: ifeq          65
      56: iload         5
      58: iload_1
      59: if_icmpne     42
      62: goto          65
      65: iload         4
      67: iload         5
      69: if_icmpge     83
      72: aload_0
      73: iload         4
      75: iload         5
      77: invokestatic  #38                 // Method exch:([III)V
      80: goto          18
      83: aload_0
      84: iload_1
      85: iload         5
      87: invokestatic  #38                 // Method exch:([III)V
      90: iload         5
      92: ireturn

  public static final void main(java.lang.String[]);
    Code:
       0: aload_0
       1: ldc           #60                 // String args
       3: invokestatic  #20                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: bipush        10
       8: newarray       int
      10: astore_2
      11: aload_2
      12: iconst_0
      13: bipush        6
      15: iastore
      16: aload_2
      17: iconst_1
      18: bipush        10
      20: iastore
      21: aload_2
      22: iconst_2
      23: iconst_1
      24: iastore
      25: aload_2
      26: iconst_3
      27: iconst_3
      28: iastore
      29: aload_2
      30: iconst_4
      31: iconst_2
      32: iastore
      33: aload_2
      34: iconst_5
      35: bipush        6
      37: iastore
      38: aload_2
      39: bipush        6
      41: bipush        8
      43: iastore
      44: aload_2
      45: bipush        7
      47: bipush        9
      49: iastore
      50: aload_2
      51: bipush        8
      53: iconst_5
      54: iastore
      55: aload_2
      56: bipush        9
      58: bipush        15
      60: iastore
      61: aload_2
      62: astore_1
      63: aload_1
      64: invokestatic  #62                 // Method quickSort:([I)V
      67: aload_1
      68: ldc           #64                 // String ,
      70: checkcast     #66                 // class java/lang/CharSequence
      73: aconst_null
      74: aconst_null
      75: iconst_0
      76: aconst_null
      77: aconst_null
      78: bipush        62
      80: aconst_null
      81: invokestatic  #72                 // Method kotlin/collections/ArraysKt.joinToString$default:([ILjava/lang/CharSequence;Ljava/lang/CharSequence;Ljava/lang/CharSequence;ILjava/lang/CharSequence;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Ljava/lang/String;
      84: getstatic     #78                 // Field java/lang/System.out:Ljava/io/PrintStream;
      87: swap
      88: invokevirtual #84                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      91: return
}


package com.radix2.algorithms.week3

import java.util.*

class NutsAndBolts {

    fun solve(nuts: IntArray, bolts: IntArray) {
        shuffle(nuts)
        shuffle(bolts)

        solve(nuts, bolts, 0, nuts.size - 1)
    }

    private fun solve(nuts: IntArray, bolts: IntArray, lo: Int, hi: Int) {
        if (lo >= hi) return

        val pivot = partition(nuts, bolts[lo], lo, hi)
        partition(bolts, nuts[pivot], lo, hi)

        solve(nuts, bolts, lo, pivot - 1)
        solve(nuts, bolts, pivot + 1, hi)
    }

    private fun partition(array: IntArray, pivot: Int, lo: Int, hi: Int): Int {
        var i = lo
        var j = hi + 1

        while (true) {
            while (array[++i] <= pivot) {
                if (i == hi) break

                if (array[i] == pivot) {
                    exch(array, lo, i)
                    i--
                }
            }

            while (pivot <= array[--j]) {
                if (j == lo) break

                if (array[j] == pivot) {
                    exch(array, lo, j)
                    j++
                }
            }

            if (i >= j) break

            exch(array, i, j)
        }

        exch(array, lo, j)

        return j
    }

    private fun exch(array: IntArray, i: Int, j: Int) {
        val temp = array[i]
        array[i] = array[j]
        array[j] = temp
    }

    private fun shuffle(array: IntArray) {
        val rnd = Random()

        for (i in 1 until array.size) {
            val randomIndex = rnd.nextInt(i)
            exch(array, i, randomIndex)
        }
    }
}

fun main(args: Array<String>) {

    val nuts = intArrayOf(5, 3, 7, 9, 2, 8, 4, 1, 6, 0, 10, 11, 12, 13, 14, 15, 16, 17)
    val bolts = intArrayOf(4, 7, 1, 6, 3, 0, 8, 9, 2, 5, 10, 11, 12, 13, 14, 15, 16, 17)

    NutsAndBolts().solve(nuts, bolts)

    println("""
        Nuts    : ${nuts.joinToString(separator = ", ")}
        Bolts   : ${bolts.joinToString(separator = ", ")}
    """.trimIndent())
}

Compiled from "NutsAndBolts.kt"
public final class com.radix2.algorithms.week3.NutsAndBoltsKt {
  public static final void main(java.lang.String[]);
    Code:
       0: aload_0
       1: ldc           #9                  // String args
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: bipush        18
       8: newarray       int
      10: astore_2
      11: aload_2
      12: iconst_0
      13: iconst_5
      14: iastore
      15: aload_2
      16: iconst_1
      17: iconst_3
      18: iastore
      19: aload_2
      20: iconst_2
      21: bipush        7
      23: iastore
      24: aload_2
      25: iconst_3
      26: bipush        9
      28: iastore
      29: aload_2
      30: iconst_4
      31: iconst_2
      32: iastore
      33: aload_2
      34: iconst_5
      35: bipush        8
      37: iastore
      38: aload_2
      39: bipush        6
      41: iconst_4
      42: iastore
      43: aload_2
      44: bipush        7
      46: iconst_1
      47: iastore
      48: aload_2
      49: bipush        8
      51: bipush        6
      53: iastore
      54: aload_2
      55: bipush        9
      57: iconst_0
      58: iastore
      59: aload_2
      60: bipush        10
      62: bipush        10
      64: iastore
      65: aload_2
      66: bipush        11
      68: bipush        11
      70: iastore
      71: aload_2
      72: bipush        12
      74: bipush        12
      76: iastore
      77: aload_2
      78: bipush        13
      80: bipush        13
      82: iastore
      83: aload_2
      84: bipush        14
      86: bipush        14
      88: iastore
      89: aload_2
      90: bipush        15
      92: bipush        15
      94: iastore
      95: aload_2
      96: bipush        16
      98: bipush        16
     100: iastore
     101: aload_2
     102: bipush        17
     104: bipush        17
     106: iastore
     107: aload_2
     108: astore_1
     109: bipush        18
     111: newarray       int
     113: astore_3
     114: aload_3
     115: iconst_0
     116: iconst_4
     117: iastore
     118: aload_3
     119: iconst_1
     120: bipush        7
     122: iastore
     123: aload_3
     124: iconst_2
     125: iconst_1
     126: iastore
     127: aload_3
     128: iconst_3
     129: bipush        6
     131: iastore
     132: aload_3
     133: iconst_4
     134: iconst_3
     135: iastore
     136: aload_3
     137: iconst_5
     138: iconst_0
     139: iastore
     140: aload_3
     141: bipush        6
     143: bipush        8
     145: iastore
     146: aload_3
     147: bipush        7
     149: bipush        9
     151: iastore
     152: aload_3
     153: bipush        8
     155: iconst_2
     156: iastore
     157: aload_3
     158: bipush        9
     160: iconst_5
     161: iastore
     162: aload_3
     163: bipush        10
     165: bipush        10
     167: iastore
     168: aload_3
     169: bipush        11
     171: bipush        11
     173: iastore
     174: aload_3
     175: bipush        12
     177: bipush        12
     179: iastore
     180: aload_3
     181: bipush        13
     183: bipush        13
     185: iastore
     186: aload_3
     187: bipush        14
     189: bipush        14
     191: iastore
     192: aload_3
     193: bipush        15
     195: bipush        15
     197: iastore
     198: aload_3
     199: bipush        16
     201: bipush        16
     203: iastore
     204: aload_3
     205: bipush        17
     207: bipush        17
     209: iastore
     210: aload_3
     211: astore_2
     212: new           #17                 // class com/radix2/algorithms/week3/NutsAndBolts
     215: dup
     216: invokespecial #21                 // Method com/radix2/algorithms/week3/NutsAndBolts."<init>":()V
     219: aload_1
     220: aload_2
     221: invokevirtual #25                 // Method com/radix2/algorithms/week3/NutsAndBolts.solve:([I[I)V
     224: new           #27                 // class java/lang/StringBuilder
     227: dup
     228: invokespecial #28                 // Method java/lang/StringBuilder."<init>":()V
     231: ldc           #30                 // String \n        Nuts    :
     233: invokevirtual #34                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     236: aload_1
     237: ldc           #36                 // String ,
     239: checkcast     #38                 // class java/lang/CharSequence
     242: aconst_null
     243: aconst_null
     244: iconst_0
     245: aconst_null
     246: aconst_null
     247: bipush        62
     249: aconst_null
     250: invokestatic  #44                 // Method kotlin/collections/ArraysKt.joinToString$default:([ILjava/lang/CharSequence;Ljava/lang/CharSequence;Ljava/lang/CharSequence;ILjava/lang/CharSequence;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Ljava/lang/String;
     253: invokevirtual #34                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     256: ldc           #46                 // String \n        Bolts   :
     258: invokevirtual #34                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     261: aload_2
     262: ldc           #36                 // String ,
     264: checkcast     #38                 // class java/lang/CharSequence
     267: aconst_null
     268: aconst_null
     269: iconst_0
     270: aconst_null
     271: aconst_null
     272: bipush        62
     274: aconst_null
     275: invokestatic  #44                 // Method kotlin/collections/ArraysKt.joinToString$default:([ILjava/lang/CharSequence;Ljava/lang/CharSequence;Ljava/lang/CharSequence;ILjava/lang/CharSequence;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Ljava/lang/String;
     278: invokevirtual #34                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     281: ldc           #48                 // String \n
     283: invokevirtual #34                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     286: invokevirtual #52                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     289: invokestatic  #58                 // Method kotlin/text/StringsKt.trimIndent:(Ljava/lang/String;)Ljava/lang/String;
     292: astore_3
     293: getstatic     #64                 // Field java/lang/System.out:Ljava/io/PrintStream;
     296: aload_3
     297: invokevirtual #70                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     300: return
}

Compiled from "NutsAndBolts.kt"
public final class com.radix2.algorithms.week3.NutsAndBolts {
  public com.radix2.algorithms.week3.NutsAndBolts();
    Code:
       0: aload_0
       1: invokespecial #8                  // Method java/lang/Object."<init>":()V
       4: return

  public final void solve(int[], int[]);
    Code:
       0: aload_1
       1: ldc           #15                 // String nuts
       3: invokestatic  #21                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_2
       7: ldc           #23                 // String bolts
       9: invokestatic  #21                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      12: aload_0
      13: aload_1
      14: invokespecial #27                 // Method shuffle:([I)V
      17: aload_0
      18: aload_2
      19: invokespecial #27                 // Method shuffle:([I)V
      22: aload_0
      23: aload_1
      24: aload_2
      25: iconst_0
      26: aload_1
      27: arraylength
      28: iconst_1
      29: isub
      30: invokespecial #30                 // Method solve:([I[III)V
      33: return

  private final void solve(int[], int[], int, int);
    Code:
       0: iload_3
       1: iload         4
       3: if_icmplt     7
       6: return
       7: aload_0
       8: aload_1
       9: aload_2
      10: iload_3
      11: iaload
      12: iload_3
      13: iload         4
      15: invokespecial #35                 // Method partition:([IIII)I
      18: istore        5
      20: aload_0
      21: aload_2
      22: aload_1
      23: iload         5
      25: iaload
      26: iload_3
      27: iload         4
      29: invokespecial #35                 // Method partition:([IIII)I
      32: pop
      33: aload_0
      34: aload_1
      35: aload_2
      36: iload_3
      37: iload         5
      39: iconst_1
      40: isub
      41: invokespecial #30                 // Method solve:([I[III)V
      44: aload_0
      45: aload_1
      46: aload_2
      47: iload         5
      49: iconst_1
      50: iadd
      51: iload         4
      53: invokespecial #30                 // Method solve:([I[III)V
      56: return

  private final int partition(int[], int, int, int);
    Code:
       0: iload_3
       1: istore        5
       3: iload         4
       5: iconst_1
       6: iadd
       7: istore        6
       9: nop
      10: aload_1
      11: iinc          5, 1
      14: iload         5
      16: iaload
      17: iload_2
      18: if_icmpgt     50
      21: iload         5
      23: iload         4
      25: if_icmpeq     50
      28: aload_1
      29: iload         5
      31: iaload
      32: iload_2
      33: if_icmpne     10
      36: aload_0
      37: aload_1
      38: iload_3
      39: iload         5
      41: invokespecial #43                 // Method exch:([III)V
      44: iinc          5, -1
      47: goto          10
      50: iload_2
      51: aload_1
      52: iinc          6, -1
      55: iload         6
      57: iaload
      58: if_icmpgt     89
      61: iload         6
      63: iload_3
      64: if_icmpeq     89
      67: aload_1
      68: iload         6
      70: iaload
      71: iload_2
      72: if_icmpne     50
      75: aload_0
      76: aload_1
      77: iload_3
      78: iload         6
      80: invokespecial #43                 // Method exch:([III)V
      83: iinc          6, 1
      86: goto          50
      89: iload         5
      91: iload         6
      93: if_icmpge     108
      96: aload_0
      97: aload_1
      98: iload         5
     100: iload         6
     102: invokespecial #43                 // Method exch:([III)V
     105: goto          9
     108: aload_0
     109: aload_1
     110: iload_3
     111: iload         6
     113: invokespecial #43                 // Method exch:([III)V
     116: iload         6
     118: ireturn

  private final void exch(int[], int, int);
    Code:
       0: aload_1
       1: iload_2
       2: iaload
       3: istore        4
       5: aload_1
       6: iload_2
       7: aload_1
       8: iload_3
       9: iaload
      10: iastore
      11: aload_1
      12: iload_3
      13: iload         4
      15: iastore
      16: return

  private final void shuffle(int[]);
    Code:
       0: new           #49                 // class java/util/Random
       3: dup
       4: invokespecial #50                 // Method java/util/Random."<init>":()V
       7: astore_2
       8: iconst_1
       9: istore_3
      10: aload_1
      11: arraylength
      12: istore        4
      14: iload_3
      15: iload         4
      17: if_icmpge     41
      20: aload_2
      21: iload_3
      22: invokevirtual #54                 // Method java/util/Random.nextInt:(I)I
      25: istore        5
      27: aload_0
      28: aload_1
      29: iload_3
      30: iload         5
      32: invokespecial #43                 // Method exch:([III)V
      35: iinc          3, 1
      38: goto          14
      41: return
}


package com.radix2.algorithms.extras

import java.util.*

fun minimumAverage(orders: Sequence<Order>): Long {
    val arrivalTimes = PriorityQueue<Order> { o1, o2 -> o1.arrivalTime.compareTo(o2.arrivalTime) }
    val burstTimes = PriorityQueue<Order> { o1, o2 -> o1.burstTime.compareTo(o2.burstTime) }

    for (order in orders) {
        arrivalTimes.add(order)
    }

    var currentTime = 0L
    var totalWaitTime = 0L
    var numberOfOrdersServed = 0L

    fun processOrder(order: Order) {
        totalWaitTime += (currentTime - order.arrivalTime + order.burstTime)
        currentTime += order.burstTime
        numberOfOrdersServed++
    }

    fun enqueueOrders() {
        while (!arrivalTimes.isEmpty() && arrivalTimes.peek().arrivalTime <= currentTime) {
            burstTimes.add(arrivalTimes.poll())
        }
    }

    while (!arrivalTimes.isEmpty()) {
        var order = arrivalTimes.poll()

        currentTime = if (burstTimes.isEmpty()) order.arrivalTime else currentTime + order.arrivalTime

        processOrder(order)
        enqueueOrders()

        while (!burstTimes.isEmpty()) {
            order = burstTimes.poll()
            processOrder(order)
            enqueueOrders()
        }
    }

    return totalWaitTime / numberOfOrdersServed
}

data class Order(val arrivalTime: Long, val burstTime: Long)

fun main(args: Array<String>) {
    val reader = System.`in`.bufferedReader()

    val n = reader.readLine().trim().toInt()

    val orders = Array<Order?>(n) { null }

    for (ordersRowItr in 0 until n) {
        val (arrivalTime, burstTime) = reader.readLine().split(" ").map { it.trim().toLong() }

        orders[ordersRowItr] = Order(arrivalTime, burstTime)
    }

    val result = minimumAverage(orders.asSequence().filterNotNull())

    println(result)
}

Compiled from "MinAvg.kt"
public final class com.radix2.algorithms.extras.MinAvgKt {
  public static final long minimumAverage(kotlin.sequences.Sequence<com.radix2.algorithms.extras.Order>);
    Code:
       0: aload_0
       1: ldc           #10                 // String orders
       3: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: new           #18                 // class java/util/PriorityQueue
       9: dup
      10: invokedynamic #38,  0             // InvokeDynamic #0:invoke:()Lkotlin/jvm/functions/Function2;
      15: invokedynamic #49,  0             // InvokeDynamic #1:compare:(Lkotlin/jvm/functions/Function2;)Ljava/util/Comparator;
      20: invokespecial #53                 // Method java/util/PriorityQueue."<init>":(Ljava/util/Comparator;)V
      23: astore_1
      24: new           #18                 // class java/util/PriorityQueue
      27: dup
      28: invokedynamic #58,  0             // InvokeDynamic #2:invoke:()Lkotlin/jvm/functions/Function2;
      33: invokedynamic #63,  0             // InvokeDynamic #3:compare:(Lkotlin/jvm/functions/Function2;)Ljava/util/Comparator;
      38: invokespecial #53                 // Method java/util/PriorityQueue."<init>":(Ljava/util/Comparator;)V
      41: astore_2
      42: aload_0
      43: invokeinterface #69,  1           // InterfaceMethod kotlin/sequences/Sequence.iterator:()Ljava/util/Iterator;
      48: astore_3
      49: aload_3
      50: invokeinterface #75,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      55: ifeq          79
      58: aload_3
      59: invokeinterface #79,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      64: checkcast     #81                 // class com/radix2/algorithms/extras/Order
      67: astore        4
      69: aload_1
      70: aload         4
      72: invokevirtual #85                 // Method java/util/PriorityQueue.add:(Ljava/lang/Object;)Z
      75: pop
      76: goto          49
      79: new           #87                 // class kotlin/jvm/internal/Ref$LongRef
      82: dup
      83: invokespecial #90                 // Method kotlin/jvm/internal/Ref$LongRef."<init>":()V
      86: astore_3
      87: new           #87                 // class kotlin/jvm/internal/Ref$LongRef
      90: dup
      91: invokespecial #90                 // Method kotlin/jvm/internal/Ref$LongRef."<init>":()V
      94: astore        4
      96: new           #87                 // class kotlin/jvm/internal/Ref$LongRef
      99: dup
     100: invokespecial #90                 // Method kotlin/jvm/internal/Ref$LongRef."<init>":()V
     103: astore        5
     105: aload_1
     106: invokevirtual #93                 // Method java/util/PriorityQueue.isEmpty:()Z
     109: ifne          219
     112: aload_1
     113: invokevirtual #96                 // Method java/util/PriorityQueue.poll:()Ljava/lang/Object;
     116: checkcast     #81                 // class com/radix2/algorithms/extras/Order
     119: astore        6
     121: aload_3
     122: aload_2
     123: invokevirtual #93                 // Method java/util/PriorityQueue.isEmpty:()Z
     126: ifeq          137
     129: aload         6
     131: invokevirtual #100                // Method com/radix2/algorithms/extras/Order.getArrivalTime:()J
     134: goto          147
     137: aload_3
     138: getfield      #104                // Field kotlin/jvm/internal/Ref$LongRef.element:J
     141: aload         6
     143: invokevirtual #100                // Method com/radix2/algorithms/extras/Order.getArrivalTime:()J
     146: ladd
     147: putfield      #104                // Field kotlin/jvm/internal/Ref$LongRef.element:J
     150: aload         4
     152: aload_3
     153: aload         5
     155: aload         6
     157: astore        7
     159: aload         7
     161: invokestatic  #108                // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
     164: aload         7
     166: invokestatic  #112                // Method minimumAverage$processOrder:(Lkotlin/jvm/internal/Ref$LongRef;Lkotlin/jvm/internal/Ref$LongRef;Lkotlin/jvm/internal/Ref$LongRef;Lcom/radix2/algorithms/extras/Order;)V
     169: aload_1
     170: aload_3
     171: aload_2
     172: invokestatic  #116                // Method minimumAverage$enqueueOrders:(Ljava/util/PriorityQueue;Lkotlin/jvm/internal/Ref$LongRef;Ljava/util/PriorityQueue;)V
     175: aload_2
     176: invokevirtual #93                 // Method java/util/PriorityQueue.isEmpty:()Z
     179: ifne          105
     182: aload_2
     183: invokevirtual #96                 // Method java/util/PriorityQueue.poll:()Ljava/lang/Object;
     186: checkcast     #81                 // class com/radix2/algorithms/extras/Order
     189: astore        6
     191: aload         4
     193: aload_3
     194: aload         5
     196: aload         6
     198: astore        7
     200: aload         7
     202: invokestatic  #108                // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
     205: aload         7
     207: invokestatic  #112                // Method minimumAverage$processOrder:(Lkotlin/jvm/internal/Ref$LongRef;Lkotlin/jvm/internal/Ref$LongRef;Lkotlin/jvm/internal/Ref$LongRef;Lcom/radix2/algorithms/extras/Order;)V
     210: aload_1
     211: aload_3
     212: aload_2
     213: invokestatic  #116                // Method minimumAverage$enqueueOrders:(Ljava/util/PriorityQueue;Lkotlin/jvm/internal/Ref$LongRef;Ljava/util/PriorityQueue;)V
     216: goto          175
     219: aload         4
     221: getfield      #104                // Field kotlin/jvm/internal/Ref$LongRef.element:J
     224: aload         5
     226: getfield      #104                // Field kotlin/jvm/internal/Ref$LongRef.element:J
     229: ldiv
     230: lreturn

  public static final void main(java.lang.String[]);
    Code:
       0: aload_0
       1: ldc           #130                // String args
       3: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: getstatic     #136                // Field java/lang/System.in:Ljava/io/InputStream;
       9: dup
      10: ldc           #137                // String in
      12: invokestatic  #140                // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
      15: astore_2
      16: getstatic     #146                // Field kotlin/text/Charsets.UTF_8:Ljava/nio/charset/Charset;
      19: astore_3
      20: new           #148                // class java/io/InputStreamReader
      23: dup
      24: aload_2
      25: aload_3
      26: invokespecial #151                // Method java/io/InputStreamReader."<init>":(Ljava/io/InputStream;Ljava/nio/charset/Charset;)V
      29: checkcast     #153                // class java/io/Reader
      32: astore        4
      34: sipush        8192
      37: istore        5
      39: aload         4
      41: instanceof    #155                // class java/io/BufferedReader
      44: ifeq          55
      47: aload         4
      49: checkcast     #155                // class java/io/BufferedReader
      52: goto          66
      55: new           #155                // class java/io/BufferedReader
      58: dup
      59: aload         4
      61: iload         5
      63: invokespecial #158                // Method java/io/BufferedReader."<init>":(Ljava/io/Reader;I)V
      66: astore_1
      67: aload_1
      68: invokevirtual #162                // Method java/io/BufferedReader.readLine:()Ljava/lang/String;
      71: dup
      72: ldc           #164                // String readLine(...)
      74: invokestatic  #140                // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
      77: checkcast     #166                // class java/lang/CharSequence
      80: invokestatic  #172                // Method kotlin/text/StringsKt.trim:(Ljava/lang/CharSequence;)Ljava/lang/CharSequence;
      83: invokevirtual #175                // Method java/lang/Object.toString:()Ljava/lang/String;
      86: invokestatic  #181                // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
      89: istore_2
      90: iconst_0
      91: istore        4
      93: iload_2
      94: anewarray     #81                 // class com/radix2/algorithms/extras/Order
      97: astore        5
      99: iload         4
     101: iload_2
     102: if_icmpge     121
     105: iload         4
     107: istore        6
     109: aload         5
     111: iload         6
     113: aconst_null
     114: aastore
     115: iinc          4, 1
     118: goto          99
     121: aload         5
     123: astore_3
     124: iconst_0
     125: istore        4
     127: iload         4
     129: iload_2
     130: if_icmpge     336
     133: aload_1
     134: invokevirtual #162                // Method java/io/BufferedReader.readLine:()Ljava/lang/String;
     137: dup
     138: ldc           #164                // String readLine(...)
     140: invokestatic  #140                // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
     143: checkcast     #166                // class java/lang/CharSequence
     146: iconst_1
     147: anewarray     #183                // class java/lang/String
     150: astore        6
     152: aload         6
     154: iconst_0
     155: ldc           #185                // String
     157: aastore
     158: aload         6
     160: iconst_0
     161: iconst_0
     162: bipush        6
     164: aconst_null
     165: invokestatic  #189                // Method kotlin/text/StringsKt.split$default:(Ljava/lang/CharSequence;[Ljava/lang/String;ZIILjava/lang/Object;)Ljava/util/List;
     168: checkcast     #191                // class java/lang/Iterable
     171: astore        6
     173: iconst_0
     174: istore        7
     176: aload         6
     178: astore        8
     180: new           #193                // class java/util/ArrayList
     183: dup
     184: aload         6
     186: bipush        10
     188: invokestatic  #199                // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
     191: invokespecial #202                // Method java/util/ArrayList."<init>":(I)V
     194: checkcast     #204                // class java/util/Collection
     197: astore        9
     199: iconst_0
     200: istore        10
     202: aload         8
     204: invokeinterface #205,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     209: astore        11
     211: aload         11
     213: invokeinterface #75,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     218: ifeq          275
     221: aload         11
     223: invokeinterface #79,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     228: astore        12
     230: aload         9
     232: aload         12
     234: checkcast     #183                // class java/lang/String
     237: astore        13
     239: astore        15
     241: iconst_0
     242: istore        14
     244: nop
     245: aload         13
     247: checkcast     #166                // class java/lang/CharSequence
     250: invokestatic  #172                // Method kotlin/text/StringsKt.trim:(Ljava/lang/CharSequence;)Ljava/lang/CharSequence;
     253: invokevirtual #175                // Method java/lang/Object.toString:()Ljava/lang/String;
     256: invokestatic  #211                // Method java/lang/Long.parseLong:(Ljava/lang/String;)J
     259: nop
     260: invokestatic  #215                // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
     263: aload         15
     265: swap
     266: invokeinterface #216,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     271: pop
     272: goto          211
     275: aload         9
     277: checkcast     #218                // class java/util/List
     280: nop
     281: astore        5
     283: aload         5
     285: iconst_0
     286: invokeinterface #222,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     291: checkcast     #224                // class java/lang/Number
     294: invokevirtual #227                // Method java/lang/Number.longValue:()J
     297: lstore        6
     299: aload         5
     301: iconst_1
     302: invokeinterface #222,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     307: checkcast     #224                // class java/lang/Number
     310: invokevirtual #227                // Method java/lang/Number.longValue:()J
     313: lstore        8
     315: aload_3
     316: iload         4
     318: new           #81                 // class com/radix2/algorithms/extras/Order
     321: dup
     322: lload         6
     324: lload         8
     326: invokespecial #230                // Method com/radix2/algorithms/extras/Order."<init>":(JJ)V
     329: aastore
     330: iinc          4, 1
     333: goto          127
     336: aload_3
     337: invokestatic  #236                // Method kotlin/collections/ArraysKt.asSequence:([Ljava/lang/Object;)Lkotlin/sequences/Sequence;
     340: invokestatic  #242                // Method kotlin/sequences/SequencesKt.filterNotNull:(Lkotlin/sequences/Sequence;)Lkotlin/sequences/Sequence;
     343: invokestatic  #244                // Method minimumAverage:(Lkotlin/sequences/Sequence;)J
     346: lstore        4
     348: getstatic     #248                // Field java/lang/System.out:Ljava/io/PrintStream;
     351: lload         4
     353: invokevirtual #254                // Method java/io/PrintStream.println:(J)V
     356: return

  private static final int minimumAverage$lambda$0(com.radix2.algorithms.extras.Order, com.radix2.algorithms.extras.Order);
    Code:
       0: aload_0
       1: invokevirtual #100                // Method com/radix2/algorithms/extras/Order.getArrivalTime:()J
       4: aload_1
       5: invokevirtual #100                // Method com/radix2/algorithms/extras/Order.getArrivalTime:()J
       8: invokestatic  #285                // Method kotlin/jvm/internal/Intrinsics.compare:(JJ)I
      11: ireturn

  private static final int minimumAverage$lambda$1(kotlin.jvm.functions.Function2, java.lang.Object, java.lang.Object);
    Code:
       0: aload_0
       1: aload_1
       2: aload_2
       3: invokeinterface #291,  3          // InterfaceMethod kotlin/jvm/functions/Function2.invoke:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
       8: checkcast     #224                // class java/lang/Number
      11: invokevirtual #295                // Method java/lang/Number.intValue:()I
      14: ireturn

  private static final int minimumAverage$lambda$2(com.radix2.algorithms.extras.Order, com.radix2.algorithms.extras.Order);
    Code:
       0: aload_0
       1: invokevirtual #302                // Method com/radix2/algorithms/extras/Order.getBurstTime:()J
       4: aload_1
       5: invokevirtual #302                // Method com/radix2/algorithms/extras/Order.getBurstTime:()J
       8: invokestatic  #285                // Method kotlin/jvm/internal/Intrinsics.compare:(JJ)I
      11: ireturn

  private static final int minimumAverage$lambda$3(kotlin.jvm.functions.Function2, java.lang.Object, java.lang.Object);
    Code:
       0: aload_0
       1: aload_1
       2: aload_2
       3: invokeinterface #291,  3          // InterfaceMethod kotlin/jvm/functions/Function2.invoke:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
       8: checkcast     #224                // class java/lang/Number
      11: invokevirtual #295                // Method java/lang/Number.intValue:()I
      14: ireturn

  private static final void minimumAverage$processOrder(kotlin.jvm.internal.Ref$LongRef, kotlin.jvm.internal.Ref$LongRef, kotlin.jvm.internal.Ref$LongRef, com.radix2.algorithms.extras.Order);
    Code:
       0: aload_0
       1: aload_0
       2: getfield      #104                // Field kotlin/jvm/internal/Ref$LongRef.element:J
       5: aload_1
       6: getfield      #104                // Field kotlin/jvm/internal/Ref$LongRef.element:J
       9: aload_3
      10: invokevirtual #100                // Method com/radix2/algorithms/extras/Order.getArrivalTime:()J
      13: lsub
      14: aload_3
      15: invokevirtual #302                // Method com/radix2/algorithms/extras/Order.getBurstTime:()J
      18: ladd
      19: ladd
      20: putfield      #104                // Field kotlin/jvm/internal/Ref$LongRef.element:J
      23: aload_1
      24: aload_1
      25: getfield      #104                // Field kotlin/jvm/internal/Ref$LongRef.element:J
      28: aload_3
      29: invokevirtual #302                // Method com/radix2/algorithms/extras/Order.getBurstTime:()J
      32: ladd
      33: putfield      #104                // Field kotlin/jvm/internal/Ref$LongRef.element:J
      36: aload_2
      37: getfield      #104                // Field kotlin/jvm/internal/Ref$LongRef.element:J
      40: lstore        4
      42: aload_2
      43: lload         4
      45: lconst_1
      46: ladd
      47: putfield      #104                // Field kotlin/jvm/internal/Ref$LongRef.element:J
      50: return

  private static final void minimumAverage$enqueueOrders(java.util.PriorityQueue<com.radix2.algorithms.extras.Order>, kotlin.jvm.internal.Ref$LongRef, java.util.PriorityQueue<com.radix2.algorithms.extras.Order>);
    Code:
       0: aload_0
       1: invokevirtual #93                 // Method java/util/PriorityQueue.isEmpty:()Z
       4: ifne          37
       7: aload_0
       8: invokevirtual #306                // Method java/util/PriorityQueue.peek:()Ljava/lang/Object;
      11: checkcast     #81                 // class com/radix2/algorithms/extras/Order
      14: invokevirtual #100                // Method com/radix2/algorithms/extras/Order.getArrivalTime:()J
      17: aload_1
      18: getfield      #104                // Field kotlin/jvm/internal/Ref$LongRef.element:J
      21: lcmp
      22: ifgt          37
      25: aload_2
      26: aload_0
      27: invokevirtual #96                 // Method java/util/PriorityQueue.poll:()Ljava/lang/Object;
      30: invokevirtual #85                 // Method java/util/PriorityQueue.add:(Ljava/lang/Object;)Z
      33: pop
      34: goto          0
      37: return
}


package com.radix2.algorithms.extras

import java.util.*

fun print(array: IntArray, lo: Int, hi: Int) {
    if (lo == hi) {
        println(array[lo])
    } else {
        println(array[lo])
        print(array, lo + 1, hi)
    }
}

fun map(array: IntArray, lo: Int, hi: Int, transform: (Int) -> Int) {
    array[lo] = transform(lo)

    if (lo < hi)
        map(array, lo + 1, hi, transform)
}

fun fib(n: Int): Int {
    return if (n < 2)
        n
    else
        fib(n - 1) + fib(n - 2)
}

fun towerOfHanoi(num: Int, from: Char, to: Char, aux: Char) {
    if (num == 1) {
        println("$from -> $to")
    } else {

        towerOfHanoi(num - 1, from, aux, to)

        println("$from -> $to")

        towerOfHanoi(num - 1, aux, to, from)
    }
}

fun isPallindrom(str: String, lo: Int, hi: Int): Boolean {
    if (hi <= lo)
        return str[lo] == str[hi]

    return str[lo] == str[hi] && isPallindrom(str, lo + 1, hi - 1)
}

fun power(a: Int, b: Int): Int {
    if (b == 1)
        return a

    return a * power(a, b - 1)
}

fun reversePrint(str: String) {
    if (str.length == 1)
        print(str)
    else {
        print("${str.last()}")
        reversePrint(str.substring(0, str.lastIndex))
    }
}

fun shiftBlankToRight(array: IntArray) {
    val list = array.sortedWith(kotlin.Comparator { o1, o2 ->
        when {
            o1 == ' '.toInt() -> 1
            o2 == ' '.toInt() -> -1
            else -> 0
        }
    })

    println(list.joinToString())
}

fun main(args: Array<String>) {
    val array = intArrayOf('A'.toInt(), 'B'.toInt(), ' '.toInt(), 'C'.toInt(), ' '.toInt(), ' '.toInt(), 'D'.toInt())

    shiftBlankToRight(array)

    println()
}

Compiled from "Scribble.kt"
public final class com.radix2.algorithms.extras.ScribbleKt {
  public static final void print(int[], int, int);
    Code:
       0: aload_0
       1: ldc           #9                  // String array
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: iload_1
       7: iload_2
       8: if_icmpne     25
      11: aload_0
      12: iload_1
      13: iaload
      14: istore_3
      15: getstatic     #21                 // Field java/lang/System.out:Ljava/io/PrintStream;
      18: iload_3
      19: invokevirtual #27                 // Method java/io/PrintStream.println:(I)V
      22: goto          44
      25: aload_0
      26: iload_1
      27: iaload
      28: istore_3
      29: getstatic     #21                 // Field java/lang/System.out:Ljava/io/PrintStream;
      32: iload_3
      33: invokevirtual #27                 // Method java/io/PrintStream.println:(I)V
      36: aload_0
      37: iload_1
      38: iconst_1
      39: iadd
      40: iload_2
      41: invokestatic  #29                 // Method print:([III)V
      44: return

  public static final void map(int[], int, int, kotlin.jvm.functions.Function1<? super java.lang.Integer, java.lang.Integer>);
    Code:
       0: aload_0
       1: ldc           #9                  // String array
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_3
       7: ldc           #38                 // String transform
       9: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      12: aload_0
      13: iload_1
      14: aload_3
      15: iload_1
      16: invokestatic  #44                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      19: invokeinterface #50,  2           // InterfaceMethod kotlin/jvm/functions/Function1.invoke:(Ljava/lang/Object;)Ljava/lang/Object;
      24: checkcast     #52                 // class java/lang/Number
      27: invokevirtual #56                 // Method java/lang/Number.intValue:()I
      30: iastore
      31: iload_1
      32: iload_2
      33: if_icmpge     45
      36: aload_0
      37: iload_1
      38: iconst_1
      39: iadd
      40: iload_2
      41: aload_3
      42: invokestatic  #58                 // Method map:([IIILkotlin/jvm/functions/Function1;)V
      45: return

  public static final int fib(int);
    Code:
       0: iload_0
       1: iconst_2
       2: if_icmpge     9
       5: iload_0
       6: goto          22
       9: iload_0
      10: iconst_1
      11: isub
      12: invokestatic  #63                 // Method fib:(I)I
      15: iload_0
      16: iconst_2
      17: isub
      18: invokestatic  #63                 // Method fib:(I)I
      21: iadd
      22: ireturn

  public static final void towerOfHanoi(int, char, char, char);
    Code:
       0: iload_0
       1: iconst_1
       2: if_icmpne     38
       5: new           #68                 // class java/lang/StringBuilder
       8: dup
       9: invokespecial #72                 // Method java/lang/StringBuilder."<init>":()V
      12: iload_1
      13: invokevirtual #76                 // Method java/lang/StringBuilder.append:(C)Ljava/lang/StringBuilder;
      16: ldc           #78                 // String  ->
      18: invokevirtual #81                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      21: iload_2
      22: invokevirtual #76                 // Method java/lang/StringBuilder.append:(C)Ljava/lang/StringBuilder;
      25: invokevirtual #85                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      28: getstatic     #21                 // Field java/lang/System.out:Ljava/io/PrintStream;
      31: swap
      32: invokevirtual #88                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      35: goto          86
      38: iload_0
      39: iconst_1
      40: isub
      41: iload_1
      42: iload_3
      43: iload_2
      44: invokestatic  #90                 // Method towerOfHanoi:(ICCC)V
      47: new           #68                 // class java/lang/StringBuilder
      50: dup
      51: invokespecial #72                 // Method java/lang/StringBuilder."<init>":()V
      54: iload_1
      55: invokevirtual #76                 // Method java/lang/StringBuilder.append:(C)Ljava/lang/StringBuilder;
      58: ldc           #78                 // String  ->
      60: invokevirtual #81                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      63: iload_2
      64: invokevirtual #76                 // Method java/lang/StringBuilder.append:(C)Ljava/lang/StringBuilder;
      67: invokevirtual #85                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      70: getstatic     #21                 // Field java/lang/System.out:Ljava/io/PrintStream;
      73: swap
      74: invokevirtual #88                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      77: iload_0
      78: iconst_1
      79: isub
      80: iload_3
      81: iload_2
      82: iload_1
      83: invokestatic  #90                 // Method towerOfHanoi:(ICCC)V
      86: return

  public static final boolean isPallindrom(java.lang.String, int, int);
    Code:
       0: aload_0
       1: ldc           #99                 // String str
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: iload_2
       7: iload_1
       8: if_icmpgt     30
      11: aload_0
      12: iload_1
      13: invokevirtual #105                // Method java/lang/String.charAt:(I)C
      16: aload_0
      17: iload_2
      18: invokevirtual #105                // Method java/lang/String.charAt:(I)C
      21: if_icmpne     28
      24: iconst_1
      25: goto          29
      28: iconst_0
      29: ireturn
      30: aload_0
      31: iload_1
      32: invokevirtual #105                // Method java/lang/String.charAt:(I)C
      35: aload_0
      36: iload_2
      37: invokevirtual #105                // Method java/lang/String.charAt:(I)C
      40: if_icmpne     60
      43: aload_0
      44: iload_1
      45: iconst_1
      46: iadd
      47: iload_2
      48: iconst_1
      49: isub
      50: invokestatic  #107                // Method isPallindrom:(Ljava/lang/String;II)Z
      53: ifeq          60
      56: iconst_1
      57: goto          61
      60: iconst_0
      61: ireturn

  public static final int power(int, int);
    Code:
       0: iload_1
       1: iconst_1
       2: if_icmpne     7
       5: iload_0
       6: ireturn
       7: iload_0
       8: iload_0
       9: iload_1
      10: iconst_1
      11: isub
      12: invokestatic  #112                // Method power:(II)I
      15: imul
      16: ireturn

  public static final void reversePrint(java.lang.String);
    Code:
       0: aload_0
       1: ldc           #99                 // String str
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: invokevirtual #119                // Method java/lang/String.length:()I
      10: iconst_1
      11: if_icmpne     24
      14: getstatic     #21                 // Field java/lang/System.out:Ljava/io/PrintStream;
      17: aload_0
      18: invokevirtual #121                // Method java/io/PrintStream.print:(Ljava/lang/Object;)V
      21: goto          62
      24: aload_0
      25: checkcast     #123                // class java/lang/CharSequence
      28: invokestatic  #129                // Method kotlin/text/StringsKt.last:(Ljava/lang/CharSequence;)C
      31: invokestatic  #132                // Method java/lang/String.valueOf:(C)Ljava/lang/String;
      34: getstatic     #21                 // Field java/lang/System.out:Ljava/io/PrintStream;
      37: swap
      38: invokevirtual #121                // Method java/io/PrintStream.print:(Ljava/lang/Object;)V
      41: aload_0
      42: iconst_0
      43: aload_0
      44: checkcast     #123                // class java/lang/CharSequence
      47: invokestatic  #136                // Method kotlin/text/StringsKt.getLastIndex:(Ljava/lang/CharSequence;)I
      50: invokevirtual #140                // Method java/lang/String.substring:(II)Ljava/lang/String;
      53: dup
      54: ldc           #142                // String substring(...)
      56: invokestatic  #145                // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
      59: invokestatic  #147                // Method reversePrint:(Ljava/lang/String;)V
      62: return

  public static final void shiftBlankToRight(int[]);
    Code:
       0: aload_0
       1: ldc           #9                  // String array
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: invokedynamic #168,  0            // InvokeDynamic #0:compare:()Ljava/util/Comparator;
      12: invokestatic  #174                // Method kotlin/collections/ArraysKt.sortedWith:([ILjava/util/Comparator;)Ljava/util/List;
      15: astore_1
      16: aload_1
      17: checkcast     #176                // class java/lang/Iterable
      20: aconst_null
      21: aconst_null
      22: aconst_null
      23: iconst_0
      24: aconst_null
      25: aconst_null
      26: bipush        63
      28: aconst_null
      29: invokestatic  #182                // Method kotlin/collections/CollectionsKt.joinToString$default:(Ljava/lang/Iterable;Ljava/lang/CharSequence;Ljava/lang/CharSequence;Ljava/lang/CharSequence;ILjava/lang/CharSequence;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Ljava/lang/String;
      32: getstatic     #21                 // Field java/lang/System.out:Ljava/io/PrintStream;
      35: swap
      36: invokevirtual #88                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      39: return

  public static final void main(java.lang.String[]);
    Code:
       0: aload_0
       1: ldc           #188                // String args
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: bipush        7
       8: newarray       int
      10: astore_2
      11: aload_2
      12: iconst_0
      13: bipush        65
      15: iastore
      16: aload_2
      17: iconst_1
      18: bipush        66
      20: iastore
      21: aload_2
      22: iconst_2
      23: bipush        32
      25: iastore
      26: aload_2
      27: iconst_3
      28: bipush        67
      30: iastore
      31: aload_2
      32: iconst_4
      33: bipush        32
      35: iastore
      36: aload_2
      37: iconst_5
      38: bipush        32
      40: iastore
      41: aload_2
      42: bipush        6
      44: bipush        68
      46: iastore
      47: aload_2
      48: astore_1
      49: aload_1
      50: invokestatic  #190                // Method shiftBlankToRight:([I)V
      53: getstatic     #21                 // Field java/lang/System.out:Ljava/io/PrintStream;
      56: invokevirtual #192                // Method java/io/PrintStream.println:()V
      59: return

  private static final int shiftBlankToRight$lambda$0(java.lang.Integer, java.lang.Integer);
    Code:
       0: nop
       1: aload_0
       2: bipush        32
       4: istore_2
       5: dup
       6: ifnonnull     13
       9: pop
      10: goto          24
      13: invokevirtual #194                // Method java/lang/Integer.intValue:()I
      16: iload_2
      17: if_icmpne     24
      20: iconst_1
      21: goto          48
      24: aload_1
      25: bipush        32
      27: istore_2
      28: dup
      29: ifnonnull     36
      32: pop
      33: goto          47
      36: invokevirtual #194                // Method java/lang/Integer.intValue:()I
      39: iload_2
      40: if_icmpne     47
      43: iconst_m1
      44: goto          48
      47: iconst_0
      48: ireturn
}


package com.radix2.algorithms.extras

import java.util.PriorityQueue

fun runningMedian(a: Array<Int>): Array<Double> {
    val minPQ = PriorityQueue<Int> { o1, o2 -> o2.compareTo(o1) }
    val maxPQ = PriorityQueue<Int>()

    val medians = Array(a.size) { 0.0 }

    for (i in 0..a.lastIndex) {
        add(a[i], minPQ, maxPQ)
        rebalance(minPQ, maxPQ)
        medians[i] = getMedian(minPQ, maxPQ)
    }

    return medians
}

fun getMedian(minPQ: PriorityQueue<Int>, maxPQ: PriorityQueue<Int>): Double {
    val maxHeap = if (minPQ.size > maxPQ.size) minPQ else maxPQ
    val minHeap = if (minPQ.size > maxPQ.size) maxPQ else minPQ

    return if (maxHeap.size == minHeap.size) {
        (maxHeap.peek() + minHeap.peek()).toDouble() / 2
    } else {
        maxHeap.peek().toDouble()
    }
}

fun rebalance(minPQ: PriorityQueue<Int>, maxPQ: PriorityQueue<Int>) {
    val biggerHeap = if (minPQ.size > maxPQ.size) minPQ else maxPQ
    val smallerHeap = if (minPQ.size > maxPQ.size) maxPQ else minPQ

    if (biggerHeap.size - smallerHeap.size >= 2) {
        smallerHeap.add(biggerHeap.poll())
    }
}

fun add(num: Int, minPQ: PriorityQueue<Int>, maxPQ: PriorityQueue<Int>) {

    if (minPQ.isEmpty() || num < minPQ.peek()) {
        minPQ.add(num)
    } else {
        maxPQ.add(num)
    }
}

fun main(args: Array<String>) {
    val reader = System.`in`.bufferedReader()

    val aCount = reader.readLine().trim().toInt()

    val a = Array(aCount) { 0 }

    for (aItr in 0 until aCount) {
        val aItem = reader.readLine().trim().toInt()
        a[aItr] = aItem
    }

    reader.close()

    val result = runningMedian(a)

    println(result.joinToString("\n"))
}

Compiled from "RunningMedian.kt"
public final class com.radix2.algorithms.extras.RunningMedianKt {
  public static final java.lang.Double[] runningMedian(java.lang.Integer[]);
    Code:
       0: aload_0
       1: ldc           #9                  // String a
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: new           #17                 // class java/util/PriorityQueue
       9: dup
      10: invokedynamic #37,  0             // InvokeDynamic #0:invoke:()Lkotlin/jvm/functions/Function2;
      15: invokedynamic #48,  0             // InvokeDynamic #1:compare:(Lkotlin/jvm/functions/Function2;)Ljava/util/Comparator;
      20: invokespecial #52                 // Method java/util/PriorityQueue."<init>":(Ljava/util/Comparator;)V
      23: astore_1
      24: new           #17                 // class java/util/PriorityQueue
      27: dup
      28: invokespecial #55                 // Method java/util/PriorityQueue."<init>":()V
      31: astore_2
      32: iconst_0
      33: istore        4
      35: aload_0
      36: arraylength
      37: istore        5
      39: iload         5
      41: anewarray     #57                 // class java/lang/Double
      44: astore        6
      46: iload         4
      48: iload         5
      50: if_icmpge     72
      53: iload         4
      55: istore        7
      57: aload         6
      59: iload         7
      61: dconst_0
      62: invokestatic  #61                 // Method java/lang/Double.valueOf:(D)Ljava/lang/Double;
      65: aastore
      66: iinc          4, 1
      69: goto          46
      72: aload         6
      74: astore_3
      75: iconst_0
      76: istore        4
      78: aload_0
      79: invokestatic  #67                 // Method kotlin/collections/ArraysKt.getLastIndex:([Ljava/lang/Object;)I
      82: istore        5
      84: iload         4
      86: iload         5
      88: if_icmpgt     133
      91: aload_0
      92: iload         4
      94: aaload
      95: invokevirtual #73                 // Method java/lang/Integer.intValue:()I
      98: aload_1
      99: aload_2
     100: invokestatic  #77                 // Method add:(ILjava/util/PriorityQueue;Ljava/util/PriorityQueue;)V
     103: aload_1
     104: aload_2
     105: invokestatic  #81                 // Method rebalance:(Ljava/util/PriorityQueue;Ljava/util/PriorityQueue;)V
     108: aload_3
     109: iload         4
     111: aload_1
     112: aload_2
     113: invokestatic  #85                 // Method getMedian:(Ljava/util/PriorityQueue;Ljava/util/PriorityQueue;)D
     116: invokestatic  #61                 // Method java/lang/Double.valueOf:(D)Ljava/lang/Double;
     119: aastore
     120: iload         4
     122: iload         5
     124: if_icmpeq     133
     127: iinc          4, 1
     130: goto          91
     133: aload_3
     134: areturn

  public static final double getMedian(java.util.PriorityQueue<java.lang.Integer>, java.util.PriorityQueue<java.lang.Integer>);
    Code:
       0: aload_0
       1: ldc           #97                 // String minPQ
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_1
       7: ldc           #98                 // String maxPQ
       9: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      12: aload_0
      13: invokevirtual #101                // Method java/util/PriorityQueue.size:()I
      16: aload_1
      17: invokevirtual #101                // Method java/util/PriorityQueue.size:()I
      20: if_icmple     27
      23: aload_0
      24: goto          28
      27: aload_1
      28: astore_2
      29: aload_0
      30: invokevirtual #101                // Method java/util/PriorityQueue.size:()I
      33: aload_1
      34: invokevirtual #101                // Method java/util/PriorityQueue.size:()I
      37: if_icmple     44
      40: aload_1
      41: goto          45
      44: aload_0
      45: astore_3
      46: aload_2
      47: invokevirtual #101                // Method java/util/PriorityQueue.size:()I
      50: aload_3
      51: invokevirtual #101                // Method java/util/PriorityQueue.size:()I
      54: if_icmpne     91
      57: aload_2
      58: invokevirtual #105                // Method java/util/PriorityQueue.peek:()Ljava/lang/Object;
      61: checkcast     #107                // class java/lang/Number
      64: invokevirtual #108                // Method java/lang/Number.intValue:()I
      67: aload_3
      68: invokevirtual #105                // Method java/util/PriorityQueue.peek:()Ljava/lang/Object;
      71: dup
      72: ldc           #110                // String peek(...)
      74: invokestatic  #113                // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
      77: checkcast     #107                // class java/lang/Number
      80: invokevirtual #108                // Method java/lang/Number.intValue:()I
      83: iadd
      84: i2d
      85: iconst_2
      86: i2d
      87: ddiv
      88: goto          102
      91: aload_2
      92: invokevirtual #105                // Method java/util/PriorityQueue.peek:()Ljava/lang/Object;
      95: checkcast     #107                // class java/lang/Number
      98: invokevirtual #108                // Method java/lang/Number.intValue:()I
     101: i2d
     102: dreturn

  public static final void rebalance(java.util.PriorityQueue<java.lang.Integer>, java.util.PriorityQueue<java.lang.Integer>);
    Code:
       0: aload_0
       1: ldc           #97                 // String minPQ
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_1
       7: ldc           #98                 // String maxPQ
       9: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      12: aload_0
      13: invokevirtual #101                // Method java/util/PriorityQueue.size:()I
      16: aload_1
      17: invokevirtual #101                // Method java/util/PriorityQueue.size:()I
      20: if_icmple     27
      23: aload_0
      24: goto          28
      27: aload_1
      28: astore_2
      29: aload_0
      30: invokevirtual #101                // Method java/util/PriorityQueue.size:()I
      33: aload_1
      34: invokevirtual #101                // Method java/util/PriorityQueue.size:()I
      37: if_icmple     44
      40: aload_1
      41: goto          45
      44: aload_0
      45: astore_3
      46: aload_2
      47: invokevirtual #101                // Method java/util/PriorityQueue.size:()I
      50: aload_3
      51: invokevirtual #101                // Method java/util/PriorityQueue.size:()I
      54: isub
      55: iconst_2
      56: if_icmplt     68
      59: aload_3
      60: aload_2
      61: invokevirtual #119                // Method java/util/PriorityQueue.poll:()Ljava/lang/Object;
      64: invokevirtual #122                // Method java/util/PriorityQueue.add:(Ljava/lang/Object;)Z
      67: pop
      68: return

  public static final void add(int, java.util.PriorityQueue<java.lang.Integer>, java.util.PriorityQueue<java.lang.Integer>);
    Code:
       0: aload_1
       1: ldc           #97                 // String minPQ
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_2
       7: ldc           #98                 // String maxPQ
       9: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      12: aload_1
      13: invokevirtual #129                // Method java/util/PriorityQueue.isEmpty:()Z
      16: ifne          33
      19: iload_0
      20: aload_1
      21: invokevirtual #105                // Method java/util/PriorityQueue.peek:()Ljava/lang/Object;
      24: checkcast     #107                // class java/lang/Number
      27: invokevirtual #108                // Method java/lang/Number.intValue:()I
      30: if_icmpge     44
      33: aload_1
      34: iload_0
      35: invokestatic  #132                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      38: invokevirtual #122                // Method java/util/PriorityQueue.add:(Ljava/lang/Object;)Z
      41: goto          52
      44: aload_2
      45: iload_0
      46: invokestatic  #132                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      49: invokevirtual #122                // Method java/util/PriorityQueue.add:(Ljava/lang/Object;)Z
      52: pop
      53: return

  public static final void main(java.lang.String[]);
    Code:
       0: aload_0
       1: ldc           #137                // String args
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: getstatic     #143                // Field java/lang/System.in:Ljava/io/InputStream;
       9: dup
      10: ldc           #144                // String in
      12: invokestatic  #113                // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
      15: astore_2
      16: getstatic     #150                // Field kotlin/text/Charsets.UTF_8:Ljava/nio/charset/Charset;
      19: astore_3
      20: new           #152                // class java/io/InputStreamReader
      23: dup
      24: aload_2
      25: aload_3
      26: invokespecial #155                // Method java/io/InputStreamReader."<init>":(Ljava/io/InputStream;Ljava/nio/charset/Charset;)V
      29: checkcast     #157                // class java/io/Reader
      32: astore        4
      34: sipush        8192
      37: istore        5
      39: aload         4
      41: instanceof    #159                // class java/io/BufferedReader
      44: ifeq          55
      47: aload         4
      49: checkcast     #159                // class java/io/BufferedReader
      52: goto          66
      55: new           #159                // class java/io/BufferedReader
      58: dup
      59: aload         4
      61: iload         5
      63: invokespecial #162                // Method java/io/BufferedReader."<init>":(Ljava/io/Reader;I)V
      66: astore_1
      67: aload_1
      68: invokevirtual #166                // Method java/io/BufferedReader.readLine:()Ljava/lang/String;
      71: dup
      72: ldc           #168                // String readLine(...)
      74: invokestatic  #113                // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
      77: checkcast     #170                // class java/lang/CharSequence
      80: invokestatic  #176                // Method kotlin/text/StringsKt.trim:(Ljava/lang/CharSequence;)Ljava/lang/CharSequence;
      83: invokevirtual #179                // Method java/lang/Object.toString:()Ljava/lang/String;
      86: invokestatic  #183                // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
      89: istore_2
      90: iconst_0
      91: istore        4
      93: iload_2
      94: anewarray     #69                 // class java/lang/Integer
      97: astore        5
      99: iload         4
     101: iload_2
     102: if_icmpge     124
     105: iload         4
     107: istore        6
     109: aload         5
     111: iload         6
     113: iconst_0
     114: invokestatic  #132                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     117: aastore
     118: iinc          4, 1
     121: goto          99
     124: aload         5
     126: astore_3
     127: iconst_0
     128: istore        4
     130: iload         4
     132: iload_2
     133: if_icmpge     175
     136: aload_1
     137: invokevirtual #166                // Method java/io/BufferedReader.readLine:()Ljava/lang/String;
     140: dup
     141: ldc           #168                // String readLine(...)
     143: invokestatic  #113                // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
     146: checkcast     #170                // class java/lang/CharSequence
     149: invokestatic  #176                // Method kotlin/text/StringsKt.trim:(Ljava/lang/CharSequence;)Ljava/lang/CharSequence;
     152: invokevirtual #179                // Method java/lang/Object.toString:()Ljava/lang/String;
     155: invokestatic  #183                // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
     158: istore        5
     160: aload_3
     161: iload         4
     163: iload         5
     165: invokestatic  #132                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     168: aastore
     169: iinc          4, 1
     172: goto          130
     175: aload_1
     176: invokevirtual #186                // Method java/io/BufferedReader.close:()V
     179: aload_3
     180: invokestatic  #188                // Method runningMedian:([Ljava/lang/Integer;)[Ljava/lang/Double;
     183: astore        4
     185: aload         4
     187: ldc           #190                // String \n
     189: checkcast     #170                // class java/lang/CharSequence
     192: aconst_null
     193: aconst_null
     194: iconst_0
     195: aconst_null
     196: aconst_null
     197: bipush        62
     199: aconst_null
     200: invokestatic  #194                // Method kotlin/collections/ArraysKt.joinToString$default:([Ljava/lang/Object;Ljava/lang/CharSequence;Ljava/lang/CharSequence;Ljava/lang/CharSequence;ILjava/lang/CharSequence;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Ljava/lang/String;
     203: getstatic     #198                // Field java/lang/System.out:Ljava/io/PrintStream;
     206: swap
     207: invokevirtual #204                // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     210: return

  private static final int runningMedian$lambda$0(java.lang.Integer, java.lang.Integer);
    Code:
       0: aload_1
       1: invokevirtual #73                 // Method java/lang/Integer.intValue:()I
       4: aload_0
       5: invokestatic  #219                // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
       8: aload_0
       9: invokevirtual #73                 // Method java/lang/Integer.intValue:()I
      12: invokestatic  #222                // Method kotlin/jvm/internal/Intrinsics.compare:(II)I
      15: ireturn

  private static final int runningMedian$lambda$1(kotlin.jvm.functions.Function2, java.lang.Object, java.lang.Object);
    Code:
       0: aload_0
       1: aload_1
       2: aload_2
       3: invokeinterface #229,  3          // InterfaceMethod kotlin/jvm/functions/Function2.invoke:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
       8: checkcast     #107                // class java/lang/Number
      11: invokevirtual #108                // Method java/lang/Number.intValue:()I
      14: ireturn
}