Skip to content
This repository was archived by the owner on Aug 22, 2025. It is now read-only.

Commit 4bbd62c

Browse files
author
Holger Lösken
committed
Add task5 in php
1 parent f88d0c9 commit 4bbd62c

File tree

4 files changed

+94
-7
lines changed

4 files changed

+94
-7
lines changed

README.md

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ Write a function `jump_out_of_array` that outputs
4444
* the amount of jumps until you jump out of the array
4545
* `-1` when you reach the end of the array but do not jump out
4646

47-
** Requirements:**
47+
**Requirements:**
4848
* Array size is indefinite
4949
* Array elements are integers, positive and negative
5050

@@ -54,12 +54,35 @@ Given an array of `A[2, 3, -1, 1, 6, 4]`.
5454

5555
![](./docs/t4/task4.png)
5656

57-
* Jump 1: A[0] + 2 = A[2]
58-
* Jump 2: A[2] + (-1) = A[1]
59-
* Jump 3: A[1] + 3 = A[4]
60-
* Jump 4: A[4] + 6 = out of range
57+
* Jump 1: `A[0]` + `2` = `A[2]`
58+
* Jump 2: `A[2]` + `(-1)` = `A[1]`
59+
* Jump 3: `A[1]` + `3` = `A[4]`
60+
* Jump 4: `A[4]` + `6` = out of range
61+
62+
So the result is `4`, you need `4` jumps to jump out of the array.
63+
64+
### Task 5
65+
66+
Find the k-complement pairs in an array of a given number. Write a function `k_complement` that that outputs the amount
67+
of pairs.
68+
69+
**Requirements:**
70+
71+
_Do not_ use nested loops to solve this problem, because of a time complexity of the loop solution.
72+
[Check this thread](https://stackoverflow.com/questions/11032015/how-to-find-time-complexity-of-an-algorithm) to see what time complexity of an algorithm means.
73+
74+
75+
**Example:**
76+
77+
Given a number `6` and an array `A[1, 8, -3, 0, 1, 3, -2, 4, 5]` the amount of pairs is `7`.
6178

62-
So the result is `4`, you need 4 jumps to jump out of the array.
79+
* `A[0]` + `A[8]` = `1` + `5` = `6`
80+
* `A[1]` + `A[6]` = `8` + `-2` = `6`
81+
* `A[4]` + `A[8]` = `1` + `5` = `6`
82+
* `A[5]` + `A[5]` = `3` + `3` = `6`
83+
* `A[5]` + `A[5]` = `3` + `3` = `6`
84+
* `A[6]` + `A[1]` = `-2` + `8` = `6`
85+
* `A[8]` + `A[0]` = `5` + `1` = `6`
6386

6487
## Contribute
6588

src/php/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"t1/s1.php",
1111
"t2/s1.php",
1212
"t3/s1.php",
13-
"t4/s1.php"
13+
"t4/s1.php",
14+
"t5/s1.php"
1415
],
1516
"psr-4": {
1617
"Tests\\": "tests/"

src/php/t5/s1.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
function k_complement(int $k, array $a): int {
6+
$amountComplimentary = 0;
7+
8+
/**
9+
* Hold amount of occurrence of a single number inside starting array '$a'
10+
* Structure is:
11+
* [
12+
* [
13+
* key: number
14+
* value: amount occurrence in original array '$a'
15+
* ],
16+
* [
17+
* ...
18+
* ],
19+
* ]
20+
*/
21+
$occurrencesNumbers = [];
22+
23+
foreach ($a as $number) {
24+
if (!array_key_exists($number, $occurrencesNumbers)) {
25+
$occurrencesNumbers[$number] = 1;
26+
} else {
27+
$occurrencesNumbers[$number]++;
28+
}
29+
}
30+
31+
$numbers = array_keys($occurrencesNumbers);
32+
33+
foreach ($numbers as $number) {
34+
// Building the complementary pair by subtracting one number from the target result
35+
// and checking if the result (other pair part) exists
36+
$result = $k - $number;
37+
if (in_array($result, $numbers)) {
38+
$amountComplimentary += $occurrencesNumbers[$number] * $occurrencesNumbers[$result];
39+
}
40+
}
41+
42+
return $amountComplimentary;
43+
}

src/php/tests/T5Test.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests;
6+
7+
use PHPUnit\Framework\TestCase;
8+
9+
final class T5Test extends TestCase
10+
{
11+
public function testKcomplementPositiveNumber(): void
12+
{
13+
$this->assertSame(7, k_complement(6, [1, 8, -3, 0, 1, 3, -2, 4, 5]));
14+
}
15+
16+
public function testKcomplementNegativeNumber(): void
17+
{
18+
$this->assertSame(2, k_complement(-1, [-1, 2, 0]));
19+
}
20+
}

0 commit comments

Comments
 (0)