Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions DisappearedNumbers.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//
// FindDisappearedNumbers.swift
// DSA-Practice
//
// Created by Paridhi Malviya on 1/10/26.
//

/*
Range = 1-8
array = [4,3, 7, 2,8, 2, 3, 1]
prerequisite -> we will be having range which is equal to the length of array.
temporary state change.

hash set under the hood is a boolean array only.
I f I want to record if 4 is present in the array, go to it's corresponding index i.e. 3 and if the number is not negative over there, make it negative. Index 3 being -ve tells that the number 4 is inside the array.
check if 3 is present in tht array, got to it's corresponding index i.e. 2 and make it negative if it's non-negative.
while iterating to check for a value, take absolute value.
in-place marking.
No extra space. space complexity - O(1)
time complexity -> O(n)
It's just a temporary state change.
At 2nd pass, we will check if the number is -ve, then the (index corresoping to the no + 1) is the number present in the array. During 2nd pass, can make the number positive to convert it back to the original array.

range - (upper no - lower no) + 1

Above algorithm can work for -nve numbers presence in array as well.
array - [0, -1, 3, -2, 4, -2, -1, -3]
add offset of 4 to the elements of above array. [4, 3, 7, 2, 8, 2, 3, 1] -> then can solve it and by in-place marking of -ve. can convert it back to original array.
*/

class FindDisappearedNumbers {

init() {
let disapparedNums = findDisappearedNumber()
print("disappeared numbers \(disapparedNums)")
}

//time complexity - O(n), space complexity - O(1)
func findDisappearedNumber() -> [Int] {
var input = [4, 3, 2, 7, 8, 2, 3, 1]
var result = [Int]()
//forward pass. marking the negative
for i in 0..<input.count {
let idx = abs(input[i]) - 1
if (input[idx] > 0) {
input[idx] *= -1
}
}

//find out
for i in 0..<input.count {
if (input[i] > 0) {
//since it's positive, this (index + 1)'s number is not present in the array
result.append(i + 1)
} else {
//convert it back to the original number
input[i] *= 1
}
}
return result
}
}

83 changes: 83 additions & 0 deletions GameOfLife.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
//
// GameOfLife.swift
// DSA-Practice
//
// Created by Paridhi Malviya on 1/11/26.
//

/*
1 -> 0 = 2
0 -> 1 = 3

Use directionss array to iterated over each and every direction in the directions array.
[[0,1], [0, -1], [-1, 0], [1, 0], [-1, -1]....]

any live cell dies -> with fewer than 2 live neighbours
any live cell live -> with 2 or 3 live neighbours
any live cell dies -> with more than 3 live neighbours
any dead cell lives -> exactly 3 live neighbours
*/
class GameOfLife {

init() {
var matrix = [[0, 1, 0], [0, 0, 1], [1, 1, 1], [0, 0, 0]]
let nextState = gameOfLife(matrix: &matrix)
print("next state *** \(nextState)")
}

func gameOfLife(matrix: inout [[Int]]) -> [[Int]] {

//replace 1 -> 0 = 2
//replace 0 -> 1 = 3
//after making the changes on thw hole matrix, replace 2 with 1 and 3 with 0
for i in 0..<matrix.count {
for j in 0..<matrix[0].count {
let liveNeighbours = countLiveCellsAround(matrix: matrix, i: i, j: j)
if (matrix[i][j] == 0) {
if (liveNeighbours == 3) {
//dead cell lives
matrix[i][j] = 3
}
} else if (matrix[i][j] == 1) {
if (liveNeighbours < 2) {
matrix[i][j] = 2 //dies
} else if (liveNeighbours > 3) {
matrix[i][j] = 2 //live cell dies
}
}
}
}

//replace back 2 -> 0 and 3 -> 1
for i in 0..<matrix.count {
for j in 0..<matrix[0].count {
if (matrix[i][j] == 2) {
matrix[i][j] = 0
} else if (matrix[i][j] == 3) {
matrix[i][j] = 1
}
}
}

return matrix
}

func countLiveCellsAround(matrix: [[Int]], i: Int, j: Int) -> Int {
let directionArray = [[1, 1], [1, 0], [1, -1], [0, -1], [-1, -1], [-1, 0], [-1, 1], [0, 1]]
var countOfLiveNeighbours = 0
for dir in directionArray {
if ((i + dir[0] < 0) || i + dir[0] >= matrix.count) {
continue
}
if ((j + dir[1] < 0) || j + dir[1] >= matrix[0].count) {
continue
}

//When we changed from 1 -> 0, we placed 2. So need to check 2 for the original value of 1 to count the live neighbours
if (matrix[i + dir[0]][j + dir[1]] == 1 || matrix[i + dir[0]][j + dir[1]] == 2) {
countOfLiveNeighbours += 1
}
}
return countOfLiveNeighbours
}
}
52 changes: 52 additions & 0 deletions MinAndMaxInArray.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//
// Untitled 2.swift
// DSA-Practice
//
// Created by Paridhi Malviya on 1/6/26.
//

/*
trick - Check if we can group numbers in pair.
*/

class MinAndMaxInArray {
init() {
findMinAndMax(nums: [100, -100, 3, 1, 8, -4, 6, 7, 9])
findMinAndMaxByReducingComparisons(nums: [100, -100, 3, 1, 8, -4, 6, 7, 9])
}

//2n - comparisons
func findMinAndMax(nums: [Int]) {
var minimum = Int.max
var maximum = Int.min

for i in 0..<nums.count {
if (nums[i] < minimum) {
minimum = nums[i]
}
if (nums[i] > maximum) {
maximum = nums[i]
}
}
print("min \(minimum) **** max \(maximum)")
}

//to significantly reducing the no of comparisons from 2n
//Since we are comparing in pairs, the no of comparisons become 3n/2.
func findMinAndMaxByReducingComparisons(nums: [Int]) {
var minimum = Int.max
var maximum = Int.min
//compare in pairs. Maximum comparisons per pair is 3
for i in stride(from: 0, to: nums.count - 1, by: 2) {
if (nums[i] > nums[i + 1]) {
maximum = max(maximum, nums[i])
minimum = min(minimum, nums[i + 1])
} else {
maximum = max(maximum, nums[i + 1])
minimum = min(minimum, nums[i])
}
}
print("by reducing the comparisons maximum \(maximum)")
print("by reducing the comparisons minimum \(minimum)")
}
}