Leetcode Product Self

I am trying to convert this java code to swift in an attempt to answer this leetcode. Loading...
The output shows index out of bound; how do I resolve this ?

func productExceptSelf(_ nums: [Int]) -> [Int] {
   var lArray = Array(repeating: 1, count: nums.count)
   var rArray = Array(repeating: 1, count: nums.count)
  
    var ansArray = [Int]()
    
    for i in 1..<lArray.count{
        lArray[i] = lArray[i-1] * nums[i-1]
    }
   
    for i in 1..<rArray.reversed().count{
        rArray[i] = rArray[i+1] * nums[i+1]
    }
    
    for i in 0..<ansArray.count {
        ansArray[i] = lArray[i] * rArray[i]
    }
    return ansArray
}

productExceptSelf([1,2,3,4])

Screen Shot 2021-12-15 at 8.49.43 PM

for i in 1..<rArray.reversed().count{
rArray[i] = rArray[i+1] * nums[i+1]
}

This will cause the bug. when i is count-1, you will access rArray[count] and nums[count]

for i in 1..<rArray.reversed().count{
        rArray[i] = rArray[i+1] * nums[i+1]
    }

should be

    for i in (0...(nums.count - 2)).reversed() {
        rArray[i] = rArray[i+1] * nums[i+1]
    }

And I think you should also either initialize ansArray or use ansArray.append() to set values.

You don't need to write imperatively; somebody did that for you already in the Algorithms package.

func productExceptSelf(_ nums: [Int]) -> [Int] {
  zip(
    nums.reductions(1, *),
    nums.dropFirst().reversed().reductions(1, *).reversed()
  ).map(*)
}

Hey Jessy,
Thank you for the solution but during an interviews once I had, I was told to do this imperatively with minimal use of functions

Hello, I ran it and it shows an empty array

func productExceptSelf(_ nums: [Int]) -> [Int] {
var lArray = Array(repeating: 1, count: nums.count)
var rArray = Array(repeating: 1, count: nums.count)

var ansArray = [Int]()

for i in 1..<lArray.count{
    lArray[i] = lArray[i-1] * nums[i-1]
}

for i in (0...(nums.count - 2)).reversed() {
        rArray[i] = rArray[i+1] * nums[i+1]
    }


for i in 0..<ansArray.count {
    ansArray.append(lArray[i] * rArray[i])
    print(ansArray)
}
return ansArray

}

productExceptSelf([1,2,3,4])

Here is full code (two versions: commented/uncommented)

func productExceptSelf(_ nums: [Int]) -> [Int] {
    let n = nums.count
    var lArray = Array(repeating: 1, count: n)
    var rArray = Array(repeating: 1, count: n)
    //var ansArray = Array(repeating: 0, count: n)
    var ansArray = [Int]()
    
    for i in 1..<n {
        lArray[i] = lArray[i-1] * nums[i-1]
    }
   
    for i in (0...(n - 2)).reversed() {
        rArray[i] = rArray[i+1] * nums[i+1]
    }
    
    for i in 0..<n {
        //ansArray[i] = lArray[i] * rArray[i]
        ansArray.append(lArray[i] * rArray[i])
    }
    return ansArray
}

productExceptSelf([1,2,3,4])
productExceptSelf([-1,1,0,-3,3])
1 Like

I see, since the arrays weren't pointing to the input N, it lead to an index out of range

  1. Why are you using reversed() then?
  2. Don't take a job at a place like that.
2 Likes

Lol I don't why. It makes no sense

Can you please explain why -2 is used in this part of the code
for i in (0...(n - 2)).reversed()

I found it in the java code you posted.

Left array first item (larray[0]) is initialized with 1, right array last item (rarray[n - 1]) is initialized with 1 then left array is filled from second item to last item 1..<n and right array is filed from last but one to first item (0...(n - 2)).reversed(). And each step in loops use the result of the previous one.

1 Like

Another way to do the same thing.

func productExceptSelf(_ nums: [Int]) -> [Int] {
    let n = nums.count
    var lArray = [1]
    var rArray = [1]
    var ansArray = [Int]()
    
    for i in 1..<n {
        lArray.append(lArray[i - 1] * nums[i - 1])
        rArray.append(rArray[i - 1] * nums[n - i])
    }
    rArray = rArray.reversed()
     
    for i in 0..<n {
        ansArray.append(lArray[i] * rArray[i])
    }
    return ansArray
}

productExceptSelf([1, 2, 3, 4])
// Should be [24, 12, 8, 6]
productExceptSelf([-1, 1, 0, -3, 3])
// Should be [0, 0, 9, 0, 0]

Thanks bro!!!

Terms of Service

Privacy Policy

Cookie Policy