Is there any solution beside O(n2) .
I was thinking about looping through every value and then get the sum
Is there any solution beside O(n2) . I was thinking about looping through every
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The pair which is made from the 2 largest elements sum to the largest number. Just find the 2 largest elements and sum them – it is
O(n)For general
kelements that sum to the largest number, you can use selection algorithm to find the k largest element, and then with a 2nd iteration – sum all elements greater then it.