Skip to content

Commit 060069c

Browse files
authored
Add Golomb Sequence (fixes TheAlgorithms#2889) (TheAlgorithms#2991)
1 parent ab544c3 commit 060069c

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/** Author : Siddhant Swarup Mallick
2+
* Github : https://github.com/siddhant2002
3+
*/
4+
/**
5+
* In mathematics, the Golomb sequence is a non-decreasing integer sequence where n-th term is equal to number of times n appears in the sequence.
6+
*/
7+
8+
/**
9+
* Wikipedia Link - https://en.wikipedia.org/wiki/Golomb_sequence
10+
*/
11+
12+
/** Program description - To find the Golomb sequence upto n */
13+
14+
package com.thealgorithms.dynamicprogramming;
15+
16+
public class CountFriendsPairing {
17+
public static boolean countFriendsPairing(int n, int a[]) {
18+
int dp[] = new int[n + 1];
19+
// array of n+1 size is created
20+
dp[0] = 1;
21+
// since 1st index position value is fixed so it's marked as 1
22+
for (int i = 1; i < n; i++) {
23+
dp[i] = 1 + dp[i - dp[dp[i - 1]]];
24+
// formula for ith golomb sequence is dp(i) = 1 + dp(i – dp(dp(i - 1)))
25+
}
26+
for (int i = 1; i < n; i++) {
27+
if (a[i - 1] != dp[i]) {
28+
return false;
29+
// checks whether the calculated answer matches with the expected answer
30+
}
31+
}
32+
return true;
33+
// returns true if calculated answer matches with the expected answer
34+
}
35+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.thealgorithms.others;
2+
import org.junit.jupiter.api.Test;
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import com.thealgorithms.dynamicprogramming.CountFriendsPairing;
6+
public class CountFriendsPairingTest {
7+
@Test
8+
void testForOneElement()
9+
{
10+
int a[] = {1,2,2};
11+
assertTrue(CountFriendsPairing.countFriendsPairing(3,a));
12+
}
13+
14+
@Test
15+
void testForTwoElements()
16+
{
17+
int a[] = {1,2,2,3};
18+
assertTrue(CountFriendsPairing.countFriendsPairing(4,a));
19+
}
20+
21+
@Test
22+
void testForThreeElements()
23+
{
24+
int a[] = {1,2,2,3,3};
25+
assertTrue(CountFriendsPairing.countFriendsPairing(5,a));
26+
}
27+
28+
@Test
29+
void testForFourElements()
30+
{
31+
int a[] = {1,2,2,3,3,4};
32+
assertTrue(CountFriendsPairing.countFriendsPairing(6,a));
33+
}
34+
35+
@Test
36+
void testForFiveElements()
37+
{
38+
int a[] = {1,2,2,3,3,4,4};
39+
assertTrue(CountFriendsPairing.countFriendsPairing(7,a));
40+
}
41+
42+
@Test
43+
void testForSixElements()
44+
{
45+
int a[] = {1,2,2,3,3,4,4,4};
46+
assertTrue(CountFriendsPairing.countFriendsPairing(8,a));
47+
}
48+
49+
@Test
50+
void testForSevenElements()
51+
{
52+
int a[] = {1,2,2,3,3,4,4,4,5};
53+
assertTrue(CountFriendsPairing.countFriendsPairing(9,a));
54+
}
55+
56+
@Test
57+
void testForEightElements()
58+
{
59+
int a[] = {1,2,2,3,3,4,4,4,5,5};
60+
assertTrue(CountFriendsPairing.countFriendsPairing(10,a));
61+
}
62+
}

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