Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save AashishNandakumar/6b25b201ac8e2f167f4b091f8bd028b8 to your computer and use it in GitHub Desktop.

Select an option

Save AashishNandakumar/6b25b201ac8e2f167f4b091f8bd028b8 to your computer and use it in GitHub Desktop.
""" O(n*n) approach """
class Solution:
def pairWithMaxSum(self, arr):
# Your code goes here
nums = arr
globalMaxSum = float('-inf')
n = len(nums)
for i in range(n-1):
fMin = nums[i]
for j in range(i+1, n):
sMin = nums[j]
if nums[j] < fMin:
sMin = fMin
fMin = nums[j]
elif nums[j] < sMin:
sMin = nums[j]
# print(i, j, fMin, sMin)
currentSum = fMin + sMin
globalMaxSum = max(globalMaxSum, currentSum)
return globalMaxSum
""" O(n) approach """
class Solution:
def pairWithMaxSum(self, arr):
# Your code goes here
globalMaxSum = float('-inf')
for i in range(len(arr)-1):
globalMaxSum = max(globalMaxSum, arr[i] + arr[i+1])
return globalMaxSum
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment