add some array playing

This commit is contained in:
Marina S 2023-07-29 11:08:44 -07:00 committed by GitHub
parent 5732cfcc61
commit 5c55b81641
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 263 additions and 0 deletions
arrays_and_strings

View file

@ -0,0 +1,27 @@
"""
Given an array arr of integers, check if there exist two indices i and j such that :
i != j
0 <= i, j < arr.length
arr[i] == 2 * arr[j]
"""
def check_if_exist(arr: list[int]) -> bool:
aux_dict = {}
for i, num in enumerate(arr):
aux_dict[2*num] = i
for j, num in enumerate(arr):
if num in aux_dict.keys() and j != aux_dict[num]:
return (j, aux_dict[num])
return False
if __name__ == "__main__":
arr = [-2, 0, 10, -19, 4, 6, -8]
print(check_if_exist(arr))