From ec0b3b397aebc023f971c81afc9ebd1212786f11 Mon Sep 17 00:00:00 2001 From: Paridhi Malviya Date: Sat, 10 Jan 2026 23:17:24 -0600 Subject: [PATCH 1/2] Diagonal traversal. Print matricx in spiral order. Product of array except self --- DiagonalTraversal.swift | 118 ++++++++++++++++++++++++++++ ProductOfArrayExceptSelf.swift | 70 +++++++++++++++++ SpiralMatrix.swift | 136 +++++++++++++++++++++++++++++++++ 3 files changed, 324 insertions(+) create mode 100644 DiagonalTraversal.swift create mode 100644 ProductOfArrayExceptSelf.swift create mode 100644 SpiralMatrix.swift diff --git a/DiagonalTraversal.swift b/DiagonalTraversal.swift new file mode 100644 index 00000000..8591b528 --- /dev/null +++ b/DiagonalTraversal.swift @@ -0,0 +1,118 @@ +// +// Untitled.swift +// DSA-Practice +// +// Created by Paridhi Malviya on 1/6/26. +// + +class DiagonalTraversal { + + + init() { + let input: [[Int]] = [[1,2,3], [4,5,6], [7,8,9]] + let output = diagonalTraversalFinal(input) + print("output \(output)") + } + + func diagonalTraversalFinal(_ nums: [[Int]]) -> [Int] { + let m = nums.count + let n = nums[0].count + var result = Array(repeating: 0, count: nums.count * nums[0].count) + + var r = 0 + var c = 0 + var flag = true //upward direction + for i in 0.. [Int] { + var start = [0,0] + var i = 0 + var j = 0 + print("last elemt ***", nums[nums.count - 1][nums[0].count - 1]) + while (nums[i][j] != nums[nums.count - 1][nums[0].count - 1]) { + + //moving upward + while (i >= 0 && j < nums[0].count - 1) { + print(nums[i][j]) + i -= 1 + j += 1 + } + + //move downward. reached top boundary + if (i < 0) { + i = 0 + } + if ( j > nums[0].count - 1) { + i += 1 + j = nums[0].count - 1 + } + //moving downward + while( i <= nums.count - 1 && j >= 0) { + print(nums[i][j]) + i += 1 + j -= 1 + } + + if (i > nums.count - 1) { + i = nums.count - 1 + } + if (j < 0) { + j = 0 + } + // //moving upward + // while (j <= nums[0].count - 1 || i > 0) { + // print(nums[i][j]) + // i -= 1 + // j += 1 + // } + // + // if (i < 0) { + // i = 0 + // } + // if (j > nums[0].count - 1) { + // j = nums[0].count - 1 + // i += 1 + // } + // //move downward + } + + return [] + } + + +} diff --git a/ProductOfArrayExceptSelf.swift b/ProductOfArrayExceptSelf.swift new file mode 100644 index 00000000..8c05e602 --- /dev/null +++ b/ProductOfArrayExceptSelf.swift @@ -0,0 +1,70 @@ +// +// ProductOfArrayExceptSelf.swift +// DSA-Practice +// +// Created by Paridhi Malviya on 1/5/26. +// + +class productOfArrayExceptSelf { + + init() { + let productExceptSelfAtEachIndex = productExceptSelf(nums: [1, 2, 3, 4, 5]) + print("product ** \(productExceptSelfAtEachIndex)") + + let productExceptSelf = productExceptSelfBySavingSpace(nums: [1, 2, 3, 4, 5]) + print("product except self \(productExceptSelf)") + } + + /* + time compelxity - O(n) + space complexity - O(3n) = O(n) + */ + func productExceptSelf(nums: [Int]) -> [Int] { + //forward pass left to right + var leftToRightProduct = Array(repeating: 0, count: nums.count) + leftToRightProduct[0] = 1 + for index in 1.. [Int] { + var rp = 1 + var result = Array(repeating: 0, count: nums.count) + result[0] = 1 // + //forward pass left to right + for index in 1.. increment top + go on the right most boundary from top to bottom + shrink the boundary -> right = right - 1 + go on the bottom row from right to left + shrink bottom - bottom - 1 + go on left from bottom to top. + once left column is done, increase the left boundary. left = left + 1 + If a single row at the innermost layer. then top and bottom is equal. left and right are different. + If any pair crossed, we would stop. If they are equal, we need to print it. + */ + /* + time complexity - O(mn) + space complexity - no extra space + */ + func spiralOrder() -> [Int] { + let input = [[1,2,3,4,5], [6,7,8,9,10], [11, 12, 13, 14, 15], [16, 17, 18, 19,20]] + var result = [Int]() + let m = input.count + let n = input[0].count + var top = 0 + var bottom = m - 1 + var left = 0 + var right = n - 1 + //when both pairs didn't cross each other. + while (top <= bottom && left <= right) { + //iterate on top row. for j = left to right + for j in left...right { + result.append(input[top][j]) + } + //once top row is done. Ijncrease the top pointer + top += 1 + + //If the variables of while loop is mutating (if base is changing), check the base case again + if (top <= bottom && left <= right) { + //right column + for i in top...bottom { + result.append(input[i][right]) + } + right -= 1 + } + + if (top <= bottom) { + //bottom row + for j in stride(from: right, through: left, by: -1) { + result.append(input[bottom][j]) + } + bottom -= 1 + } + + if (left <= right) { + //left column + for i in stride(from: bottom, through: top, by: -1) { + result.append(input[i][left]) + } + left += 1 + } + } + return result + } + + //Recursion stack space - no of spirals + func spiralMatrixUsingRecursion() { + let input = [[1,2,3,4,5], [6,7,8,9,10], [11, 12, 13, 14, 15], [16, 17, 18, 19,20]] + var top = 0 + var bottom = input.count - 1 + var left = 0 + var right = input[0].count - 1 + var result = [Int]() + helperForSpiralOrder(input: input, left: left, right: right, top: top, bottom: bottom, result: &result) + print("result : \(result)") + } + + func helperForSpiralOrder(input: [[Int]], left: Int, right: Int, top: Int, bottom: Int, result: inout [Int]) { + var top = top + var bottom = bottom + var left = left + var right = right + + if (top > bottom || left > right) { + return + } + //iterate on top row. for j = left to right + for j in left...right { + result.append(input[top][j]) + } + //once top row is done. Increase the top pointer + top += 1 + + //right column + for i in top...bottom { + result.append(input[i][right]) + } + right -= 1 + + //If the variables of while loop is mutating (if base case variable is changing), check the base case again + + if (top <= bottom) { + //bottom row + for j in stride(from: right, through: left, by: -1) { + result.append(input[bottom][j]) + } + bottom -= 1 + } + + if (left <= right) { + //left column + for i in stride(from: bottom, through: top, by: -1) { + result.append(input[i][left]) + } + left += 1 + } + + helperForSpiralOrder(input: input, left: left, right: right, top: top, bottom: bottom, result: &result) + } +} From 64a0cc7bdb7b7a601e5b17c62dbe69e6213f3357 Mon Sep 17 00:00:00 2001 From: Paridhi Malviya Date: Sat, 10 Jan 2026 23:19:04 -0600 Subject: [PATCH 2/2] Clean up --- DiagonalTraversal.swift | 61 ++--------------------------------------- 1 file changed, 2 insertions(+), 59 deletions(-) diff --git a/DiagonalTraversal.swift b/DiagonalTraversal.swift index 8591b528..2e68d9a1 100644 --- a/DiagonalTraversal.swift +++ b/DiagonalTraversal.swift @@ -10,11 +10,11 @@ class DiagonalTraversal { init() { let input: [[Int]] = [[1,2,3], [4,5,6], [7,8,9]] - let output = diagonalTraversalFinal(input) + let output = diagonalTraversal(input) print("output \(output)") } - func diagonalTraversalFinal(_ nums: [[Int]]) -> [Int] { + func diagonalTraversal(_ nums: [[Int]]) -> [Int] { let m = nums.count let n = nums[0].count var result = Array(repeating: 0, count: nums.count * nums[0].count) @@ -58,61 +58,4 @@ class DiagonalTraversal { return result } - - func diagonalTraversal(_ nums: [[Int]]) -> [Int] { - var start = [0,0] - var i = 0 - var j = 0 - print("last elemt ***", nums[nums.count - 1][nums[0].count - 1]) - while (nums[i][j] != nums[nums.count - 1][nums[0].count - 1]) { - - //moving upward - while (i >= 0 && j < nums[0].count - 1) { - print(nums[i][j]) - i -= 1 - j += 1 - } - - //move downward. reached top boundary - if (i < 0) { - i = 0 - } - if ( j > nums[0].count - 1) { - i += 1 - j = nums[0].count - 1 - } - //moving downward - while( i <= nums.count - 1 && j >= 0) { - print(nums[i][j]) - i += 1 - j -= 1 - } - - if (i > nums.count - 1) { - i = nums.count - 1 - } - if (j < 0) { - j = 0 - } - // //moving upward - // while (j <= nums[0].count - 1 || i > 0) { - // print(nums[i][j]) - // i -= 1 - // j += 1 - // } - // - // if (i < 0) { - // i = 0 - // } - // if (j > nums[0].count - 1) { - // j = nums[0].count - 1 - // i += 1 - // } - // //move downward - } - - return [] - } - - }