Skip to content

Commit 2991d8f

Browse files
committed
Add solution to Task #1
1 parent 5bd04cd commit 2991d8f

File tree

3 files changed

+193
-0
lines changed

3 files changed

+193
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# 📍 Coding Problems
2+
3+
## 🦅 Overview
4+
5+
In this section (in current java package), I have added a list of coding problems that I have found on the Internet, and
6+
I have found them quite interesting and useful in learning algorithms.
7+
8+
If you want to get even more value from this section, please try to solve these tasks by yourself before looking onto my
9+
implementations.
10+
11+
## 📙 Task #1
12+
13+
Given a list of numbers and a number k, return whether any two numbers from the list add up to k.
14+
15+
For example, given [10, 15, 3, 7] and k of 17, return true since 10 + 7 is 17.
16+
17+
**Bonus:** Can you do this in one pass?
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.anverbogatov.algorithms.tasks;
2+
3+
import java.util.HashSet;
4+
5+
public final class Task1 {
6+
7+
/**
8+
* Find if any of two numbers in the given array add up to given `sum`.
9+
* Time complexity of the algorithm used here is O(n^2).
10+
*
11+
* @param numbers - given array of numbers
12+
* @param sum - given sum
13+
* @return `true` if two numbers in the given array add up to `sum`
14+
*/
15+
public static boolean isSumExist(int[] numbers, int sum) {
16+
for (int i = 0; i < numbers.length; i++) {
17+
int firstNum = numbers[i];
18+
for (int j = 0; j < numbers.length; j++) {
19+
int secondNum = numbers[j];
20+
if (i == j) {
21+
continue;
22+
}
23+
if (firstNum + secondNum == sum) {
24+
return true;
25+
}
26+
}
27+
}
28+
29+
return false;
30+
}
31+
32+
/**
33+
* Find if any of two numbers in the given array add up to given `sum`.
34+
* Time complexity of the algorithm used here is O(n) and that is better than another version in the class.
35+
*
36+
* @param numbers - given array of numbers
37+
* @param sum - given sum
38+
* @return `true` if two numbers in the given array add up to `sum`
39+
*/
40+
public static boolean isSumExistBonus(int[] numbers, int sum) {
41+
// todo: re-check
42+
if (numbers.length <= 1) {
43+
return false;
44+
}
45+
/*
46+
I'm using memoization here to store temporary data.
47+
Since both operations - add & contains on HashSets have O(1) complexity,
48+
we can do all calculations we need in one iteration on a given array.
49+
*/
50+
var temp = new HashSet<Integer>(numbers.length);
51+
for (var num : numbers) {
52+
temp.add(num);
53+
if (temp.contains(sum - num)) {
54+
return true;
55+
}
56+
}
57+
58+
return false;
59+
}
60+
61+
62+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package com.anverbogatov.algorithms.tasks;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
import java.util.stream.IntStream;
7+
8+
public class Task1Test {
9+
10+
@Test
11+
public void isSumExistOnEmptyArray() {
12+
// given
13+
var numbers = new int[]{};
14+
var sum = 17;
15+
// when
16+
var result = Task1.isSumExist(numbers, sum);
17+
// then
18+
Assert.assertFalse(result);
19+
}
20+
21+
@Test
22+
public void isSumExistOnSingleElementArray() {
23+
// given
24+
var numbers = new int[]{17};
25+
var sum = 17;
26+
// when
27+
var result = Task1.isSumExist(numbers, sum);
28+
// then
29+
Assert.assertFalse(result);
30+
}
31+
32+
@Test
33+
public void isSumExist() {
34+
// given
35+
var numbers = new int[]{10, 15, 3, 7};
36+
var sum = 17;
37+
// when
38+
var result = Task1.isSumExist(numbers, sum);
39+
// then
40+
Assert.assertTrue(result);
41+
}
42+
43+
@Test
44+
public void isSumExistNegative() {
45+
// given
46+
var numbers = new int[]{10, 15, 3, 7};
47+
var sum = 19;
48+
// when
49+
var result = Task1.isSumExist(numbers, sum);
50+
// then
51+
Assert.assertFalse(result);
52+
}
53+
54+
55+
@Test
56+
public void isSumExistOnEmptyArrayBonus() {
57+
// given
58+
var numbers = new int[]{};
59+
var sum = 17;
60+
// when
61+
var result = Task1.isSumExistBonus(numbers, sum);
62+
// then
63+
Assert.assertFalse(result);
64+
}
65+
66+
@Test
67+
public void isSumExistOnSingleElementArrayBonus() {
68+
// given
69+
var numbers = new int[]{17};
70+
var sum = 17;
71+
// when
72+
var result = Task1.isSumExistBonus(numbers, sum);
73+
// then
74+
Assert.assertFalse(result);
75+
}
76+
77+
@Test
78+
public void isSumExistBonus() {
79+
// given
80+
var numbers = new int[]{10, 15, 3, 7};
81+
var sum = 17;
82+
// when
83+
var result = Task1.isSumExistBonus(numbers, sum);
84+
// then
85+
Assert.assertTrue(result);
86+
}
87+
88+
@Test
89+
public void isSumExistNegativeBonus() {
90+
// given
91+
var numbers = new int[]{10, 15, 3, 7};
92+
var sum = 19;
93+
// when
94+
var result = Task1.isSumExistBonus(numbers, sum);
95+
// then
96+
Assert.assertFalse(result);
97+
}
98+
99+
@Test
100+
public void isSumExistPerformanceLog() {
101+
// given
102+
var numbers = IntStream.range(0, 10000).toArray();
103+
var sum = 9451;
104+
// when
105+
var _start = System.nanoTime();
106+
var result1 = Task1.isSumExist(numbers, sum);
107+
System.out.println("Original: " + (System.nanoTime() - _start));
108+
_start = System.nanoTime();
109+
var result2 = Task1.isSumExistBonus(numbers, sum);
110+
System.out.println("Bonus: " + (System.nanoTime() - _start));
111+
// then
112+
Assert.assertEquals(result1, result2);
113+
}
114+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy