Skip to content

Commit 5313498

Browse files
committed
Add "Find max consecutive elements in array" task
1 parent 3d3da7d commit 5313498

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed

src/main/java/com/anverbogatov/algorithms/tasks/README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,10 @@ For example, given [10, 15, 3, 7] and k of 17, return true since 10 + 7 is 17.
2020

2121
Implement algorithm that shows if all characters in a given string are unique.
2222

23-
**Bonus**: What if you are not allowed to use other data structures?
23+
**Bonus**: What if you are not allowed to use other data structures?
24+
25+
## 📗 Task #3
26+
27+
In the given array of numbers find max amount of consecutive elements.
28+
29+
For example, given [10, 15, 2, 2, 2, 3, 7] max amount of consecutive elements will be equal to 3.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.anverbogatov.algorithms.tasks;
2+
3+
import java.util.HashSet;
4+
5+
public final class Task3 {
6+
7+
/**
8+
* Calculate amount of consecutive elements in a given array.
9+
*
10+
* @param arr - array that need to be checked
11+
* @return number of consecutive elements
12+
*/
13+
public static int maxAmountOfConsecutiveElements(int[] arr) {
14+
if (arr == null || arr.length == 0) {
15+
return 0;
16+
}
17+
18+
if (arr.length == 1) {
19+
return 1;
20+
}
21+
22+
int result = 0;
23+
int leftIndex = 0;
24+
25+
for (; leftIndex < arr.length - 1; leftIndex++) {
26+
int rightIndex = leftIndex;
27+
28+
for (; rightIndex < arr.length; rightIndex++) {
29+
if (arr[leftIndex] != arr[rightIndex]) {
30+
break;
31+
}
32+
}
33+
34+
result = Math.max(result, rightIndex - leftIndex);
35+
}
36+
return result;
37+
}
38+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.anverbogatov.algorithms.tasks;
2+
3+
import junit.framework.TestCase;
4+
import org.junit.Assert;
5+
6+
public class Task3Test extends TestCase {
7+
8+
public void testNull() {
9+
Assert.assertEquals(0, Task3.maxAmountOfConsecutiveElements(null));
10+
}
11+
12+
public void testSingleElement() {
13+
Assert.assertEquals(1, Task3.maxAmountOfConsecutiveElements(new int[]{1}));
14+
}
15+
16+
public void testAlgorithmWithSingleSequence() {
17+
Assert.assertEquals(3, Task3.maxAmountOfConsecutiveElements(new int[]{10, 15, 2, 2, 2, 3, 7}));
18+
}
19+
20+
public void testAlgorithmWithMultipleSequence() {
21+
Assert.assertEquals(4, Task3.maxAmountOfConsecutiveElements(new int[]{10, 15, 2, 2, 2, 3, 7, 1, 1, 7, 7, 7, 7, 11, 2, -4}));
22+
}
23+
}

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