Created
September 11, 2024 16:59
-
-
Save omar115/04af0433f97b80cd810db2f07cf9c48d to your computer and use it in GitHub Desktop.
Live Coding Test Solution of Airwrk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Problem - 1 | |
# solution of problem - 1 | |
# define a fucntion | |
def subarray_equals_k(nums, k): | |
count = 0 | |
c_s = 0 | |
x = {0: 1} # dictionary | |
for num in nums: | |
c_s += num | |
if ( c_s - k ) in x: | |
count += x[c_s - k] | |
if c_s in x: | |
x[c_s] += 1 | |
else: | |
x[c_s] = 1 | |
return count | |
nums = [1,2,3] | |
target = 3 | |
solution = subarray_equals_k(nums, target) | |
print(solution) | |
// problem - 2 | |
# solution-2 | |
s = 'abcabcbb' | |
def find_longest_substring(s): | |
char_x = set() | |
left = 0 | |
max_length = 0 | |
# for right in range(len(s)): | |
# while s[right] in char_x: | |
# # need to remove the s[left] and +1 left | |
# char_x.remove(s[left]) | |
# left += 1 | |
# char_x.add(s[right]) # abcabcbb | |
right = len(s) - 1 | |
while left < right: | |
if s[left] == s[right]: | |
char_x.add(s[left]) | |
left += 1 | |
right -= 1 | |
print(char_x) | |
print(find_longest_substring(s)) | |
// problem-3 | |
# function to find the missing number | |
def find_missing_nums(nums): | |
# logic is | |
# len(nums) + 1 and multipy with len(nums) then +1 | |
n = len(nums) | |
value = n * ( n + 1 ) // 2 | |
final = sum(nums) | |
print( final) | |
print(value - final) | |
nums = [9,6,4,2,3,5,7,0,1] | |
print(find_missing_nums(nums)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment