package calculator

val numbers = mutableListOf<Int>()

fun Char.isOperator(): Boolean {
    return this == '+' || this == '-'
}

fun start() {
    val input = readln().split(' ').joinToString("")
    when (input) {
        "" -> start()
        "/exit" -> throw Exception()
        "/help" -> println(
            """The program does the following operations:
            sum(+) -> 1 + 3 = 4
            subtract(-) -> 3 - 2 = 1
            mix operations -> -3 -- 1 +++ 3 -15 = -14
        """.trimIndent()
        )

        else -> fixLine(input)
    }
    start()
}

fun computePlusMinus(operator1: Char, operator2: Char): Char {
    if (operator1 == operator2) {
        return '+'
    } else {
        return '-'
    }
}

fun fixLine(line: String) {
    var awryLine = line
    do {
        var operator: Char? = null
        do {
            var first = awryLine[0]
            val second: Char
            if (1 <= awryLine.lastIndex && awryLine[1].isOperator() && first.isOperator()) {
                second = awryLine[1]
                awryLine = awryLine.drop(2)
                awryLine = computePlusMinus(first, second) + awryLine
                first = awryLine[0]
            } else if (first.isOperator()) {
                operator = first
                awryLine = awryLine.removePrefix(first.toString())
                first = awryLine[0]
            }

        } while (first.digitToIntOrNull() == null)

        val numberPattern = Regex("((-?)|(\\+?))?\\d+")
        val positionNumber = numberPattern.find(awryLine)
        if (positionNumber != null) {
            val number = awryLine.substring(positionNumber.range)
            val numberAndOperator = if (operator == '-') '-' + number else number
            numbers.add(numberAndOperator.toInt())
            awryLine = awryLine.removePrefix(number)
        }
    } while (awryLine.length != 0)
    compute()
}

fun compute() {
    println(numbers.sum())
    numbers.clear()
}

fun getNumbers(input: String): MutableList<Int> {
    val numbers = mutableListOf<Int>()
    input.split(' ').forEach { numbers.add(it.toInt()) }
    return numbers
}

fun main() {
    try {
        start()
    } catch (e: Exception) {
        println("Bye!")
    }
}

Compiled from "Main.kt"
public final class calculator.MainKt {
  private static final java.util.List<java.lang.Integer> numbers;

  public static final java.util.List<java.lang.Integer> getNumbers();
    Code:
       0: getstatic     #12                 // Field numbers:Ljava/util/List;
       3: areturn

  public static final boolean isOperator(char);
    Code:
       0: iload_0
       1: bipush        43
       3: if_icmpeq     12
       6: iload_0
       7: bipush        45
       9: if_icmpne     16
      12: iconst_1
      13: goto          17
      16: iconst_0
      17: ireturn

  public static final void start();
    Code:
       0: invokestatic  #24                 // Method kotlin/io/ConsoleKt.readln:()Ljava/lang/String;
       3: checkcast     #26                 // class java/lang/CharSequence
       6: iconst_1
       7: newarray       char
       9: astore_1
      10: aload_1
      11: iconst_0
      12: bipush        32
      14: castore
      15: aload_1
      16: iconst_0
      17: iconst_0
      18: bipush        6
      20: aconst_null
      21: invokestatic  #32                 // Method kotlin/text/StringsKt.split$default:(Ljava/lang/CharSequence;[CZIILjava/lang/Object;)Ljava/util/List;
      24: checkcast     #34                 // class java/lang/Iterable
      27: ldc           #36                 // String
      29: checkcast     #26                 // class java/lang/CharSequence
      32: aconst_null
      33: aconst_null
      34: iconst_0
      35: aconst_null
      36: aconst_null
      37: bipush        62
      39: aconst_null
      40: invokestatic  #42                 // 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;
      43: astore_0
      44: aload_0
      45: astore_1
      46: aload_1
      47: invokevirtual #48                 // Method java/lang/String.hashCode:()I
      50: lookupswitch  { // 3
                     0: 84
              46533069: 108
              46604272: 96
               default: 147
          }
      84: aload_1
      85: ldc           #36                 // String
      87: invokevirtual #52                 // Method java/lang/String.equals:(Ljava/lang/Object;)Z
      90: ifne          120
      93: goto          147
      96: aload_1
      97: ldc           #54                 // String /help
      99: invokevirtual #52                 // Method java/lang/String.equals:(Ljava/lang/Object;)Z
     102: ifne          134
     105: goto          147
     108: aload_1
     109: ldc           #56                 // String /exit
     111: invokevirtual #52                 // Method java/lang/String.equals:(Ljava/lang/Object;)Z
     114: ifne          126
     117: goto          147
     120: invokestatic  #58                 // Method start:()V
     123: goto          151
     126: new           #60                 // class java/lang/Exception
     129: dup
     130: invokespecial #63                 // Method java/lang/Exception."<init>":()V
     133: athrow
     134: ldc           #65                 // String The program does the following operations:\n            sum(+) -> 1 + 3 = 4\n            subtract(-) -> 3 - 2 = 1\n            mix operations -> -3 -- 1 +++ 3 -15 = -14
     136: astore_2
     137: getstatic     #71                 // Field java/lang/System.out:Ljava/io/PrintStream;
     140: aload_2
     141: invokevirtual #77                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     144: goto          151
     147: aload_0
     148: invokestatic  #81                 // Method fixLine:(Ljava/lang/String;)V
     151: invokestatic  #58                 // Method start:()V
     154: return

  public static final char computePlusMinus(char, char);
    Code:
       0: iload_0
       1: iload_1
       2: if_icmpne     8
       5: bipush        43
       7: ireturn
       8: bipush        45
      10: ireturn

  public static final void fixLine(java.lang.String);
    Code:
       0: aload_0
       1: ldc           #89                 // String line
       3: invokestatic  #95                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: astore_1
       8: aconst_null
       9: astore_2
      10: aload_1
      11: iconst_0
      12: invokevirtual #99                 // Method java/lang/String.charAt:(I)C
      15: istore_3
      16: iconst_0
      17: istore        4
      19: iconst_1
      20: aload_1
      21: checkcast     #26                 // class java/lang/CharSequence
      24: invokestatic  #103                // Method kotlin/text/StringsKt.getLastIndex:(Ljava/lang/CharSequence;)I
      27: if_icmpgt     98
      30: aload_1
      31: iconst_1
      32: invokevirtual #99                 // Method java/lang/String.charAt:(I)C
      35: invokestatic  #105                // Method isOperator:(C)Z
      38: ifeq          98
      41: iload_3
      42: invokestatic  #105                // Method isOperator:(C)Z
      45: ifeq          98
      48: aload_1
      49: iconst_1
      50: invokevirtual #99                 // Method java/lang/String.charAt:(I)C
      53: istore        4
      55: aload_1
      56: iconst_2
      57: invokestatic  #109                // Method kotlin/text/StringsKt.drop:(Ljava/lang/String;I)Ljava/lang/String;
      60: astore_1
      61: iload_3
      62: iload         4
      64: invokestatic  #111                // Method computePlusMinus:(CC)C
      67: istore        5
      69: new           #113                // class java/lang/StringBuilder
      72: dup
      73: invokespecial #114                // Method java/lang/StringBuilder."<init>":()V
      76: iload         5
      78: invokevirtual #118                // Method java/lang/StringBuilder.append:(C)Ljava/lang/StringBuilder;
      81: aload_1
      82: invokevirtual #121                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      85: invokevirtual #124                // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      88: astore_1
      89: aload_1
      90: iconst_0
      91: invokevirtual #99                 // Method java/lang/String.charAt:(I)C
      94: istore_3
      95: goto          128
      98: iload_3
      99: invokestatic  #105                // Method isOperator:(C)Z
     102: ifeq          128
     105: iload_3
     106: invokestatic  #130                // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
     109: astore_2
     110: aload_1
     111: iload_3
     112: invokestatic  #133                // Method java/lang/String.valueOf:(C)Ljava/lang/String;
     115: checkcast     #26                 // class java/lang/CharSequence
     118: invokestatic  #137                // Method kotlin/text/StringsKt.removePrefix:(Ljava/lang/String;Ljava/lang/CharSequence;)Ljava/lang/String;
     121: astore_1
     122: aload_1
     123: iconst_0
     124: invokevirtual #99                 // Method java/lang/String.charAt:(I)C
     127: istore_3
     128: iload_3
     129: invokestatic  #143                // Method kotlin/text/CharsKt.digitToIntOrNull:(C)Ljava/lang/Integer;
     132: ifnull        10
     135: new           #145                // class kotlin/text/Regex
     138: dup
     139: ldc           #147                // String ((-?)|(\\+?))?\\d+
     141: invokespecial #149                // Method kotlin/text/Regex."<init>":(Ljava/lang/String;)V
     144: astore_3
     145: aload_3
     146: aload_1
     147: checkcast     #26                 // class java/lang/CharSequence
     150: iconst_0
     151: iconst_2
     152: aconst_null
     153: invokestatic  #153                // Method kotlin/text/Regex.find$default:(Lkotlin/text/Regex;Ljava/lang/CharSequence;IILjava/lang/Object;)Lkotlin/text/MatchResult;
     156: astore        4
     158: aload         4
     160: ifnull        255
     163: aload_1
     164: aload         4
     166: invokeinterface #159,  1          // InterfaceMethod kotlin/text/MatchResult.getRange:()Lkotlin/ranges/IntRange;
     171: invokestatic  #163                // Method kotlin/text/StringsKt.substring:(Ljava/lang/String;Lkotlin/ranges/IntRange;)Ljava/lang/String;
     174: astore        5
     176: aload_2
     177: bipush        45
     179: istore        7
     181: dup
     182: ifnonnull     189
     185: pop
     186: goto          224
     189: invokevirtual #167                // Method java/lang/Character.charValue:()C
     192: iload         7
     194: if_icmpne     224
     197: bipush        45
     199: istore        7
     201: new           #113                // class java/lang/StringBuilder
     204: dup
     205: invokespecial #114                // Method java/lang/StringBuilder."<init>":()V
     208: iload         7
     210: invokevirtual #118                // Method java/lang/StringBuilder.append:(C)Ljava/lang/StringBuilder;
     213: aload         5
     215: invokevirtual #121                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     218: invokevirtual #124                // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     221: goto          226
     224: aload         5
     226: astore        6
     228: getstatic     #12                 // Field numbers:Ljava/util/List;
     231: aload         6
     233: invokestatic  #173                // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
     236: invokestatic  #176                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     239: invokeinterface #181,  2          // InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z
     244: pop
     245: aload_1
     246: aload         5
     248: checkcast     #26                 // class java/lang/CharSequence
     251: invokestatic  #137                // Method kotlin/text/StringsKt.removePrefix:(Ljava/lang/String;Ljava/lang/CharSequence;)Ljava/lang/String;
     254: astore_1
     255: aload_1
     256: invokevirtual #184                // Method java/lang/String.length:()I
     259: ifne          8
     262: invokestatic  #187                // Method compute:()V
     265: return

  public static final void compute();
    Code:
       0: getstatic     #12                 // Field numbers:Ljava/util/List;
       3: checkcast     #34                 // class java/lang/Iterable
       6: invokestatic  #202                // Method kotlin/collections/CollectionsKt.sumOfInt:(Ljava/lang/Iterable;)I
       9: istore_0
      10: getstatic     #71                 // Field java/lang/System.out:Ljava/io/PrintStream;
      13: iload_0
      14: invokevirtual #205                // Method java/io/PrintStream.println:(I)V
      17: getstatic     #12                 // Field numbers:Ljava/util/List;
      20: invokeinterface #208,  1          // InterfaceMethod java/util/List.clear:()V
      25: return

  public static final java.util.List<java.lang.Integer> getNumbers(java.lang.String);
    Code:
       0: aload_0
       1: ldc           #211                // String input
       3: invokestatic  #95                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: new           #213                // class java/util/ArrayList
       9: dup
      10: invokespecial #214                // Method java/util/ArrayList."<init>":()V
      13: checkcast     #178                // class java/util/List
      16: astore_1
      17: aload_0
      18: checkcast     #26                 // class java/lang/CharSequence
      21: iconst_1
      22: newarray       char
      24: astore_2
      25: aload_2
      26: iconst_0
      27: bipush        32
      29: castore
      30: aload_2
      31: iconst_0
      32: iconst_0
      33: bipush        6
      35: aconst_null
      36: invokestatic  #32                 // Method kotlin/text/StringsKt.split$default:(Ljava/lang/CharSequence;[CZIILjava/lang/Object;)Ljava/util/List;
      39: checkcast     #34                 // class java/lang/Iterable
      42: astore_2
      43: iconst_0
      44: istore_3
      45: aload_2
      46: invokeinterface #218,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      51: astore        4
      53: aload         4
      55: invokeinterface #224,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
      60: ifeq          101
      63: aload         4
      65: invokeinterface #228,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      70: astore        5
      72: aload         5
      74: checkcast     #44                 // class java/lang/String
      77: astore        6
      79: iconst_0
      80: istore        7
      82: aload_1
      83: aload         6
      85: invokestatic  #173                // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
      88: invokestatic  #176                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      91: invokeinterface #181,  2          // InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z
      96: pop
      97: nop
      98: goto          53
     101: nop
     102: aload_1
     103: areturn

  public static final void main();
    Code:
       0: nop
       1: invokestatic  #58                 // Method start:()V
       4: goto          17
       7: astore_0
       8: ldc           #239                // String Bye!
      10: getstatic     #71                 // Field java/lang/System.out:Ljava/io/PrintStream;
      13: swap
      14: invokevirtual #77                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      17: return
    Exception table:
       from    to  target type
           0     4     7   Class java/lang/Exception

  public static void main(java.lang.String[]);
    Code:
       0: invokestatic  #244                // Method main:()V
       3: return

  static {};
    Code:
       0: new           #213                // class java/util/ArrayList
       3: dup
       4: invokespecial #214                // Method java/util/ArrayList."<init>":()V
       7: checkcast     #178                // class java/util/List
      10: putstatic     #12                 // Field numbers:Ljava/util/List;
      13: return
}


package leetcode.a350_intersectTwoArrays

import java.util.*
import kotlin.math.min

/**
 * 给定两个数组，编写一个函数来计算它们的交集。
 *
 * 示例 1：
 * 输入：nums1 = [1,2,2,1], nums2 = [2,2]
 * 输出：[2,2]
 *
 * 示例 2:
 * 输入：nums1 = [4,9,5], nums2 = [9,4,9,8,4]
 * 输出：[4,9]
 *
 *
 * 说明：
 * 输出结果中每个元素出现的次数，应与元素在两个数组中出现次数的最小值一致。
 * 我们可以不考虑输出结果的顺序。
 * 进阶：
 * 如果给定的数组已经排好序呢？你将如何优化你的算法？
 * 如果 nums1 的大小比 nums2 小很多，哪种方法更优？
 * 如果 nums2 的元素存储在磁盘上，磁盘内存是有限的，并且你不能一次加载所有的元素到内存中，你该怎么办？
 *
 * @author neptune
 * @create 2020 07 13 10:39 下午
 */
class Solution {

    fun intersect(nums1: IntArray, nums2: IntArray): IntArray {
//        return nums2.intersect(nums1.asIterable()).toIntArray()
        nums1.sort()
        nums2.sort()
        val length1 = nums1.size
        val length2 = nums2.size
        val intersection = IntArray(min(length1, length2))
        var index1 = 0
        var index2 = 0
        var index = 0
        while (index1 < length1 && index2 < length2) {
            if (nums1[index1] < nums2[index2]) {
                index1++
            } else if (nums1[index1] > nums2[index2]) {
                index2++
            } else {
                intersection[index] = nums1[index1]
                index1++
                index2++
                index++
            }
        }
        return Arrays.copyOfRange(intersection, 0, index)
    }
}

fun main(args: Array<String>) {
    val solution = Solution()
    val nums1:IntArray = arrayOf(1,2,2,1).toIntArray()
    val nums2:IntArray = arrayOf(2).toIntArray()
//    val nums1:IntArray = arrayOf(1,2,2,1).toIntArray()
//    val nums2:IntArray = arrayOf(2,2).toIntArray()
    solution.intersect(nums1,nums2).iterator().forEach { el -> println(el) }
}

Compiled from "Solution.kt"
public final class leetcode.a350_intersectTwoArrays.Solution {
  public leetcode.a350_intersectTwoArrays.Solution();
    Code:
       0: aload_0
       1: invokespecial #8                  // Method java/lang/Object."<init>":()V
       4: return

  public final int[] intersect(int[], int[]);
    Code:
       0: aload_1
       1: ldc           #15                 // String nums1
       3: invokestatic  #21                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_2
       7: ldc           #23                 // String nums2
       9: invokestatic  #21                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      12: aload_1
      13: invokestatic  #29                 // Method kotlin/collections/ArraysKt.sort:([I)V
      16: aload_2
      17: invokestatic  #29                 // Method kotlin/collections/ArraysKt.sort:([I)V
      20: aload_1
      21: arraylength
      22: istore_3
      23: aload_2
      24: arraylength
      25: istore        4
      27: iload_3
      28: iload         4
      30: invokestatic  #35                 // Method java/lang/Math.min:(II)I
      33: newarray       int
      35: astore        5
      37: iconst_0
      38: istore        6
      40: iconst_0
      41: istore        7
      43: iconst_0
      44: istore        8
      46: iload         6
      48: iload_3
      49: if_icmpge     114
      52: iload         7
      54: iload         4
      56: if_icmpge     114
      59: aload_1
      60: iload         6
      62: iaload
      63: aload_2
      64: iload         7
      66: iaload
      67: if_icmpge     76
      70: iinc          6, 1
      73: goto          46
      76: aload_1
      77: iload         6
      79: iaload
      80: aload_2
      81: iload         7
      83: iaload
      84: if_icmple     93
      87: iinc          7, 1
      90: goto          46
      93: aload         5
      95: iload         8
      97: aload_1
      98: iload         6
     100: iaload
     101: iastore
     102: iinc          6, 1
     105: iinc          7, 1
     108: iinc          8, 1
     111: goto          46
     114: aload         5
     116: iconst_0
     117: iload         8
     119: invokestatic  #41                 // Method java/util/Arrays.copyOfRange:([III)[I
     122: dup
     123: ldc           #43                 // String copyOfRange(...)
     125: invokestatic  #46                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
     128: areturn
}

Compiled from "Solution.kt"
public final class leetcode.a350_intersectTwoArrays.SolutionKt {
  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: new           #17                 // class leetcode/a350_intersectTwoArrays/Solution
       9: dup
      10: invokespecial #21                 // Method leetcode/a350_intersectTwoArrays/Solution."<init>":()V
      13: astore_1
      14: iconst_4
      15: anewarray     #23                 // class java/lang/Integer
      18: astore_3
      19: aload_3
      20: iconst_0
      21: iconst_1
      22: invokestatic  #27                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      25: aastore
      26: aload_3
      27: iconst_1
      28: iconst_2
      29: invokestatic  #27                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      32: aastore
      33: aload_3
      34: iconst_2
      35: iconst_2
      36: invokestatic  #27                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      39: aastore
      40: aload_3
      41: iconst_3
      42: iconst_1
      43: invokestatic  #27                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      46: aastore
      47: aload_3
      48: invokestatic  #33                 // Method kotlin/collections/ArraysKt.toIntArray:([Ljava/lang/Integer;)[I
      51: astore_2
      52: iconst_1
      53: anewarray     #23                 // class java/lang/Integer
      56: astore        4
      58: aload         4
      60: iconst_0
      61: iconst_2
      62: invokestatic  #27                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      65: aastore
      66: aload         4
      68: invokestatic  #33                 // Method kotlin/collections/ArraysKt.toIntArray:([Ljava/lang/Integer;)[I
      71: astore_3
      72: aload_1
      73: aload_2
      74: aload_3
      75: invokevirtual #37                 // Method leetcode/a350_intersectTwoArrays/Solution.intersect:([I[I)[I
      78: invokestatic  #43                 // Method kotlin/jvm/internal/ArrayIteratorsKt.iterator:([I)Lkotlin/collections/IntIterator;
      81: checkcast     #45                 // class java/util/Iterator
      84: astore        4
      86: iconst_0
      87: istore        5
      89: aload         4
      91: astore        6
      93: aload         6
      95: invokeinterface #49,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     100: ifeq          138
     103: aload         6
     105: invokeinterface #53,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     110: astore        7
     112: aload         7
     114: checkcast     #55                 // class java/lang/Number
     117: invokevirtual #59                 // Method java/lang/Number.intValue:()I
     120: istore        8
     122: iconst_0
     123: istore        9
     125: getstatic     #65                 // Field java/lang/System.out:Ljava/io/PrintStream;
     128: iload         8
     130: invokevirtual #71                 // Method java/io/PrintStream.println:(I)V
     133: nop
     134: nop
     135: goto          93
     138: nop
     139: return
}


package leetcode.a167_twoSumInputSortArray_SIMPLE

/**
 * 两数之和 II - 输入有序数组
 *
 给定一个已按照升序排列 的有序数组，找到两个数使得它们相加之和等于目标数。
函数应该返回这两个下标值 index1 和 index2，其中 index1 必须小于 index2。

说明:
返回的下标值（index1 和 index2）不是从零开始的。
你可以假设每个输入只对应唯一的答案，而且你不可以重复使用相同的元素。
示例:

输入: numbers = [2, 7, 11, 15], target = 9
输出: [1,2]
解释: 2 与 7 之和等于目标数 9 。因此 index1 = 1, index2 = 2 。

 */
class Solution {

    /**
     * 解题思路：
     * https://leetcode-cn.com/problems/two-sum-ii-input-array-is-sorted/solution/shuang-zhi-zhen-on-shi-jian-fu-za-du-by-cyc2018/

    使用双指针，一个指针指向值较小的元素，一个指针指向值较大的元素。指向较小元素的指针从头向尾遍历，指向较大元素的指针从尾向头遍历。
    如果两个指针指向元素的和 sum == targetsum==target，那么得到要求的结果；
    如果 sum > targetsum>target，移动较大的元素，使 sumsum 变小一些；
    如果 sum < targetsum<target，移动较小的元素，使 sumsum 变大一些。
    数组中的元素最多遍历一次，时间复杂度为 O(N)O(N)。只使用了两个额外变量，空间复杂度为 O(1)O(1)。
     */
    fun twoSum(numbers: IntArray, target: Int): IntArray {
        if (numbers == null) return intArrayOf()
        var i = 0
        var j = numbers.size - 1
        while (i < j) {
            var sum = numbers[i] + numbers[j];
            if (sum == target) {
                return intArrayOf(i + 1, j + 1)
            } else if (sum < target) {
                i++
            } else {
                j--
            }
        }
        return intArrayOf()
    }
}

Compiled from "Solution.kt"
public final class leetcode.a167_twoSumInputSortArray_SIMPLE.Solution {
  public leetcode.a167_twoSumInputSortArray_SIMPLE.Solution();
    Code:
       0: aload_0
       1: invokespecial #8                  // Method java/lang/Object."<init>":()V
       4: return

  public final int[] twoSum(int[], int);
    Code:
       0: aload_1
       1: ldc           #15                 // String numbers
       3: invokestatic  #21                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: nop
       7: iconst_0
       8: istore_3
       9: aload_1
      10: arraylength
      11: iconst_1
      12: isub
      13: istore        4
      15: iload_3
      16: iload         4
      18: if_icmpge     78
      21: aload_1
      22: iload_3
      23: iaload
      24: aload_1
      25: iload         4
      27: iaload
      28: iadd
      29: istore        5
      31: iload         5
      33: iload_2
      34: if_icmpne     60
      37: iconst_2
      38: newarray       int
      40: astore        6
      42: aload         6
      44: iconst_0
      45: iload_3
      46: iconst_1
      47: iadd
      48: iastore
      49: aload         6
      51: iconst_1
      52: iload         4
      54: iconst_1
      55: iadd
      56: iastore
      57: aload         6
      59: areturn
      60: iload         5
      62: iload_2
      63: if_icmpge     72
      66: iinc          3, 1
      69: goto          15
      72: iinc          4, -1
      75: goto          15
      78: iconst_0
      79: newarray       int
      81: areturn
}


package leetcode.a392_isSubSequence_HARD

/**
 * 判断子序列
 *
 * 给定字符串 s 和 t ，判断 s 是否为 t 的子序列。
 * 你可以认为 s 和 t 中仅包含英文小写字母。字符串 t 可能会很长（长度 ~= 500,000），而 s 是个短字符串（长度 <=100）。
 *
 * 字符串的一个子序列是原始字符串删除一些（也可以不删除）字符而不改变剩余字符相对位置形成的新字符串。（例如，"ace"是"abcde"的一个子序列，而"aec"不是）。
 *
 * 示例 1:
 * s = "abc", t = "ahbgdc"
 * 返回 true.
 *
 * 示例 2:
 * s = "axc", t = "ahbgdc"
 * 返回 false.
 *
 * 后续挑战 :
 * 如果有大量输入的 S，称作S1, S2, ... , Sk 其中 k >= 10亿，你需要依次检查它们是否为 T 的子序列。在这种情况下，你会怎样改变代码？
 *
 * 来源：力扣（LeetCode）
 * 链接：https://leetcode-cn.com/problems/is-subsequence
 * 著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
 * @author neptune
 * @create 2020 07 27 9:27 下午
 */
class Solution {

    fun isSubsequence(s: String, t: String): Boolean {
        if (s.length == 0)
            return true;
        var indexS = 0
        var indexT = 0
        while (indexT < t.length) {
            if (t[indexT] == s[indexS]) {
                //指向s的指针只有在两个字符串相同时才会往右移
                indexS++;
                if (indexS == s.length)
                    return true;
            }
            //指向t的指针每次都会往右移一位
            indexT++;
        }
        return false
    }

}

Compiled from "Solution.kt"
public final class leetcode.a392_isSubSequence_HARD.Solution {
  public leetcode.a392_isSubSequence_HARD.Solution();
    Code:
       0: aload_0
       1: invokespecial #8                  // Method java/lang/Object."<init>":()V
       4: return

  public final boolean isSubsequence(java.lang.String, java.lang.String);
    Code:
       0: aload_1
       1: ldc           #15                 // String s
       3: invokestatic  #21                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_2
       7: ldc           #23                 // String t
       9: invokestatic  #21                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      12: aload_1
      13: invokevirtual #29                 // Method java/lang/String.length:()I
      16: ifne          21
      19: iconst_1
      20: ireturn
      21: iconst_0
      22: istore_3
      23: iconst_0
      24: istore        4
      26: iload         4
      28: aload_2
      29: invokevirtual #29                 // Method java/lang/String.length:()I
      32: if_icmpge     68
      35: aload_2
      36: iload         4
      38: invokevirtual #33                 // Method java/lang/String.charAt:(I)C
      41: aload_1
      42: iload_3
      43: invokevirtual #33                 // Method java/lang/String.charAt:(I)C
      46: if_icmpne     62
      49: iinc          3, 1
      52: iload_3
      53: aload_1
      54: invokevirtual #29                 // Method java/lang/String.length:()I
      57: if_icmpne     62
      60: iconst_1
      61: ireturn
      62: iinc          4, 1
      65: goto          26
      68: iconst_0
      69: ireturn
}


package part06

import java.util.TreeSet

fun main(args: Array<String>) {

    /*val treeSet = TreeSet<String>()

    treeSet.add("adbbcc")
    treeSet.add("abbabc")
    treeSet.add("acbcc")
    treeSet.add("abbc")
    treeSet.add("abcc")

    val iterator = treeSet.iterator()
    while (iterator.hasNext()) {
        println(iterator.next())
    }*/

    /*
    //元素自身具备比较性，实现Comparable接口即可。
    val treeSet = TreeSet<Student>()
    treeSet.add(Student("aa", 11))
    treeSet.add(Student("bb", 22))
    treeSet.add(Student("cc", 8))
    treeSet.add(Student("dd", 6))
    treeSet.add(Student("ee", 6))

    val iterator = treeSet.iterator()
    while (iterator.hasNext()) {
        val student = iterator.next()
        println("student:${student.name}--${student.age}")
    }*/

    //当元素不具备比较性，我们可以传入自定义比较器
    val treeSet = TreeSet<Student>(MyCompare())
    treeSet.add(Student("aa", 11))
    treeSet.add(Student("bb", 22))
    treeSet.add(Student("cc", 8))
    treeSet.add(Student("dd", 6))
    treeSet.add(Student("ee", 6))

    val iterator = treeSet.iterator()
    while (iterator.hasNext()) {
        val student = iterator.next()
        println("student:${student.name}--${student.age}")
    }

}

/**
 * 实现Comparable接口，实现比较方法
 */
private data class Student constructor(var name: String, var age: Int) : Comparable<Student> {
    override fun compareTo(other: Student): Int {
        if (age > other.age) return 1
        //注意：比较了主要条件以后，需要比较次要条件。
        if (age == other.age) {
            return name.compareTo(other.name)
        }
        return -1
    }
}

/**
 * 自定义比较器（推荐这种比较形式）
 */
private class MyCompare : Comparator<Student> {
    override fun compare(o1: Student, o2: Student): Int {
        val compareTo = o1.age.compareTo(o2.age)
        if (compareTo == 0) {
            return o1.name.compareTo(o2.name)
        }
        return compareTo
    }
}

Compiled from "part06.kt"
public final class part06.Part06Kt {
  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: new           #17                 // class java/util/TreeSet
       9: dup
      10: new           #19                 // class part06/MyCompare
      13: dup
      14: invokespecial #23                 // Method part06/MyCompare."<init>":()V
      17: checkcast     #25                 // class java/util/Comparator
      20: invokespecial #28                 // Method java/util/TreeSet."<init>":(Ljava/util/Comparator;)V
      23: astore_1
      24: aload_1
      25: new           #30                 // class part06/Student
      28: dup
      29: ldc           #32                 // String aa
      31: bipush        11
      33: invokespecial #35                 // Method part06/Student."<init>":(Ljava/lang/String;I)V
      36: invokevirtual #39                 // Method java/util/TreeSet.add:(Ljava/lang/Object;)Z
      39: pop
      40: aload_1
      41: new           #30                 // class part06/Student
      44: dup
      45: ldc           #41                 // String bb
      47: bipush        22
      49: invokespecial #35                 // Method part06/Student."<init>":(Ljava/lang/String;I)V
      52: invokevirtual #39                 // Method java/util/TreeSet.add:(Ljava/lang/Object;)Z
      55: pop
      56: aload_1
      57: new           #30                 // class part06/Student
      60: dup
      61: ldc           #43                 // String cc
      63: bipush        8
      65: invokespecial #35                 // Method part06/Student."<init>":(Ljava/lang/String;I)V
      68: invokevirtual #39                 // Method java/util/TreeSet.add:(Ljava/lang/Object;)Z
      71: pop
      72: aload_1
      73: new           #30                 // class part06/Student
      76: dup
      77: ldc           #45                 // String dd
      79: bipush        6
      81: invokespecial #35                 // Method part06/Student."<init>":(Ljava/lang/String;I)V
      84: invokevirtual #39                 // Method java/util/TreeSet.add:(Ljava/lang/Object;)Z
      87: pop
      88: aload_1
      89: new           #30                 // class part06/Student
      92: dup
      93: ldc           #47                 // String ee
      95: bipush        6
      97: invokespecial #35                 // Method part06/Student."<init>":(Ljava/lang/String;I)V
     100: invokevirtual #39                 // Method java/util/TreeSet.add:(Ljava/lang/Object;)Z
     103: pop
     104: aload_1
     105: invokevirtual #51                 // Method java/util/TreeSet.iterator:()Ljava/util/Iterator;
     108: dup
     109: ldc           #53                 // String iterator(...)
     111: invokestatic  #56                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
     114: astore_2
     115: aload_2
     116: invokeinterface #62,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     121: ifeq          184
     124: aload_2
     125: invokeinterface #66,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     130: dup
     131: ldc           #68                 // String next(...)
     133: invokestatic  #56                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
     136: checkcast     #30                 // class part06/Student
     139: astore_3
     140: new           #70                 // class java/lang/StringBuilder
     143: dup
     144: invokespecial #71                 // Method java/lang/StringBuilder."<init>":()V
     147: ldc           #73                 // String student:
     149: invokevirtual #77                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     152: aload_3
     153: invokevirtual #81                 // Method part06/Student.getName:()Ljava/lang/String;
     156: invokevirtual #77                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     159: ldc           #83                 // String --
     161: invokevirtual #77                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     164: aload_3
     165: invokevirtual #87                 // Method part06/Student.getAge:()I
     168: invokevirtual #90                 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
     171: invokevirtual #93                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     174: getstatic     #99                 // Field java/lang/System.out:Ljava/io/PrintStream;
     177: swap
     178: invokevirtual #105                // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     181: goto          115
     184: return
}


package org.example

class Solution {
    enum class Color {
        Red, Blue, None;
    }

    fun isBipartite(graph: Array<IntArray>): Boolean {
        val queue = ArrayDeque<Int>()
        val colorArray = Array(graph.size) { Color.None }
        for (index in colorArray.indices) {
            if (colorArray[index] != Color.None) continue
            colorArray[index] = Color.Red
            queue.addLast(index)
            while (queue.isNotEmpty()) {
                val node = queue.removeFirst()
                val nodeColor = colorArray[node]
                val neighborColor = if (nodeColor == Color.Red) Color.Blue else Color.Red
                val neighbors = graph[node]
                for (neighbor in neighbors) {
                    if (colorArray[neighbor] == Color.None) {
                        colorArray[neighbor] = neighborColor
                        queue.addLast(neighbor)
                    } else if (colorArray[neighbor] == nodeColor) {
                        return false
                    }
                }
            }
        }
        return true
    }

}

fun parseGraph(s: String): Array<IntArray> {
    var bracketCount = 0
    val array = mutableListOf<IntArray>()
    var innerList: MutableList<Int> = mutableListOf()
    for (char in s.toCharArray()) {
        if (char == '[') {
            bracketCount++
            if (bracketCount == 2) {
                innerList = mutableListOf()
            }
        } else if (char == ']') {
            bracketCount--
            if (bracketCount == 1) {
                array.add(innerList.toIntArray())
            }
        } else if (char == ',') {
            continue
        } else {
            if (bracketCount == 2) {
                innerList.add(char.digitToInt())
            }
        }
    }
    return array.toTypedArray()
}

fun main() {
    val graph1 = arrayOf(
        intArrayOf(1, 2, 3),
        intArrayOf(0, 2),
        intArrayOf(0, 1, 3),
        intArrayOf(0, 2)
    )
    val graph2 = arrayOf(
        intArrayOf(1, 3),
        intArrayOf(0, 2),
        intArrayOf(1, 3),
        intArrayOf(0, 2)
    )

    val graph74 = parseGraph("[[],[3],[],[1],[]]")
    val graph76 = parseGraph("[[],[2,4,6],[1,4,8,9],[7,8],[1,2,8,9],[6,9],[1,5,7,8,9],[3,6,9],[2,3,4,6,9],[2,4,5,6,7,8]]")

    // [[1],[0,3],[3],[1,2]]
    val graph78 = arrayOf(
        intArrayOf(1),
        intArrayOf(0, 3),
        intArrayOf(3),
        intArrayOf(1, 2)
    )

    val solution = Solution()
    assert(!solution.isBipartite(graph1))
    assert(solution.isBipartite(graph2))
    assert(solution.isBipartite(graph74))
    assert(!solution.isBipartite(graph76))
    assert(solution.isBipartite(graph78))
}

Compiled from "BiPartiteGraph.kt"
public final class org.example.BiPartiteGraphKt {
  public static final int[][] parseGraph(java.lang.String);
    Code:
       0: aload_0
       1: ldc           #9                  // String s
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: iconst_0
       7: istore_1
       8: new           #17                 // class java/util/ArrayList
      11: dup
      12: invokespecial #21                 // Method java/util/ArrayList."<init>":()V
      15: checkcast     #23                 // class java/util/List
      18: astore_2
      19: new           #17                 // class java/util/ArrayList
      22: dup
      23: invokespecial #21                 // Method java/util/ArrayList."<init>":()V
      26: checkcast     #23                 // class java/util/List
      29: astore_3
      30: aload_0
      31: invokevirtual #29                 // Method java/lang/String.toCharArray:()[C
      34: dup
      35: ldc           #31                 // String toCharArray(...)
      37: invokestatic  #34                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
      40: astore        4
      42: iconst_0
      43: istore        5
      45: aload         4
      47: arraylength
      48: istore        6
      50: iload         5
      52: iload         6
      54: if_icmpge     176
      57: aload         4
      59: iload         5
      61: caload
      62: istore        7
      64: iload         7
      66: lookupswitch  { // 3
                    44: 147
                    91: 100
                    93: 122
               default: 150
          }
     100: iinc          1, 1
     103: iload_1
     104: iconst_2
     105: if_icmpne     170
     108: new           #17                 // class java/util/ArrayList
     111: dup
     112: invokespecial #21                 // Method java/util/ArrayList."<init>":()V
     115: checkcast     #23                 // class java/util/List
     118: astore_3
     119: goto          170
     122: iinc          1, -1
     125: iload_1
     126: iconst_1
     127: if_icmpne     170
     130: aload_2
     131: aload_3
     132: checkcast     #36                 // class java/util/Collection
     135: invokestatic  #42                 // Method kotlin/collections/CollectionsKt.toIntArray:(Ljava/util/Collection;)[I
     138: invokeinterface #46,  2           // InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z
     143: pop
     144: goto          170
     147: goto          170
     150: iload_1
     151: iconst_2
     152: if_icmpne     170
     155: aload_3
     156: iload         7
     158: invokestatic  #52                 // Method kotlin/text/CharsKt.digitToInt:(C)I
     161: invokestatic  #58                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     164: invokeinterface #46,  2           // InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z
     169: pop
     170: iinc          5, 1
     173: goto          50
     176: aload_2
     177: checkcast     #36                 // class java/util/Collection
     180: astore        4
     182: iconst_0
     183: istore        5
     185: aload         4
     187: astore        6
     189: aload         6
     191: iconst_0
     192: anewarray     #60                 // class "[I"
     195: invokeinterface #64,  2           // InterfaceMethod java/util/Collection.toArray:([Ljava/lang/Object;)[Ljava/lang/Object;
     200: checkcast     #66                 // class "[[I"
     203: areturn

  public static final void main();
    Code:
       0: iconst_4
       1: anewarray     #60                 // class "[I"
       4: astore_1
       5: aload_1
       6: iconst_0
       7: iconst_3
       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_2
      18: iastore
      19: aload_2
      20: iconst_2
      21: iconst_3
      22: iastore
      23: aload_2
      24: aastore
      25: aload_1
      26: iconst_1
      27: iconst_2
      28: newarray       int
      30: astore_2
      31: aload_2
      32: iconst_0
      33: iconst_0
      34: iastore
      35: aload_2
      36: iconst_1
      37: iconst_2
      38: iastore
      39: aload_2
      40: aastore
      41: aload_1
      42: iconst_2
      43: iconst_3
      44: newarray       int
      46: astore_2
      47: aload_2
      48: iconst_0
      49: iconst_0
      50: iastore
      51: aload_2
      52: iconst_1
      53: iconst_1
      54: iastore
      55: aload_2
      56: iconst_2
      57: iconst_3
      58: iastore
      59: aload_2
      60: aastore
      61: aload_1
      62: iconst_3
      63: iconst_2
      64: newarray       int
      66: astore_2
      67: aload_2
      68: iconst_0
      69: iconst_0
      70: iastore
      71: aload_2
      72: iconst_1
      73: iconst_2
      74: iastore
      75: aload_2
      76: aastore
      77: aload_1
      78: astore_0
      79: iconst_4
      80: anewarray     #60                 // class "[I"
      83: astore_2
      84: aload_2
      85: iconst_0
      86: iconst_2
      87: newarray       int
      89: astore_3
      90: aload_3
      91: iconst_0
      92: iconst_1
      93: iastore
      94: aload_3
      95: iconst_1
      96: iconst_3
      97: iastore
      98: aload_3
      99: aastore
     100: aload_2
     101: iconst_1
     102: iconst_2
     103: newarray       int
     105: astore_3
     106: aload_3
     107: iconst_0
     108: iconst_0
     109: iastore
     110: aload_3
     111: iconst_1
     112: iconst_2
     113: iastore
     114: aload_3
     115: aastore
     116: aload_2
     117: iconst_2
     118: iconst_2
     119: newarray       int
     121: astore_3
     122: aload_3
     123: iconst_0
     124: iconst_1
     125: iastore
     126: aload_3
     127: iconst_1
     128: iconst_3
     129: iastore
     130: aload_3
     131: aastore
     132: aload_2
     133: iconst_3
     134: iconst_2
     135: newarray       int
     137: astore_3
     138: aload_3
     139: iconst_0
     140: iconst_0
     141: iastore
     142: aload_3
     143: iconst_1
     144: iconst_2
     145: iastore
     146: aload_3
     147: aastore
     148: aload_2
     149: astore_1
     150: ldc           #83                 // String [[],[3],[],[1],[]]
     152: invokestatic  #85                 // Method parseGraph:(Ljava/lang/String;)[[I
     155: astore_2
     156: ldc           #87                 // String [[],[2,4,6],[1,4,8,9],[7,8],[1,2,8,9],[6,9],[1,5,7,8,9],[3,6,9],[2,3,4,6,9],[2,4,5,6,7,8]]
     158: invokestatic  #85                 // Method parseGraph:(Ljava/lang/String;)[[I
     161: astore_3
     162: iconst_4
     163: anewarray     #60                 // class "[I"
     166: astore        5
     168: aload         5
     170: iconst_0
     171: iconst_1
     172: newarray       int
     174: astore        6
     176: aload         6
     178: iconst_0
     179: iconst_1
     180: iastore
     181: aload         6
     183: aastore
     184: aload         5
     186: iconst_1
     187: iconst_2
     188: newarray       int
     190: astore        6
     192: aload         6
     194: iconst_0
     195: iconst_0
     196: iastore
     197: aload         6
     199: iconst_1
     200: iconst_3
     201: iastore
     202: aload         6
     204: aastore
     205: aload         5
     207: iconst_2
     208: iconst_1
     209: newarray       int
     211: astore        6
     213: aload         6
     215: iconst_0
     216: iconst_3
     217: iastore
     218: aload         6
     220: aastore
     221: aload         5
     223: iconst_3
     224: iconst_2
     225: newarray       int
     227: astore        6
     229: aload         6
     231: iconst_0
     232: iconst_1
     233: iastore
     234: aload         6
     236: iconst_1
     237: iconst_2
     238: iastore
     239: aload         6
     241: aastore
     242: aload         5
     244: astore        4
     246: new           #89                 // class org/example/Solution
     249: dup
     250: invokespecial #90                 // Method org/example/Solution."<init>":()V
     253: astore        5
     255: aload         5
     257: aload_0
     258: invokevirtual #94                 // Method org/example/Solution.isBipartite:([[I)Z
     261: ifne          268
     264: iconst_1
     265: goto          269
     268: iconst_0
     269: istore        6
     271: getstatic     #100                // Field kotlin/_Assertions.ENABLED:Z
     274: ifeq          296
     277: iload         6
     279: ifne          296
     282: ldc           #102                // String Assertion failed
     284: astore        7
     286: new           #104                // class java/lang/AssertionError
     289: dup
     290: aload         7
     292: invokespecial #107                // Method java/lang/AssertionError."<init>":(Ljava/lang/Object;)V
     295: athrow
     296: aload         5
     298: aload_1
     299: invokevirtual #94                 // Method org/example/Solution.isBipartite:([[I)Z
     302: istore        6
     304: getstatic     #100                // Field kotlin/_Assertions.ENABLED:Z
     307: ifeq          329
     310: iload         6
     312: ifne          329
     315: ldc           #102                // String Assertion failed
     317: astore        7
     319: new           #104                // class java/lang/AssertionError
     322: dup
     323: aload         7
     325: invokespecial #107                // Method java/lang/AssertionError."<init>":(Ljava/lang/Object;)V
     328: athrow
     329: aload         5
     331: aload_2
     332: invokevirtual #94                 // Method org/example/Solution.isBipartite:([[I)Z
     335: istore        6
     337: getstatic     #100                // Field kotlin/_Assertions.ENABLED:Z
     340: ifeq          362
     343: iload         6
     345: ifne          362
     348: ldc           #102                // String Assertion failed
     350: astore        7
     352: new           #104                // class java/lang/AssertionError
     355: dup
     356: aload         7
     358: invokespecial #107                // Method java/lang/AssertionError."<init>":(Ljava/lang/Object;)V
     361: athrow
     362: aload         5
     364: aload_3
     365: invokevirtual #94                 // Method org/example/Solution.isBipartite:([[I)Z
     368: ifne          375
     371: iconst_1
     372: goto          376
     375: iconst_0
     376: istore        6
     378: getstatic     #100                // Field kotlin/_Assertions.ENABLED:Z
     381: ifeq          403
     384: iload         6
     386: ifne          403
     389: ldc           #102                // String Assertion failed
     391: astore        7
     393: new           #104                // class java/lang/AssertionError
     396: dup
     397: aload         7
     399: invokespecial #107                // Method java/lang/AssertionError."<init>":(Ljava/lang/Object;)V
     402: athrow
     403: aload         5
     405: aload         4
     407: invokevirtual #94                 // Method org/example/Solution.isBipartite:([[I)Z
     410: istore        6
     412: getstatic     #100                // Field kotlin/_Assertions.ENABLED:Z
     415: ifeq          437
     418: iload         6
     420: ifne          437
     423: ldc           #102                // String Assertion failed
     425: astore        7
     427: new           #104                // class java/lang/AssertionError
     430: dup
     431: aload         7
     433: invokespecial #107                // Method java/lang/AssertionError."<init>":(Ljava/lang/Object;)V
     436: athrow
     437: return

  public static void main(java.lang.String[]);
    Code:
       0: invokestatic  #117                // Method main:()V
       3: return
}


package ceres.geo

import kotlin.math.*

// Floyd-Rivest selection algorithm from https://github.com/mourner/quickselect

fun quickselect(
    arr: Array<Double>,
    k: Int,
    left: Int = 0,
    right: Int = arr.size - 1,
    compare: Comparator<Double> = defaultCompare
) {
    quickselectStep(arr, k, left, right, compare)
}

fun quickselectStep(
    arr: Array<Double>,
    k: Int,
    l: Int,
    r: Int,
    compare: Comparator<Double>
){
    var left = l
    var right = r

    while(right > left) {
        if (right - left > 600) {
            val n = right - left + 1
            val m = k - left
            val z = ln(n.toFloat())
            val s = 0.5 * exp(2 * z / 3)
            val sd = 0.5 * sqrt(z * s * (n - s) / n) * (if (m - n / 2 < 0) -1 else 1)
            val newLeft = max(left, floor(k - m * s / n + sd).toInt())
            val newRight = min(right, floor(k + (n - m) * s / n + sd).toInt())
            quickselectStep(arr, k, newLeft, newRight, compare)
        }

        val t = arr[k]
        var i = left
        var j = right

        swap(arr, left, k)
        if (compare.compare(arr[right], t) > 0) swap(arr, left, right)

        while (i < j) {
            swap(arr, i, j)
            i++
            j++
            while (compare.compare(arr[i], t) < 0) i++
            while (compare.compare(arr[j], t) > 0) i++
        }

        if (compare.compare(arr[left], t) == 0) swap(arr, left, j)
        else {
            j++
            swap(arr, j, right)
        }

        if (j <= k) left = j + 1
        if (k <= j) right = j - 1
    }
}

fun swap(arr: Array<Double>, i: Int, j: Int) {
    val tmp = arr[i]
    arr[i] = arr[j]
    arr[j] = tmp
}

val defaultCompare: Comparator<Double> = object : Comparator<Double> {
    override fun compare(a: Double, b: Double): Int = a.compareTo(b)
}

Compiled from "Selection.kt"
public final class ceres.geo.SelectionKt$defaultCompare$1 implements java.util.Comparator<java.lang.Double> {
  ceres.geo.SelectionKt$defaultCompare$1();
    Code:
       0: aload_0
       1: invokespecial #11                 // Method java/lang/Object."<init>":()V
       4: return

  public int compare(double, double);
    Code:
       0: dload_1
       1: dload_3
       2: invokestatic  #19                 // Method java/lang/Double.compare:(DD)I
       5: ireturn

  public int compare(java.lang.Object, java.lang.Object);
    Code:
       0: aload_0
       1: aload_1
       2: checkcast     #25                 // class java/lang/Number
       5: invokevirtual #29                 // Method java/lang/Number.doubleValue:()D
       8: aload_2
       9: checkcast     #25                 // class java/lang/Number
      12: invokevirtual #29                 // Method java/lang/Number.doubleValue:()D
      15: invokevirtual #30                 // Method compare:(DD)I
      18: ireturn
}

Compiled from "Selection.kt"
public final class ceres.geo.SelectionKt {
  private static final java.util.Comparator<java.lang.Double> defaultCompare;

  public static final void quickselect(java.lang.Double[], int, int, int, java.util.Comparator<java.lang.Double>);
    Code:
       0: aload_0
       1: ldc           #10                 // String arr
       3: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload         4
       8: ldc           #18                 // String compare
      10: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      13: aload_0
      14: iload_1
      15: iload_2
      16: iload_3
      17: aload         4
      19: invokestatic  #21                 // Method quickselectStep:([Ljava/lang/Double;IIILjava/util/Comparator;)V
      22: return

  public static void quickselect$default(java.lang.Double[], int, int, int, java.util.Comparator, int, java.lang.Object);
    Code:
       0: iload         5
       2: iconst_4
       3: iand
       4: ifeq          9
       7: iconst_0
       8: istore_2
       9: iload         5
      11: bipush        8
      13: iand
      14: ifeq          22
      17: aload_0
      18: arraylength
      19: iconst_1
      20: isub
      21: istore_3
      22: iload         5
      24: bipush        16
      26: iand
      27: ifeq          35
      30: getstatic     #32                 // Field defaultCompare:Ljava/util/Comparator;
      33: astore        4
      35: aload_0
      36: iload_1
      37: iload_2
      38: iload_3
      39: aload         4
      41: invokestatic  #34                 // Method quickselect:([Ljava/lang/Double;IIILjava/util/Comparator;)V
      44: return

  public static final void quickselectStep(java.lang.Double[], int, int, int, java.util.Comparator<java.lang.Double>);
    Code:
       0: aload_0
       1: ldc           #10                 // String arr
       3: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload         4
       8: ldc           #18                 // String compare
      10: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      13: iload_2
      14: istore        5
      16: iload_3
      17: istore        6
      19: iload         6
      21: iload         5
      23: if_icmple     382
      26: iload         6
      28: iload         5
      30: isub
      31: sipush        600
      34: if_icmple     193
      37: iload         6
      39: iload         5
      41: isub
      42: iconst_1
      43: iadd
      44: istore        7
      46: iload_1
      47: iload         5
      49: isub
      50: istore        8
      52: iload         7
      54: i2f
      55: f2d
      56: invokestatic  #40                 // Method java/lang/Math.log:(D)D
      59: d2f
      60: fstore        9
      62: ldc2_w        #41                 // double 0.5d
      65: iconst_2
      66: i2f
      67: fload         9
      69: fmul
      70: iconst_3
      71: i2f
      72: fdiv
      73: f2d
      74: invokestatic  #45                 // Method java/lang/Math.exp:(D)D
      77: d2f
      78: f2d
      79: dmul
      80: dstore        10
      82: ldc2_w        #41                 // double 0.5d
      85: fload         9
      87: f2d
      88: dload         10
      90: dmul
      91: iload         7
      93: i2d
      94: dload         10
      96: dsub
      97: dmul
      98: iload         7
     100: i2d
     101: ddiv
     102: invokestatic  #48                 // Method java/lang/Math.sqrt:(D)D
     105: dmul
     106: iload         8
     108: iload         7
     110: iconst_2
     111: idiv
     112: isub
     113: ifge          120
     116: iconst_m1
     117: goto          121
     120: iconst_1
     121: i2d
     122: dmul
     123: dstore        12
     125: iload         5
     127: iload_1
     128: i2d
     129: iload         8
     131: i2d
     132: dload         10
     134: dmul
     135: iload         7
     137: i2d
     138: ddiv
     139: dsub
     140: dload         12
     142: dadd
     143: invokestatic  #51                 // Method java/lang/Math.floor:(D)D
     146: d2i
     147: invokestatic  #55                 // Method java/lang/Math.max:(II)I
     150: istore        14
     152: iload         6
     154: iload_1
     155: i2d
     156: iload         7
     158: iload         8
     160: isub
     161: i2d
     162: dload         10
     164: dmul
     165: iload         7
     167: i2d
     168: ddiv
     169: dadd
     170: dload         12
     172: dadd
     173: invokestatic  #51                 // Method java/lang/Math.floor:(D)D
     176: d2i
     177: invokestatic  #58                 // Method java/lang/Math.min:(II)I
     180: istore        15
     182: aload_0
     183: iload_1
     184: iload         14
     186: iload         15
     188: aload         4
     190: invokestatic  #21                 // Method quickselectStep:([Ljava/lang/Double;IIILjava/util/Comparator;)V
     193: aload_0
     194: iload_1
     195: aaload
     196: invokevirtual #64                 // Method java/lang/Double.doubleValue:()D
     199: dstore        7
     201: iload         5
     203: istore        9
     205: iload         6
     207: istore        10
     209: aload_0
     210: iload         5
     212: iload_1
     213: invokestatic  #68                 // Method swap:([Ljava/lang/Double;II)V
     216: aload         4
     218: aload_0
     219: iload         6
     221: aaload
     222: dload         7
     224: invokestatic  #72                 // Method java/lang/Double.valueOf:(D)Ljava/lang/Double;
     227: invokeinterface #77,  3           // InterfaceMethod java/util/Comparator.compare:(Ljava/lang/Object;Ljava/lang/Object;)I
     232: ifle          243
     235: aload_0
     236: iload         5
     238: iload         6
     240: invokestatic  #68                 // Method swap:([Ljava/lang/Double;II)V
     243: iload         9
     245: iload         10
     247: if_icmpge     314
     250: aload_0
     251: iload         9
     253: iload         10
     255: invokestatic  #68                 // Method swap:([Ljava/lang/Double;II)V
     258: iinc          9, 1
     261: iinc          10, 1
     264: aload         4
     266: aload_0
     267: iload         9
     269: aaload
     270: dload         7
     272: invokestatic  #72                 // Method java/lang/Double.valueOf:(D)Ljava/lang/Double;
     275: invokeinterface #77,  3           // InterfaceMethod java/util/Comparator.compare:(Ljava/lang/Object;Ljava/lang/Object;)I
     280: ifge          289
     283: iinc          9, 1
     286: goto          264
     289: aload         4
     291: aload_0
     292: iload         10
     294: aaload
     295: dload         7
     297: invokestatic  #72                 // Method java/lang/Double.valueOf:(D)Ljava/lang/Double;
     300: invokeinterface #77,  3           // InterfaceMethod java/util/Comparator.compare:(Ljava/lang/Object;Ljava/lang/Object;)I
     305: ifle          243
     308: iinc          9, 1
     311: goto          289
     314: aload         4
     316: aload_0
     317: iload         5
     319: aaload
     320: dload         7
     322: invokestatic  #72                 // Method java/lang/Double.valueOf:(D)Ljava/lang/Double;
     325: invokeinterface #77,  3           // InterfaceMethod java/util/Comparator.compare:(Ljava/lang/Object;Ljava/lang/Object;)I
     330: ifne          344
     333: aload_0
     334: iload         5
     336: iload         10
     338: invokestatic  #68                 // Method swap:([Ljava/lang/Double;II)V
     341: goto          355
     344: iinc          10, 1
     347: aload_0
     348: iload         10
     350: iload         6
     352: invokestatic  #68                 // Method swap:([Ljava/lang/Double;II)V
     355: iload         10
     357: iload_1
     358: if_icmpgt     367
     361: iload         10
     363: iconst_1
     364: iadd
     365: istore        5
     367: iload_1
     368: iload         10
     370: if_icmpgt     19
     373: iload         10
     375: iconst_1
     376: isub
     377: istore        6
     379: goto          19
     382: return

  public static final void swap(java.lang.Double[], int, int);
    Code:
       0: aload_0
       1: ldc           #10                 // String arr
       3: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: iload_1
       8: aaload
       9: invokevirtual #64                 // Method java/lang/Double.doubleValue:()D
      12: dstore_3
      13: aload_0
      14: iload_1
      15: aload_0
      16: iload_2
      17: aaload
      18: aastore
      19: aload_0
      20: iload_2
      21: dload_3
      22: invokestatic  #72                 // Method java/lang/Double.valueOf:(D)Ljava/lang/Double;
      25: aastore
      26: return

  public static final java.util.Comparator<java.lang.Double> getDefaultCompare();
    Code:
       0: getstatic     #32                 // Field defaultCompare:Ljava/util/Comparator;
       3: areturn

  static {};
    Code:
       0: new           #100                // class ceres/geo/SelectionKt$defaultCompare$1
       3: dup
       4: invokespecial #103                // Method ceres/geo/SelectionKt$defaultCompare$1."<init>":()V
       7: checkcast     #74                 // class java/util/Comparator
      10: putstatic     #32                 // Field defaultCompare:Ljava/util/Comparator;
      13: return
}


package com.hackerrank

import java.util.*

val magicSquares = arrayOf(
        intArrayOf(8, 1, 6, 3, 5, 7, 4, 9, 2),
        intArrayOf(4, 3, 8, 9, 5, 1, 2, 7, 6),
        intArrayOf(2, 9, 4, 7, 5, 3, 6, 1, 8),
        intArrayOf(6, 7, 2, 1, 5, 9, 8, 3, 4),
        intArrayOf(6, 1, 8, 7, 5, 3, 2, 9, 4),
        intArrayOf(8, 3, 4, 1, 5, 9, 6, 7, 2),
        intArrayOf(4, 9, 2, 3, 5, 7, 8, 1, 6),
        intArrayOf(2, 7, 6, 9, 5, 1, 4, 3, 8))

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

    for (i in 0..2) {
        for (j in 0..2) {
            arr[i * 3 + j] = scan.nextInt()
        }
    }

    println(minimalCost(arr))
}

fun minimalCost(arr: IntArray): Int {
    return magicSquares.map {
        arr.zip(it)
                .filter { (first, second) -> first != second }
                .sumBy { (first, second) -> Math.abs(first - second) }
    }.min()!!
}

Compiled from "FormingAMagicSquare.kt"
public final class com.hackerrank.FormingAMagicSquareKt {
  private static final int[][] magicSquares;

  public static final int[][] getMagicSquares();
    Code:
       0: getstatic     #11                 // Field magicSquares:[[I
       3: areturn

  public static final void main(java.lang.String[]);
    Code:
       0: aload_0
       1: ldc           #15                 // String args
       3: invokestatic  #21                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: new           #23                 // class java/util/Scanner
       9: dup
      10: getstatic     #29                 // Field java/lang/System.in:Ljava/io/InputStream;
      13: invokespecial #33                 // Method java/util/Scanner."<init>":(Ljava/io/InputStream;)V
      16: astore_1
      17: bipush        9
      19: newarray       int
      21: astore_2
      22: iconst_0
      23: istore_3
      24: iload_3
      25: iconst_3
      26: if_icmpge     62
      29: iconst_0
      30: istore        4
      32: iload         4
      34: iconst_3
      35: if_icmpge     56
      38: aload_2
      39: iload_3
      40: iconst_3
      41: imul
      42: iload         4
      44: iadd
      45: aload_1
      46: invokevirtual #37                 // Method java/util/Scanner.nextInt:()I
      49: iastore
      50: iinc          4, 1
      53: goto          32
      56: iinc          3, 1
      59: goto          24
      62: aload_2
      63: invokestatic  #41                 // Method minimalCost:([I)I
      66: istore_3
      67: getstatic     #45                 // Field java/lang/System.out:Ljava/io/PrintStream;
      70: iload_3
      71: invokevirtual #51                 // Method java/io/PrintStream.println:(I)V
      74: return

  public static final int minimalCost(int[]);
    Code:
       0: aload_0
       1: ldc           #61                 // String arr
       3: invokestatic  #21                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: getstatic     #11                 // Field magicSquares:[[I
       9: checkcast     #63                 // class "[Ljava/lang/Object;"
      12: astore_1
      13: iconst_0
      14: istore_2
      15: aload_1
      16: astore_3
      17: new           #65                 // class java/util/ArrayList
      20: dup
      21: aload_1
      22: arraylength
      23: invokespecial #67                 // Method java/util/ArrayList."<init>":(I)V
      26: checkcast     #69                 // class java/util/Collection
      29: astore        4
      31: iconst_0
      32: istore        5
      34: iconst_0
      35: istore        6
      37: aload_3
      38: arraylength
      39: istore        7
      41: iload         6
      43: iload         7
      45: if_icmpge     321
      48: aload_3
      49: iload         6
      51: aaload
      52: astore        8
      54: aload         4
      56: aload         8
      58: checkcast     #60                 // class "[I"
      61: astore        9
      63: astore        24
      65: iconst_0
      66: istore        10
      68: aload_0
      69: aload         9
      71: invokestatic  #75                 // Method kotlin/collections/ArraysKt.zip:([I[I)Ljava/util/List;
      74: checkcast     #77                 // class java/lang/Iterable
      77: astore        11
      79: nop
      80: iconst_0
      81: istore        12
      83: aload         11
      85: astore        13
      87: new           #65                 // class java/util/ArrayList
      90: dup
      91: invokespecial #80                 // Method java/util/ArrayList."<init>":()V
      94: checkcast     #69                 // class java/util/Collection
      97: astore        14
      99: iconst_0
     100: istore        15
     102: aload         13
     104: invokeinterface #84,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     109: astore        16
     111: aload         16
     113: invokeinterface #90,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     118: ifeq          194
     121: aload         16
     123: invokeinterface #94,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     128: astore        17
     130: aload         17
     132: checkcast     #96                 // class kotlin/Pair
     135: astore        18
     137: iconst_0
     138: istore        19
     140: aload         18
     142: invokevirtual #99                 // Method kotlin/Pair.component1:()Ljava/lang/Object;
     145: checkcast     #101                // class java/lang/Number
     148: invokevirtual #104                // Method java/lang/Number.intValue:()I
     151: istore        20
     153: aload         18
     155: invokevirtual #107                // Method kotlin/Pair.component2:()Ljava/lang/Object;
     158: checkcast     #101                // class java/lang/Number
     161: invokevirtual #104                // Method java/lang/Number.intValue:()I
     164: istore        21
     166: iload         20
     168: iload         21
     170: if_icmpeq     177
     173: iconst_1
     174: goto          178
     177: iconst_0
     178: ifeq          111
     181: aload         14
     183: aload         17
     185: invokeinterface #111,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     190: pop
     191: goto          111
     194: aload         14
     196: checkcast     #113                // class java/util/List
     199: nop
     200: checkcast     #77                 // class java/lang/Iterable
     203: astore        11
     205: nop
     206: iconst_0
     207: istore        12
     209: iconst_0
     210: istore        13
     212: aload         11
     214: invokeinterface #84,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     219: astore        14
     221: aload         14
     223: invokeinterface #90,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     228: ifeq          300
     231: aload         14
     233: invokeinterface #94,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     238: astore        15
     240: iload         13
     242: aload         15
     244: checkcast     #96                 // class kotlin/Pair
     247: astore        16
     249: istore        22
     251: iconst_0
     252: istore        17
     254: aload         16
     256: invokevirtual #99                 // Method kotlin/Pair.component1:()Ljava/lang/Object;
     259: checkcast     #101                // class java/lang/Number
     262: invokevirtual #104                // Method java/lang/Number.intValue:()I
     265: istore        18
     267: aload         16
     269: invokevirtual #107                // Method kotlin/Pair.component2:()Ljava/lang/Object;
     272: checkcast     #101                // class java/lang/Number
     275: invokevirtual #104                // Method java/lang/Number.intValue:()I
     278: istore        19
     280: iload         18
     282: iload         19
     284: isub
     285: invokestatic  #119                // Method java/lang/Math.abs:(I)I
     288: istore        23
     290: iload         22
     292: iload         23
     294: iadd
     295: istore        13
     297: goto          221
     300: iload         13
     302: nop
     303: invokestatic  #125                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     306: aload         24
     308: swap
     309: invokeinterface #111,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     314: pop
     315: iinc          6, 1
     318: goto          41
     321: aload         4
     323: checkcast     #113                // class java/util/List
     326: nop
     327: checkcast     #77                 // class java/lang/Iterable
     330: invokestatic  #131                // Method kotlin/collections/CollectionsKt.minOrThrow:(Ljava/lang/Iterable;)Ljava/lang/Comparable;
     333: dup
     334: invokestatic  #135                // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
     337: checkcast     #101                // class java/lang/Number
     340: invokevirtual #104                // Method java/lang/Number.intValue:()I
     343: ireturn

  static {};
    Code:
       0: bipush        8
       2: anewarray     #60                 // class "[I"
       5: astore_0
       6: aload_0
       7: iconst_0
       8: bipush        9
      10: newarray       int
      12: astore_1
      13: aload_1
      14: iconst_0
      15: bipush        8
      17: iastore
      18: aload_1
      19: iconst_1
      20: iconst_1
      21: iastore
      22: aload_1
      23: iconst_2
      24: bipush        6
      26: iastore
      27: aload_1
      28: iconst_3
      29: iconst_3
      30: iastore
      31: aload_1
      32: iconst_4
      33: iconst_5
      34: iastore
      35: aload_1
      36: iconst_5
      37: bipush        7
      39: iastore
      40: aload_1
      41: bipush        6
      43: iconst_4
      44: iastore
      45: aload_1
      46: bipush        7
      48: bipush        9
      50: iastore
      51: aload_1
      52: bipush        8
      54: iconst_2
      55: iastore
      56: aload_1
      57: aastore
      58: aload_0
      59: iconst_1
      60: bipush        9
      62: newarray       int
      64: astore_1
      65: aload_1
      66: iconst_0
      67: iconst_4
      68: iastore
      69: aload_1
      70: iconst_1
      71: iconst_3
      72: iastore
      73: aload_1
      74: iconst_2
      75: bipush        8
      77: iastore
      78: aload_1
      79: iconst_3
      80: bipush        9
      82: iastore
      83: aload_1
      84: iconst_4
      85: iconst_5
      86: iastore
      87: aload_1
      88: iconst_5
      89: iconst_1
      90: iastore
      91: aload_1
      92: bipush        6
      94: iconst_2
      95: iastore
      96: aload_1
      97: bipush        7
      99: bipush        7
     101: iastore
     102: aload_1
     103: bipush        8
     105: bipush        6
     107: iastore
     108: aload_1
     109: aastore
     110: aload_0
     111: iconst_2
     112: bipush        9
     114: newarray       int
     116: astore_1
     117: aload_1
     118: iconst_0
     119: iconst_2
     120: iastore
     121: aload_1
     122: iconst_1
     123: bipush        9
     125: iastore
     126: aload_1
     127: iconst_2
     128: iconst_4
     129: iastore
     130: aload_1
     131: iconst_3
     132: bipush        7
     134: iastore
     135: aload_1
     136: iconst_4
     137: iconst_5
     138: iastore
     139: aload_1
     140: iconst_5
     141: iconst_3
     142: iastore
     143: aload_1
     144: bipush        6
     146: bipush        6
     148: iastore
     149: aload_1
     150: bipush        7
     152: iconst_1
     153: iastore
     154: aload_1
     155: bipush        8
     157: bipush        8
     159: iastore
     160: aload_1
     161: aastore
     162: aload_0
     163: iconst_3
     164: bipush        9
     166: newarray       int
     168: astore_1
     169: aload_1
     170: iconst_0
     171: bipush        6
     173: iastore
     174: aload_1
     175: iconst_1
     176: bipush        7
     178: iastore
     179: aload_1
     180: iconst_2
     181: iconst_2
     182: iastore
     183: aload_1
     184: iconst_3
     185: iconst_1
     186: iastore
     187: aload_1
     188: iconst_4
     189: iconst_5
     190: iastore
     191: aload_1
     192: iconst_5
     193: bipush        9
     195: iastore
     196: aload_1
     197: bipush        6
     199: bipush        8
     201: iastore
     202: aload_1
     203: bipush        7
     205: iconst_3
     206: iastore
     207: aload_1
     208: bipush        8
     210: iconst_4
     211: iastore
     212: aload_1
     213: aastore
     214: aload_0
     215: iconst_4
     216: bipush        9
     218: newarray       int
     220: astore_1
     221: aload_1
     222: iconst_0
     223: bipush        6
     225: iastore
     226: aload_1
     227: iconst_1
     228: iconst_1
     229: iastore
     230: aload_1
     231: iconst_2
     232: bipush        8
     234: iastore
     235: aload_1
     236: iconst_3
     237: bipush        7
     239: iastore
     240: aload_1
     241: iconst_4
     242: iconst_5
     243: iastore
     244: aload_1
     245: iconst_5
     246: iconst_3
     247: iastore
     248: aload_1
     249: bipush        6
     251: iconst_2
     252: iastore
     253: aload_1
     254: bipush        7
     256: bipush        9
     258: iastore
     259: aload_1
     260: bipush        8
     262: iconst_4
     263: iastore
     264: aload_1
     265: aastore
     266: aload_0
     267: iconst_5
     268: bipush        9
     270: newarray       int
     272: astore_1
     273: aload_1
     274: iconst_0
     275: bipush        8
     277: iastore
     278: aload_1
     279: iconst_1
     280: iconst_3
     281: iastore
     282: aload_1
     283: iconst_2
     284: iconst_4
     285: iastore
     286: aload_1
     287: iconst_3
     288: iconst_1
     289: iastore
     290: aload_1
     291: iconst_4
     292: iconst_5
     293: iastore
     294: aload_1
     295: iconst_5
     296: bipush        9
     298: iastore
     299: aload_1
     300: bipush        6
     302: bipush        6
     304: iastore
     305: aload_1
     306: bipush        7
     308: bipush        7
     310: iastore
     311: aload_1
     312: bipush        8
     314: iconst_2
     315: iastore
     316: aload_1
     317: aastore
     318: aload_0
     319: bipush        6
     321: bipush        9
     323: newarray       int
     325: astore_1
     326: aload_1
     327: iconst_0
     328: iconst_4
     329: iastore
     330: aload_1
     331: iconst_1
     332: bipush        9
     334: iastore
     335: aload_1
     336: iconst_2
     337: iconst_2
     338: iastore
     339: aload_1
     340: iconst_3
     341: iconst_3
     342: iastore
     343: aload_1
     344: iconst_4
     345: iconst_5
     346: iastore
     347: aload_1
     348: iconst_5
     349: bipush        7
     351: iastore
     352: aload_1
     353: bipush        6
     355: bipush        8
     357: iastore
     358: aload_1
     359: bipush        7
     361: iconst_1
     362: iastore
     363: aload_1
     364: bipush        8
     366: bipush        6
     368: iastore
     369: aload_1
     370: aastore
     371: aload_0
     372: bipush        7
     374: bipush        9
     376: newarray       int
     378: astore_1
     379: aload_1
     380: iconst_0
     381: iconst_2
     382: iastore
     383: aload_1
     384: iconst_1
     385: bipush        7
     387: iastore
     388: aload_1
     389: iconst_2
     390: bipush        6
     392: iastore
     393: aload_1
     394: iconst_3
     395: bipush        9
     397: iastore
     398: aload_1
     399: iconst_4
     400: iconst_5
     401: iastore
     402: aload_1
     403: iconst_5
     404: iconst_1
     405: iastore
     406: aload_1
     407: bipush        6
     409: iconst_4
     410: iastore
     411: aload_1
     412: bipush        7
     414: iconst_3
     415: iastore
     416: aload_1
     417: bipush        8
     419: bipush        8
     421: iastore
     422: aload_1
     423: aastore
     424: aload_0
     425: putstatic     #11                 // Field magicSquares:[[I
     428: return
}


package com.hackerrank

import java.util.*

fun main(args: Array<String>) {

    val scan = Scanner(System.`in`)
    val n = scan.nextInt()
    val m = scan.nextInt()
    val arr = Array(n, { Array(m, { scan.nextInt() }) })

    println(sizeOfMaxRegion(Board(arr, n, m)))
}

fun sizeOfMaxRegion(board: Board): Int {
    var maxSize = 0
    for (i in 0..board.n - 1) {
        for (j in 0..board.m - 1) {
            val node = Pair(i, j)
            if (!board.wasVisited(node)) {
                val size = regionSize(node, board)
                if (size > maxSize) {
                    maxSize = size
                }
            }
        }
    }
    return maxSize
}

fun regionSize(node: Pair<Int, Int>, board: Board): Int {
    var region = if (board.isNode(node)) 1 else 0
    board.markVisited(node)
    board.neighbours(node)
            .map { regionSize(it, board) }
            .forEach { region += it }
    return region
}

class Board(val arr: Array<Array<Int>>, val n: Int, val m: Int) {
    private var visited = mutableSetOf<Pair<Int, Int>>()

    fun markVisited(node: Pair<Int, Int>) {
        visited.add(node)
    }

    fun neighbours(node: Pair<Int, Int>): List<Pair<Int, Int>> {
        val neighbours = setOf(
                Pair(node.first - 1, node.second - 1),
                Pair(node.first - 1, node.second),
                Pair(node.first - 1, node.second + 1),
                Pair(node.first, node.second - 1),
                Pair(node.first, node.second + 1),
                Pair(node.first + 1, node.second - 1),
                Pair(node.first + 1, node.second),
                Pair(node.first + 1, node.second + 1))

        return neighbours.filter { (first, second) -> valid(first, second) }
    }

    fun isNode(pair: Pair<Int, Int>): Boolean {
        return arr[pair.first][pair.second] == 1 && !wasVisited(pair)
    }

    fun wasVisited(pair: Pair<Int, Int>): Boolean {
        return visited.contains(pair)
    }

    private fun valid(row: Int, col: Int): Boolean {
        return row in 0..n - 1
                && col in 0..m - 1
                && arr[row][col] == 1
                && !wasVisited(Pair(row, col))
    }

}

Compiled from "ConnectedCellsInAGrid.kt"
public final class com.hackerrank.ConnectedCellsInAGridKt {
  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: new           #17                 // class java/util/Scanner
       9: dup
      10: getstatic     #23                 // Field java/lang/System.in:Ljava/io/InputStream;
      13: invokespecial #27                 // Method java/util/Scanner."<init>":(Ljava/io/InputStream;)V
      16: astore_1
      17: aload_1
      18: invokevirtual #31                 // Method java/util/Scanner.nextInt:()I
      21: istore_2
      22: aload_1
      23: invokevirtual #31                 // Method java/util/Scanner.nextInt:()I
      26: istore_3
      27: iconst_0
      28: istore        5
      30: iload_2
      31: anewarray     #33                 // class "[Ljava/lang/Integer;"
      34: astore        6
      36: iload         5
      38: iload_2
      39: if_icmpge     104
      42: iload         5
      44: istore        7
      46: aload         6
      48: iload         7
      50: iconst_0
      51: istore        8
      53: iload_3
      54: anewarray     #35                 // class java/lang/Integer
      57: astore        9
      59: istore        12
      61: astore        11
      63: iload         8
      65: iload_3
      66: if_icmpge     91
      69: iload         8
      71: istore        10
      73: aload         9
      75: iload         10
      77: aload_1
      78: invokevirtual #31                 // Method java/util/Scanner.nextInt:()I
      81: invokestatic  #39                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      84: aastore
      85: iinc          8, 1
      88: goto          63
      91: aload         11
      93: iload         12
      95: aload         9
      97: aastore
      98: iinc          5, 1
     101: goto          36
     104: aload         6
     106: astore        4
     108: new           #41                 // class com/hackerrank/Board
     111: dup
     112: aload         4
     114: iload_2
     115: iload_3
     116: invokespecial #44                 // Method com/hackerrank/Board."<init>":([[Ljava/lang/Integer;II)V
     119: invokestatic  #48                 // Method sizeOfMaxRegion:(Lcom/hackerrank/Board;)I
     122: istore        5
     124: getstatic     #52                 // Field java/lang/System.out:Ljava/io/PrintStream;
     127: iload         5
     129: invokevirtual #58                 // Method java/io/PrintStream.println:(I)V
     132: return

  public static final int sizeOfMaxRegion(com.hackerrank.Board);
    Code:
       0: aload_0
       1: ldc           #70                 // String board
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: iconst_0
       7: istore_1
       8: iconst_0
       9: istore_2
      10: aload_0
      11: invokevirtual #73                 // Method com/hackerrank/Board.getN:()I
      14: iconst_1
      15: isub
      16: istore_3
      17: iload_2
      18: iload_3
      19: if_icmpgt     108
      22: iconst_0
      23: istore        4
      25: aload_0
      26: invokevirtual #76                 // Method com/hackerrank/Board.getM:()I
      29: iconst_1
      30: isub
      31: istore        5
      33: iload         4
      35: iload         5
      37: if_icmpgt     97
      40: new           #78                 // class kotlin/Pair
      43: dup
      44: iload_2
      45: invokestatic  #39                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      48: iload         4
      50: invokestatic  #39                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      53: invokespecial #81                 // Method kotlin/Pair."<init>":(Ljava/lang/Object;Ljava/lang/Object;)V
      56: astore        6
      58: aload_0
      59: aload         6
      61: invokevirtual #85                 // Method com/hackerrank/Board.wasVisited:(Lkotlin/Pair;)Z
      64: ifne          84
      67: aload         6
      69: aload_0
      70: invokestatic  #89                 // Method regionSize:(Lkotlin/Pair;Lcom/hackerrank/Board;)I
      73: istore        7
      75: iload         7
      77: iload_1
      78: if_icmple     84
      81: iload         7
      83: istore_1
      84: iload         4
      86: iload         5
      88: if_icmpeq     97
      91: iinc          4, 1
      94: goto          40
      97: iload_2
      98: iload_3
      99: if_icmpeq     108
     102: iinc          2, 1
     105: goto          22
     108: iload_1
     109: ireturn

  public static final int regionSize(kotlin.Pair<java.lang.Integer, java.lang.Integer>, com.hackerrank.Board);
    Code:
       0: aload_0
       1: ldc           #98                 // String node
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_1
       7: ldc           #70                 // String board
       9: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      12: iconst_0
      13: istore_2
      14: aload_1
      15: aload_0
      16: invokevirtual #101                // Method com/hackerrank/Board.isNode:(Lkotlin/Pair;)Z
      19: ifeq          26
      22: iconst_1
      23: goto          27
      26: iconst_0
      27: istore_2
      28: aload_1
      29: aload_0
      30: invokevirtual #105                // Method com/hackerrank/Board.markVisited:(Lkotlin/Pair;)V
      33: aload_1
      34: aload_0
      35: invokevirtual #109                // Method com/hackerrank/Board.neighbours:(Lkotlin/Pair;)Ljava/util/List;
      38: checkcast     #111                // class java/lang/Iterable
      41: astore_3
      42: nop
      43: iconst_0
      44: istore        4
      46: aload_3
      47: astore        5
      49: new           #113                // class java/util/ArrayList
      52: dup
      53: aload_3
      54: bipush        10
      56: invokestatic  #119                // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
      59: invokespecial #121                // Method java/util/ArrayList."<init>":(I)V
      62: checkcast     #123                // class java/util/Collection
      65: astore        6
      67: iconst_0
      68: istore        7
      70: aload         5
      72: invokeinterface #127,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      77: astore        8
      79: aload         8
      81: invokeinterface #133,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
      86: ifeq          133
      89: aload         8
      91: invokeinterface #137,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      96: astore        9
      98: aload         6
     100: aload         9
     102: checkcast     #78                 // class kotlin/Pair
     105: astore        10
     107: astore        12
     109: iconst_0
     110: istore        11
     112: aload         10
     114: aload_1
     115: invokestatic  #89                 // Method regionSize:(Lkotlin/Pair;Lcom/hackerrank/Board;)I
     118: invokestatic  #39                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     121: aload         12
     123: swap
     124: invokeinterface #141,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     129: pop
     130: goto          79
     133: aload         6
     135: checkcast     #143                // class java/util/List
     138: nop
     139: checkcast     #111                // class java/lang/Iterable
     142: astore_3
     143: nop
     144: iconst_0
     145: istore        4
     147: aload_3
     148: invokeinterface #127,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     153: astore        5
     155: aload         5
     157: invokeinterface #133,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
     162: ifeq          196
     165: aload         5
     167: invokeinterface #137,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     172: astore        6
     174: aload         6
     176: checkcast     #145                // class java/lang/Number
     179: invokevirtual #148                // Method java/lang/Number.intValue:()I
     182: istore        7
     184: iconst_0
     185: istore        8
     187: iload_2
     188: iload         7
     190: iadd
     191: istore_2
     192: nop
     193: goto          155
     196: nop
     197: iload_2
     198: ireturn
}