In the set of n positive integers, find the largest element m, satisfy M=a + B, where a, a and a are the elements of the set S

Source: Internet
Author: User

Idea: first set sorting, time complexity Nlogn, and then the M from the maximum start to traverse, A and b respectively take less than m minimum and maximum, test whether the a+b is equal to M, if the small a right shift, if the big B shift left. The total time responsibility is n*n


int find (int s[], int n) {sort (s, S + N); for (int i = n-1; I >=2; i.) {int left = 0, right = I-1;while (left < righ T) {if (S[left] + s[right] = = S[i]) return S[i];else if (S[left] + s[right] > S[i])--right;else++left;}} return-1;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

In the set of n positive integers, find the largest element m, satisfy M=a + B, where a, a and a are the elements of the set S

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.