forked from Srinivas11789/AlgorithmNuggets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharr.py
More file actions
18 lines (14 loc) · 671 Bytes
/
arr.py
File metadata and controls
18 lines (14 loc) · 671 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution(object):
def arrayPairSum(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
# 100 pass - O(n)
# Taking 2 numbers at a time itself, works to be n//2 or n from 2n
# Sorting the array and moving forward ensures the maximum number that can be obtained from the operation, as the ascending order ensures that maximum number is always obtained in an operation of minumim between 2 consecutive natural numbers
nums.sort()
total = 0
for i in range(0, len(nums), 2):
total += min(nums[i],nums[i+1])
return total