review of a codility test - pair_sum_even_count
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Codility tests are widely used to assess the technical skills of candidates for software development roles. One of the tasks you might encounter involves counting pairs of numbers in an array that sum up to an even number. This challenge, often referred to as pair_sum_even_count, tests one's understanding of number properties, array manipulation, and efficient computation.
Problem Statement
The task is as follows:
Given an array A of N integers, count the number of pairs (i, j) such that:
0 <= i < j < N(A[i] + A[j])is even.
Approach to the Problem
To solve the problem, we need to delve into some number properties:
Understanding Even and Odd
- Even + Even = Even
- Odd + Odd = Even
- Even + Odd = Odd
The above properties highlight that for a pair sum to be even, both numbers need to either be odd or even.
Algorithm
Based on the properties, our approach is broken into several steps:
- Count the Occurrences: Traverse through the array to count the number of even and odd numbers. Let's call them
even_countandodd_countrespectively. - Calculate Pairs:
- Even Pairs: Choose any two numbers from the even count. The number of such pairs can be calculated using combinatorics: \binom{\text{even_count}}{2} = \frac{\text{even_count} \times (\text{even_count} - 1)}{2}.
- Odd Pairs: Similarly, calculate pairs from the odd numbers: \binom{\text{odd_count}}{2} = \frac{\text{odd_count} \times (\text{odd_count} - 1)}{2}.
- Sum Up the Results: The total number of pairs is the sum of even pairs and odd pairs.
Pseudocode
Here’s how the solution might look in pseudocode:
Example Case
Let's say we have an array A = [1, 2, 3, 4, 5].
- Even Numbers:
[2, 4](Count: 2) - Odd Numbers:
[1, 3, 5](Count: 3)
- Even Pairs: Choose any 2 from 2: pair (
(2, 4)). - Odd Pairs: Choose any 2 from 3: pairs (
(1, 3),(1, 5),(3, 5)).
Total pairs with even sum = 1 (even pairs) + 3 (odd pairs) = 4.
Complexity Analysis
Counting the number of evens and odds takes O(N) time, where N is the length of the array. Calculating even and odd pairs takes O(1) due to constant-time arithmetic after counting. Thus, the overall time complexity of this solution is O(N) and the auxiliary space complexity is O(1).
Key Points and Data Summary
| Property | Odd + Even | Even + Even | Odd + Odd | Time Complexity |
| Sum Type | Odd | Even | Even | O(N) |
| Combination | 0 | $\binom{\text{even_count}}{2}$ | $\binom{\text{odd_count}}{2}$ | O(1) Space Complexity |
Conclusion
The pair_sum_even_count task challenges candidates to apply their understanding of mathematical properties in a coding context, focusing on efficiency and correctness. The solution literally builds on mathematical principles and culminates in a clever use of combinatorics. This problem is a great example of how seemingly simple mathematical insights can optimize code for real-world software development challenges.
Related reading
- RMI alternatives for bidirectional asynchronous calls and callbacks through firewalls or NAT
- Robot exploration algorithm
- Robust algorithm for chromatic instrument tuner?
- robust algorithm for surface reconstruction from 3D point cloud?
- Robust Algorithm to detect uneven illumination in images Detection Only Needed
- Robust and fast checksum algorithm?
- Robust Line Extraction from Image
- Rock paper Scissors bot algorithm

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.