""" 0001.1 - Solution 1 - Brute Force Approach """
#####################################################################################
# Imports
#####################################################################################
from typing import List
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def twoSum(self, nums: List[int], target: int) -> List[int]:
"""Two Sum Function"""
for i in range(len(nums)):
for j in range(i + 1, len(nums)):
if nums[i] + nums[j] == target:
return [i, j]
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().twoSum([3, 3], 6))
print(Solution().twoSum([3, 2, 4], 6))
print(Solution().twoSum([2, 7, 11, 15], 9))
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()
""" 0001.1 - Solution 1 - Using Enumerate """
#####################################################################################
# Imports
#####################################################################################
from typing import List
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def twoSum(self, nums: List[int], target: int) -> List[int]:
"""Two Sum Function"""
for key1, num1 in enumerate(nums):
for key2, num2 in enumerate(nums[key1 + 1 :], key1 + 1):
# enumerate(iterable, start=1) -> start value determines starting index
# Printing value and key will output 1 i , 2 j, 3 k, 4 l
if num1 + num2 == target:
return [key1, key2]
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().twoSum([3, 3], 6))
print(Solution().twoSum([3, 2, 4], 6))
print(Solution().twoSum([2, 7, 11, 15], 9))
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()
""" 1502.1 - Can Make Arithmetic Progression From Sequence - Solution 1 - Sorting """
#####################################################################################
# Imports
#####################################################################################
from typing import List
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def canMakeArithmeticProgression(self, arr: List[int]) -> bool:
"""Can Make Arithmetic Progression Function"""
arr.sort()
diff = arr[1] - arr[0]
for i in range(2, len(arr)):
if arr[i] - arr[i - 1] != diff:
return False
return True
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().canMakeArithmeticProgression([3, 5, 1])) # True
print(Solution().canMakeArithmeticProgression([1, 2, 4])) # False
print(Solution().canMakeArithmeticProgression([1, 1, 1])) # True
print(Solution().canMakeArithmeticProgression([5, 1, 9, 3, 7])) # True
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()
""" 1502.2 - Can Make Arithmetic Progression From Sequence - Solution 2 - O(n) Set """
#####################################################################################
# Imports
#####################################################################################
from typing import List
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def canMakeArithmeticProgression(self, arr: List[int]) -> bool:
"""Can Make Arithmetic Progression Function"""
n = len(arr)
mn, mx = min(arr), max(arr)
diff = mx - mn
# All elements are the same
if diff == 0:
return True
# The range must be evenly divisible by n - 1
if diff % (n - 1) != 0:
return False
step = diff // (n - 1)
seen = set()
for a in arr:
if (a - mn) % step != 0:
return False
if a in seen:
return False
seen.add(a)
return True
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().canMakeArithmeticProgression([3, 5, 1])) # True
print(Solution().canMakeArithmeticProgression([1, 2, 4])) # False
print(Solution().canMakeArithmeticProgression([1, 1, 1])) # True
print(Solution().canMakeArithmeticProgression([5, 1, 9, 3, 7])) # True
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()