Using Mask and Bitwise operations.
```\n    fun earliestAndLatest(n: Int, firstPlayer: Int, secondPlayer: Int): IntArray {\n        return helper(1.shl(n + 1) - 1, 1, n, firstPlayer, secondPlayer
```\n    fun earliestAndLatest(n: Int, firstPlayer: Int, secondPlayer: Int): IntArray {\n        return helper(1.shl(n + 1) - 1, 1, n, firstPlayer, secondPlayer, 1, n)\n    }\n    \n    val db = HashMap<String, IntArray>()\n    \n    fun helper(mask: Int, s: Int, e: Int, f: Int, fs: Int, round: Int, n :Int): IntArray {\n        if(mask == 1) return intArrayOf(-1, -1) \n        if(mask.and(1.shl(f)) == 0 || mask.and(1.shl(fs)) == 0) return intArrayOf(-1, -1) \n\t\t\n\t\t// round completed\n        if(s >= e) return helper(mask, 1, n, f, fs, round + 1, n)\n\t\t\n\t\t// got the combination we need so return\n        if(s == f && e == fs) {\n            if(mask.and(1.shl(s)) == 0 || mask.and(1.shl(e)) == 0) return intArrayOf(-1, -1) \n            return intArrayOf(round, round)\n        }\n        \n\t\t// if position at s index is already consumed, the look for next one\n        if(mask.and(1.shl(s)) == 0) return helper(mask, s + 1, e, f, fs, round, n)\n\t\t\n\t\t// if position at e index is already consumed, the look for next one\n        if(mask.and(1.shl(e)) == 0) return helper(mask, s, e - 1, f, fs, round, n)\n        \n\t\t// create a key from current state \n        val key = "" + mask + "-" + s + "-" + e\n        if(db[key] != null) return db[key]!!\n\t\t\n        var min = Int.MAX_VALUE\n        var max = Int.MIN_VALUE\n        var value = helper(mask.xor(1.shl(s)), s + 1, e - 1, f, fs, round, n)\n        if(value[0] != -1) {\n          min = Math.min(min, value[0])\n          max = Math.max(max, value[1])\n        }\n        value = helper(mask.xor(1.shl(e)), s + 1, e - 1, f, fs, round, n)\n        if(value[0] != -1) {\n            min = Math.min(min, value[0])\n            max = Math.max(max, value[1])\n        }\n        db[key] = intArrayOf(min, max)\n        return intArrayOf(min, max)\n    }
[]