Twitter OA prepare: even sum pairs

Write a function:
class Solution { public int solution(int[] A); }
that, given an array A consisting of N integers, returns the number of pairs (P, Q) such that 0 ≤ P < Q < N and (A[P] + A[Q]) is even. The function should return −1 if the 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] = 9
the function should return 4, because there are four pairs that fulfill the above condition, namely (0,3), (1,2), (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-case time complexity is O(N);
expected worst-case space complexity is O(1), beyond input storage (not counting the storage required for input arguments).
Elements of input arrays can be modified.

思路:无非就是扫描一遍记录奇数和偶数各自的个数,比如为M和N,然后就是奇数里面选两个、偶数里面选两个、一奇一偶,答案就是M(M-1)/2 + N(N-1)/2 + MN

时间: 2024-10-26 21:46:00

Twitter OA prepare: even sum pairs的相关文章

Twitter OA prepare: Rational Sum

In mathematics, a rational number is any number that can be expressed in the form of a fraction p/q , where p & q are two integers, and the denominator q is not equal to zero. Hence, all integers are rational numbers where denominator, in the most re

Twitter OA prepare: K-complementary pair

A non-empty zero-indexed array A consisting of N integers is given. A pair of integers (P, Q) is called K-complementary in array A if 0 ≤ P, Q < N and A[P] + A[Q] = K. For example, consider array A such that: A[0] = 1 A[1] = 8 A[2]= -3 A[3] = 0 A[4]

Twitter OA prepare: Equilibrium index of an array

Equilibrium index of an array is an index such that the sum of elements at lower indexes is equal to the sum of elements at higher indexes. For example, in an arrya A: A[0] = -7, A[1] = 1, A[2] = 5, A[3] = 2, A[4] = -4, A[5] = 3, A[6]=0 3 is an equil

Twitter OA prepare: Flipping a bit

You are given a binary array with N elements: d[0], d[1], ... d[N - 1]. You can perform AT MOST one move on the array: choose any two integers [L, R], and flip all the elements between (and including) the L-th and R-th bits. L and R represent the lef

Twitter OA prepare: Anagram is A Palindrome

A string is a palindrome if it has exactly the same sequence of characters when traversed left-to-right as right-to-left. For example, the following strings are palindromes: "kayak" "codilitytilidoc" "neveroddoreven" A string

Twitter OA prepare: Visit element of the array

A zero-indexed array A consisting of N integers is viven. We visit the indexs of the array in the following way. In the first step we visit the index 0; in every subsequent step we move from the visited index K to the index: M = K + A[K]; provided M

LC 677. Map Sum Pairs

Implement a MapSum class with insert, and sum methods. For the method insert, you'll be given a pair of (string, integer). The string represents the key and the integer represents the value. If the key already existed, then the original key-value pai

[Swift]LeetCode677. 键值映射 | Map Sum Pairs

Implement a MapSum class with insert, and sum methods. For the method insert, you'll be given a pair of (string, integer). The string represents the key and the integer represents the value. If the key already existed, then the original key-value pai

LeetCode 677. Map Sum Pairs 键值映射(C++/Java)

题目: Implement a MapSum class with insert, and sum methods. For the method insert, you'll be given a pair of (string, integer). The string represents the key and the integer represents the value. If the key already existed, then the original key-value