arcbjorn

thoughtbook

RSS Feed

Saturday, March 4, 2023

Longest nice subarray

Leetcode 2401 problem.

Go

  • Time complexity: O(n)O(n) - n is a length of number
  • Auxiliary space: O(1)O(1) - constant amount of space
func longestNiceSubarray(nums []int) int {
    maxLength := 0
    sum := 0
    startSubArrayIndex := 0

    for index := range nums {
        for sum&nums[index] != 0 {
            sum ^= nums[startSubArrayIndex]
            startSubArrayIndex++
        }
    
        sum ^= nums[index]
        maxLength = max(maxLength, index - startSubArrayIndex + 1)
    }

    return maxLength
}


func max(num1 int, num2 int) int {
    if num1 > num2 {
        return num1
    }
    return num2
}
Creative Commons Licence