Skip to content

Commit

Permalink
day 4 completed
Browse files Browse the repository at this point in the history
  • Loading branch information
SamirPaulb committed May 10, 2022
1 parent cd485ad commit 7b14882
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 3 deletions.
3 changes: 0 additions & 3 deletions 30-Days-SDE-Sheet-Practice/04. Day 4 Arrays Part-IV/01.py

This file was deleted.

27 changes: 27 additions & 0 deletions 30-Days-SDE-Sheet-Practice/04. Day 4 Arrays Part-IV/4Sum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# https://leetcode.com/problems/4sum/

class Solution:
def fourSum(self, nums: List[int], target: int) -> List[List[int]]:
nums.sort()
res = []
n = len(nums)
for i in range(n-3):
if i > 0 and nums[i] == nums[i-1]: continue
for j in range(i+1, n-2):
if j > i+1 and nums[j] == nums[j-1]: continue
l = j+1; r = n-1
while l < r:
fourSum = nums[i] + nums[j] + nums[l] + nums[r]
if fourSum < target:
l += 1
elif fourSum > target:
r -= 1
else:
res.append([nums[i], nums[j], nums[l], nums[r]])
l += 1
while l < r and nums[l] == nums[l-1]:
l += 1

return res


Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# https://www.interviewbit.com/problems/subarray-with-given-xor/
# https://youtu.be/lO9R5CaGRPY
'''
Use the concept of Largest subarray with 0 sum.
Keep a dictionary to store the current_perfix_xor(cpx). Suppose the current subarray can be devided
into 2 parts y and k.
<----cpx---->
[4, 2, 2, 6, 4]
<---y--> <-k->
y ^ k = cpx
take ^ on both side(as k^k = 0)
y = cpx ^ k
if we got y previously in dictionary then increase the res by that count.
'''
class Solution:
def solve(self, arr, k):
dic = {}
cpx = 0
res = 0

for i in arr:
cpx = cpx ^ i

y = cpx ^ k
if y in dic: res += dic[y]
if cpx == k: res += 1

if cpx in dic: dic[cpx] += 1
else: dic[cpx] = 1

return res

# Time: O(N)
# Space: O(N)


'''
# Brute Force:
# The brute force solution is to generate all possible subarrays.
# For each generated subarray we get the respective XOR and then check if this XOR is equal to B.
class Solution:
def solve(self, arr, k):
res = 0
for i in range(len(arr)):
xor = 0
for j in range(i, len(arr)):
xor = xor ^ arr[j]
if xor == k: res += 1
return res
# Time Limit Exceeded
# Time: O(N^2)
# Space: O(1)
'''


Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# https://practice.geeksforgeeks.org/problems/largest-subarray-with-0-sum/1#
'''
Use a dictionary to store current sum if we get same sum later then it must be zero in that range.
'''

class Solution:
def maxLen(self, n, arr):
dic = {0:-1}
res = 0
s = 0

for i in range(n):
s += arr[i]
if s in dic:
res = max(res, i - dic[s])
else:
dic[s] = i

return res

# Time: O(n)
# Space: O(n)
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# https://leetcode.com/problems/longest-consecutive-sequence/

'''
As we have to find the max consecutive length (ie. sorted array) so start from the smallest value (ie. the
element where num-1 not present) then keep counting greater consecutive elements.
Instead of using 2 loops we basically traversed each element only once. So it is Linear Time.
'''
class Solution:
def longestConsecutive(self, nums: List[int]) -> int:
track = set(nums)
res = 0

for num in nums:
if (num - 1) not in track:
tmpRes = 1
while (num + 1) in track:
tmpRes += 1
num += 1
res = max(res, tmpRes)

return res

# Time: O(N)
# Space: O(N)
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# https://leetcode.com/problems/longest-substring-without-repeating-characters/
# https://youtu.be/qtVh-XEpsJo

class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
track = set()
l = 0
res = 0

for r in range(len(s)):
while s[r] in track:
track.remove(s[l])
l += 1
res = max(res, r-l+1)
track.add(s[r])

return res

# Time Complexity: O(N)
# Space Complexity: O(len(set(s))) # number of unique charecters

16 changes: 16 additions & 0 deletions 30-Days-SDE-Sheet-Practice/04. Day 4 Arrays Part-IV/Two Sm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# https://leetcode.com/problems/two-sum/
'''
Keep a track of traversed element in a dictionary. If we get the target - nums[i] then we got the 2 indeces
'''

class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
dic = {}

for i in range(len(nums)):
required = target - nums[i]

if required in dic:
return [dic[required], i]

dic[nums[i]] = i

0 comments on commit 7b14882

Please sign in to comment.