""" 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()
""" 1913.1 - Maximum Product Difference Between Two Pairs - Solution 1 - Sorting """
#####################################################################################
# Imports
#####################################################################################
from typing import List
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def maxProductDifference(self, nums: List[int]) -> int:
"""Max Product Difference Function"""
nums.sort()
return (nums[-1] * nums[-2]) - (nums[0] * nums[1])
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().maxProductDifference([5, 6, 2, 7, 4])) # Expected: 34
print(Solution().maxProductDifference([4, 2, 5, 9, 7, 4, 8])) # Expected: 64
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()
""" 1913.2 - Maximum Product Difference Between Two Pairs - Solution 2 - Single Pass """
#####################################################################################
# Imports
#####################################################################################
from typing import List
import math
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def maxProductDifference(self, nums: List[int]) -> int:
"""Max Product Difference Function"""
max1 = max2 = 0
min1 = min2 = math.inf
for num in nums:
if num > max1:
max2 = max1
max1 = num
elif num > max2:
max2 = num
if num < min1:
min2 = min1
min1 = num
elif num < min2:
min2 = num
return (max1 * max2) - (min1 * min2)
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().maxProductDifference([5, 6, 2, 7, 4])) # Expected: 34
print(Solution().maxProductDifference([4, 2, 5, 9, 7, 4, 8])) # Expected: 64
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()