Last active
August 29, 2024 09:42
-
-
Save AashishNandakumar/6b25b201ac8e2f167f4b091f8bd028b8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| """ 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 | |
This file contains hidden or 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
| """ 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