Codility Test
Pair Sum
Programming
Code Review
Algorithm Evaluation

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.

Practice algorithms

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

  1. Even + Even = Even
  2. Odd + Odd = Even
  3. 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:

  1. Count the Occurrences: Traverse through the array to count the number of even and odd numbers. Let's call them even_count and odd_count respectively.
  2. 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}.
  3. 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:

plaintext
1function pair_sum_even_count(A):
2    even_count = 0
3    odd_count = 0
4    
5    for number in A:
6        if number % 2 == 0:
7            even_count += 1
8        else:
9            odd_count += 1
10    
11    even_pairs = (even_count * (even_count - 1)) / 2
12    odd_pairs = (odd_count * (odd_count - 1)) / 2
13    
14    return even_pairs + odd_pairs

Example Case

Let's say we have an array A = [1, 2, 3, 4, 5].

  1. Even Numbers: [2, 4] (Count: 2)
  2. Odd Numbers: [1, 3, 5] (Count: 3)
  • Even Pairs: Choose any 2 from 2: 11 pair ((2, 4)).
  • Odd Pairs: Choose any 2 from 3: (32)=3\binom{3}{2} = 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

PropertyOdd + EvenEven + EvenOdd + OddTime Complexity
Sum TypeOddEvenEvenO(N)
Combination0$\binom&#123;\text&#123;even_count&#125;&#125;&#123;2&#125;$$\binom&#123;\text&#123;odd_count&#125;&#125;&#123;2&#125;$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
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice algorithms

All Rights Reserved.