Twitter OA Prepare:even sum pairs

Source: Internet
Author: User

Write a function:classSolution { Public intSolution (int[] A); }that, given an array A consisting of N integers, returns the number of pairs (P, Q) such that0≤p < Q < N and (A[p] + a[q]) is even. The function shouldreturn−1ifThe number of such pairs exceeds 1,000,000,000. For example, given array A such that:a[0] = 2, a[1] = 1, a[2] = 5, a[3] =−6, a[4] = 9The function shouldreturn4, because there is four pairs that fulfill the above condition, namely (0,3), (), (1,4), (2,4). Assume That:n is an integer within the range [0..1,000,000];each element of array A is an integer within the range [−2,147,483,648..2,147,483,647]. Complexity:expected Worst- CaseTime complexity is O (N); expected worst- CaseSpace complexity is O (1), Beyond input storage (not counting the storage required forinput arguments). Elements of input arrays can be modified.

Idea: Nothing is to scan the number of odd and even numbers, for example, M and N, and then the odd selection of two, even inside the selection of two, a odd and even, the answer is M (M-1)/2 + N (N-1)/2 + MN

Twitter OA Prepare:even sum pairs

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.