From 26975ceb075b1d2a9956c61d89046f32f1862297 Mon Sep 17 00:00:00 2001 From: Dharma Teja Bandaru Date: Sun, 4 Jan 2026 18:47:40 -0600 Subject: [PATCH] Dharma Array-2 submission --- find_all_disappeared.py | 63 +++++++++++++++++++++++++++++++++++++++++ game_of_life.py | 30 ++++++++++++++++++++ n-2_comparisons.py | 49 ++++++++++++++++++++++++++++++++ 3 files changed, 142 insertions(+) create mode 100644 find_all_disappeared.py create mode 100644 game_of_life.py create mode 100644 n-2_comparisons.py diff --git a/find_all_disappeared.py b/find_all_disappeared.py new file mode 100644 index 00000000..21cf6210 --- /dev/null +++ b/find_all_disappeared.py @@ -0,0 +1,63 @@ +''' +Docstring for find_all_disappeared +This is pretty straight forward soilution, and it is very optimal as well, the only caveat is, it does run with extra space, that's it. +''' + +class Solution: + def findDisappearedNumbers(self, nums: List[int]) -> List[int]: + + res = [] + for i in range(len(nums)): + + index = abs(nums[i])-1 + + index_num = abs(nums[index])*-1 + + nums[index] = index_num + + + for i in range(len(nums)): + + if nums[i]>0: + res.append(i+1) + return res + +''' +This is tricky, it involves making changes to the given array, and making it multiply with -1 + +At the end, you will figure out the elements which are greater than 0, and that's when you realize those are the elements you want. + +You will want to put this logic on the paper for sure, for clarity. + +If the input also contains negative integers, you solve it using a offset. + + + + +''' + + +class Solution: + def findDisappearedNumbers(self, nums: List[int]) -> List[int]: + + res = [] + for i in range(len(nums)): + + index = abs(nums[i])-1 + + index_num = abs(nums[index])*-1 + + nums[index] = index_num + + + for i in range(len(nums)): + + if nums[i]>0: + res.append(i+1) + return res + + + + + + diff --git a/game_of_life.py b/game_of_life.py new file mode 100644 index 00000000..7cc9bff2 --- /dev/null +++ b/game_of_life.py @@ -0,0 +1,30 @@ +class Solution: + def gameOfLife(self, board: List[List[int]]) -> None: + """ + Do not return anything, modify board in-place instead. + """ + + directions =[[-1,0],[1,0],[0,1],[0,-1],[-1,-1],[1,1],[-1,1],[1,-1]] + + for i in range(len(board)): + for j in range(len(board[0])): + count = 0 + for r,c in directions: + if(i+r in range(len(board)) and j+c in range(len(board[0]))): + if board[i+r][c+j]==2 or board[i+r][c+j]==1: + count+=1 + if(count ==3 and board[i][j]==0): + board[i][j]=3 + + elif( (count<2 or count>3) and board[i][j]==1): + board[i][j]=2 + + for i in range(len(board)): + for j in range(len(board[0])): + + if board[i][j]==2: + board[i][j] = 0 + + elif board[i][j]==3: + board[i][j]=1 + diff --git a/n-2_comparisons.py b/n-2_comparisons.py new file mode 100644 index 00000000..38df3bd6 --- /dev/null +++ b/n-2_comparisons.py @@ -0,0 +1,49 @@ +''' + +Inorder to find out the max and min of the array, we generally go over the array. +While going through the array, lets say array have n elements. +To make min and max comparison at every element, we make 2*n comparisons. + +In the current approach. + +We are taking two elements in one go, that is we. re reducing the number of comparisons +to n/2. + +and at each step we are making 3 comparisons, which is totally equal to 3*n/2=1.5n + +which means we are reducing the total comparison from 2*n to 1.5 n. + +If the length is odd, we are making two extra comparison, which is 1.5n+2 + +Although the time complexity remains at O(n), we are reducing the comparisons to 1.5n+2 + +''' + +## Example :[-12,4,-18,6,25] + +# nums =[-12,4,-18,6,25] + + +nums=[-12, 4, -18, 6, 25, -204] + + + +min_num = float('inf') +max_num = float('-inf') + +for i in range(0, len(nums)-1, 2): + if nums[i]