From 0dac6362ac34f70ca65b555a23310fdc7bb2e1d5 Mon Sep 17 00:00:00 2001 From: Paridhi Malviya Date: Sun, 11 Jan 2026 19:07:42 -0600 Subject: [PATCH] Find disappeared numbers. Game of life. Find min and max in array by reducing comparisons --- DisappearedNumbers.swift | 63 ++++++++++++++++++++++++++++++ GameOfLife.swift | 83 ++++++++++++++++++++++++++++++++++++++++ MinAndMaxInArray.swift | 52 +++++++++++++++++++++++++ 3 files changed, 198 insertions(+) create mode 100644 DisappearedNumbers.swift create mode 100644 GameOfLife.swift create mode 100644 MinAndMaxInArray.swift diff --git a/DisappearedNumbers.swift b/DisappearedNumbers.swift new file mode 100644 index 00000000..4676cba9 --- /dev/null +++ b/DisappearedNumbers.swift @@ -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.. 0) { + input[idx] *= -1 + } + } + + //find out + for i in 0.. 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 + } +} + diff --git a/GameOfLife.swift b/GameOfLife.swift new file mode 100644 index 00000000..a09f7977 --- /dev/null +++ b/GameOfLife.swift @@ -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.. 3) { + matrix[i][j] = 2 //live cell dies + } + } + } + } + + //replace back 2 -> 0 and 3 -> 1 + for i in 0.. 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 + } +} diff --git a/MinAndMaxInArray.swift b/MinAndMaxInArray.swift new file mode 100644 index 00000000..be2f9f0d --- /dev/null +++ b/MinAndMaxInArray.swift @@ -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.. 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)") + } +}