All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0201_0300.s0260_single_number_iii.Solution.kt Maven / Gradle / Ivy

There is a newer version: 1.30
Show newest version
package g0201_0300.s0260_single_number_iii

// #Medium #Array #Bit_Manipulation #2022_11_01_Time_222_ms_(100.00%)_Space_36.3_MB_(100.00%)

class Solution {
    fun singleNumber(nums: IntArray): IntArray {
        var xorSum = 0
        for (num in nums) {
            // will give xor of required nos
            xorSum = xorSum xor num
        }
        // find rightmost bit which is set
        val rightBit = xorSum and -xorSum
        var a = 0
        for (num in nums) {
            // xor only those number whose rightmost bit is set
            if (num and rightBit != 0) {
                a = a xor num
            }
        }
        return intArrayOf(a, a xor xorSum)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy