From 74cbc9fb07496b9a83391dde27b6c5ab2ab29e97 Mon Sep 17 00:00:00 2001 From: miguel Date: Mon, 22 Apr 2024 14:55:15 +0100 Subject: [PATCH] feat(findspairs): new exam exercise --- subjects/findpairs/README.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 subjects/findpairs/README.md diff --git a/subjects/findpairs/README.md b/subjects/findpairs/README.md new file mode 100644 index 000000000..388829f86 --- /dev/null +++ b/subjects/findpairs/README.md @@ -0,0 +1,32 @@ +## Find Pairs with Sum + +### Instructions + +Write a program that finds all pairs of elements in an integer array that sum up to a given target value. The program should output a list of pairs, each representing the indices of the elements that form the pair. + +In this exercise you must take in consideration the following: + +- Ensure its possible to have positive or negative integers in the array +- Ensure each element is used only once in a pair +- Allow for multiple pairs to sum up to the target value +- Return the message "No pairs found." when no pair is present +- Return the message "Invalid target sum." if the target is invalid +- Return the message "Invalid number." if the number in the array is invalid + +Let's consider the input `arr = [1, 2, 3, 4, 5]` and the target sum `targetSum = 6`. When we run the program, the `findPairs()` function will search for pairs in the array that sum up to `targetSum`. + +The expected output for this input will be: + +```console +$ go run . "[1, 2, 3, 4, 5]" "6" +Pairs with sum 6: [[0 4] [1 3]] +$ go run . "[-1, 2, -3, 4, -5]" "1" +Pairs with sum 1: [[0 1] [2 3]] +$ go run . "[1, 2, 3, 4, 5]" "10" +No pairs found. +$ go run . "[1, 2, 3, 4, 20, -4, 5]" "2 5" +Invalid target sum. +$ go run . "[1, 2, 3, 4, 20, p, 5]" "5" +Invalid number: p +$ +```