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

Commit f4c37ad

Browse files
committed
new algorithms
1 parent 4a69250 commit f4c37ad

17 files changed

+584
-0
lines changed

src/main/java/BinaryNumbers.java

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @author medany
3+
*/
4+
5+
/*
6+
* Given a base-10 integer, n, convert it to binary (base-2). Then find and
7+
* print the base-10 integer denoting the maximum number of consecutive 1's in
8+
* n's binary representation.
9+
*/
10+
public class BinaryNumbers {
11+
12+
public int solve(int decimal) {
13+
int max = 0, consecutive = 0;
14+
while (decimal != 0) {
15+
consecutive = decimal % 2 == 1 ? consecutive + 1 : 0;
16+
max = consecutive > max ? consecutive : max;
17+
decimal /= 2;
18+
}
19+
return max;
20+
}
21+
}

src/main/java/BirthdayChocolate.java

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* @author medany
3+
*/
4+
5+
/*
6+
* Lily has a chocolate bar consisting of a row of n squares where each square
7+
* has an integer written on it. She wants to share it with Ron for his
8+
* birthday, which falls on month m and day d. Lily wants to give Ron a piece of
9+
* chocolate only if it contains consecutive squares whose integers sum to d.
10+
*
11+
* Given m, d, and the sequence of integers written on each square of Lily's
12+
* chocolate bar, how many different ways can Lily break off a piece of
13+
* chocolate to give to Ron?
14+
*/
15+
public class BirthdayChocolate {
16+
17+
public int solve(int d, int m, int[] sequence) {
18+
int max = 0, count = 0;
19+
for (int i = 0; i < sequence.length; i++) {
20+
count = 0;
21+
for (int j = i; i + m > sequence.length ? j < sequence.length : j < i + m; j++) {
22+
count += sequence[j];
23+
if (count > d) {
24+
break;
25+
} else if (count == d && j == i + m - 1) {
26+
count = 0;
27+
max++;
28+
break;
29+
}
30+
}
31+
}
32+
return max;
33+
}
34+
}

src/main/java/BitwiseAND.java

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* @author medany
3+
*/
4+
5+
public class BitwiseAND {
6+
public int solve(int n, int k) {
7+
return ((k - 1) | k) > n && k % 2 == 0 ? k - 2 : k - 1;
8+
}
9+
10+
}

src/main/java/EqualizetheArray.java

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import java.util.HashMap;
2+
import java.util.Map;
3+
4+
/**
5+
* @author medany
6+
*/
7+
8+
/*
9+
* Karl has an array n of integers defined as A = a0, a1, ..., an-1. In one
10+
* operation, he can delete any element from the array.
11+
*
12+
* Karl wants all the elements of the array to be equal to one another. To do
13+
* this, he must delete zero or more elements from the array. Find and print the
14+
* minimum number of deletion operations Karl must perform so that all the
15+
* array's elements are equal.
16+
*/
17+
18+
public class EqualizetheArray {
19+
20+
public int solve(int[] arr) {
21+
Map<Integer, Integer> allOccurs = new HashMap<>();
22+
int maxOccurs = 0;
23+
for (int i = 0; i < arr.length; i++) {
24+
if (allOccurs.containsKey(arr[i])) {
25+
allOccurs.put(arr[i], allOccurs.get(arr[i]) + 1);
26+
if (allOccurs.get(arr[i]) > maxOccurs) {
27+
maxOccurs = allOccurs.get(arr[i]);
28+
}
29+
} else {
30+
allOccurs.put(arr[i], 1);
31+
}
32+
}
33+
return maxOccurs == 0 ? arr.length - 1 : arr.length - maxOccurs;
34+
}
35+
36+
}

src/main/java/FindAnagrams.java

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import java.util.HashSet;
2+
import java.util.LinkedHashMap;
3+
import java.util.Map;
4+
import java.util.Set;
5+
6+
/**
7+
* @author mohamed
8+
*/
9+
10+
/*
11+
* Find if a String A contains anagrams of a String B
12+
*/
13+
14+
public class FindAnagrams {
15+
16+
private static int primes[] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79,
17+
83, 89, 97, 101 };
18+
19+
public String[] solve(String haystack, String needle) {
20+
final String word = haystack.toLowerCase().trim();
21+
final String anagram = needle.toLowerCase().trim();
22+
final Set<String> anagrams = new HashSet<>();
23+
24+
long anagramProduct = 1;
25+
26+
// 01. calculate product of given String B
27+
for (int i = 0; i < anagram.length(); i++) {
28+
anagramProduct *= primes[anagram.charAt(i) - 'a'];
29+
}
30+
31+
final Map<Long, Integer> cumprods = new LinkedHashMap<>();
32+
33+
long cumprod = 1;
34+
for (int i = 0; i < word.length(); i++) {
35+
String current = "";
36+
// 02. calculate cumulative product of String A
37+
cumprod *= primes[word.charAt(i) - 'a'];
38+
// 03. add cumulative product to a Set
39+
cumprods.put(cumprod, i);
40+
41+
if (cumprod % anagramProduct == 0) {
42+
if (cumprod == anagramProduct || cumprods.containsKey(cumprod / anagramProduct)) {
43+
current = word.substring(i - anagram.length() + 1, i + 1);
44+
if (!anagram.equals(current))
45+
anagrams.add((word.substring(i - anagram.length() + 1, i + 1)));
46+
}
47+
}
48+
}
49+
return anagrams.toArray(new String[] {});
50+
}
51+
}

src/main/java/LeapYear.java

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
/**
3+
* @author medany
4+
*/
5+
6+
public class LeapYear {
7+
public boolean solve(int year) {
8+
return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0) ? true : false;
9+
}
10+
}

src/main/java/RepeatedString.java

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* @author medany
3+
*/
4+
5+
/*
6+
* Print a single integer denoting the number of letter a's in the first n
7+
* letters of the infinite string created by repeating s infinitely many times.
8+
*/
9+
10+
public class RepeatedString {
11+
12+
public long solve(String s, long n) {
13+
long num = 0, repeats = 0, aCount = 0;
14+
if (s.equals("a")) {
15+
return n;
16+
} else {
17+
for (int i = 0; i < s.length(); i++) {
18+
if (s.charAt(i) == 'a') {
19+
aCount++;
20+
}
21+
}
22+
23+
repeats = n / s.length();
24+
25+
if (repeats * s.length() == n) {
26+
return aCount * repeats;
27+
} else {
28+
num = aCount * repeats;
29+
String remain = s.substring(0, (int) n - (int) (s.length() * repeats));
30+
for (int i = 0; i < remain.length(); i++) {
31+
if (remain.charAt(i) == 'a') {
32+
num++;
33+
}
34+
}
35+
}
36+
}
37+
return num;
38+
}
39+
}

src/main/java/StringSimilarity.java

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
/**
3+
* @author medany
4+
*/
5+
6+
/*
7+
* For two strings A and B, we define the similarity of the strings to be the
8+
* length of the longest prefix common to both strings. For example, the
9+
* similarity of strings "abc" and "abd" is 2, while the similarity of strings
10+
* "aaa" and "aaab" is 3.
11+
*
12+
* Calculate the sum of similarities of a string S with each of it's suffixes.
13+
*/
14+
public class StringSimilarity {
15+
16+
public long solve(String a) {
17+
18+
long similarities = a.length();
19+
String suffix = new String(a.substring(1));
20+
int i;
21+
22+
while (!suffix.isEmpty()) {
23+
for (i = 0; i < suffix.length(); i++) {
24+
if (suffix.charAt(i) == a.charAt(i)) {
25+
similarities += 1;
26+
} else
27+
break;
28+
}
29+
suffix = suffix.substring(1);
30+
}
31+
32+
return similarities;
33+
}
34+
35+
}

test/main/java/BinaryNumbersTest.java

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import org.junit.Assert;
2+
import org.junit.Test;
3+
4+
/**
5+
* @author medany
6+
*/
7+
public class BinaryNumbersTest {
8+
9+
private BinaryNumbers alg = new BinaryNumbers();
10+
private int actual, expected;
11+
12+
@Test
13+
public void Test_1() {
14+
15+
actual = alg.solve(5);
16+
expected = 1;
17+
18+
Assert.assertEquals(expected, actual);
19+
}
20+
21+
@Test
22+
public void Test_2() {
23+
24+
actual = alg.solve(12);
25+
expected = 2;
26+
27+
Assert.assertEquals(expected, actual);
28+
}
29+
30+
@Test
31+
public void Test_3() {
32+
33+
actual = alg.solve(524283);
34+
expected = 16;
35+
36+
Assert.assertEquals(expected, actual);
37+
}
38+
39+
}
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import org.junit.Assert;
2+
import org.junit.Test;
3+
4+
/**
5+
* @author medany
6+
*/
7+
public class BirthdayChocolateTest {
8+
9+
private BirthdayChocolate alg = new BirthdayChocolate();
10+
private int actual, expected;
11+
12+
@Test
13+
public void Test_1() {
14+
15+
actual = alg.solve(3, 2, new int[] { 1, 2, 1, 3, 2 });
16+
expected = 2;
17+
18+
Assert.assertEquals(expected, actual);
19+
}
20+
21+
@Test
22+
public void Test_2() {
23+
24+
actual = alg.solve(3, 2, new int[] { 1, 1, 1, 1, 1, 1 });
25+
expected = 0;
26+
27+
Assert.assertEquals(expected, actual);
28+
}
29+
30+
@Test
31+
public void Test_3() {
32+
33+
actual = alg.solve(18, 6, new int[] { 4, 5, 4, 5, 1, 2, 1, 4, 3, 2, 4, 4, 3, 5, 2, 2, 5, 4, 3, 2, 3, 5, 2, 1, 5,
34+
2, 3, 1, 2, 3, 3, 1, 2, 5 });
35+
expected = 6;
36+
37+
Assert.assertEquals(expected, actual);
38+
}
39+
40+
}

test/main/java/BitwiseANDTest.java

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import org.junit.Assert;
2+
import org.junit.Test;
3+
4+
/**
5+
* @author medany
6+
*/
7+
public class BitwiseANDTest {
8+
9+
private BitwiseAND alg = new BitwiseAND();
10+
private int actual, expected;
11+
12+
@Test
13+
public void Test_1() {
14+
15+
actual = alg.solve(5, 2);
16+
expected = 1;
17+
18+
Assert.assertEquals(expected, actual);
19+
}
20+
21+
@Test
22+
public void Test_2() {
23+
24+
actual = alg.solve(8, 5);
25+
expected = 4;
26+
27+
Assert.assertEquals(expected, actual);
28+
}
29+
30+
@Test
31+
public void Test_3() {
32+
33+
actual = alg.solve(2, 2);
34+
expected = 0;
35+
36+
Assert.assertEquals(expected, actual);
37+
}
38+
39+
}

0 commit comments

Comments
 (0)