Skip to content

Commit 7c5251a

Browse files
committed
Recursive Algorithms updated
1 parent 236f7a8 commit 7c5251a

File tree

1 file changed

+85
-70
lines changed

1 file changed

+85
-70
lines changed

RecursionPractice/src/Main.java

Lines changed: 85 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,100 @@
11
public class Main {
2-
public static void main(String[] args) {
3-
// System.out.println(factorial(3));
4-
// for (int i = 0; i < 10; i++) {
5-
// System.out.print(findFibonacciSeries(i) + " ");
6-
// }
7-
// System.out.println(reverseString("Hello"));
8-
// System.out.println(reverseNumber(456));
9-
int[] array = {1, 2, 3, 4, 5};
10-
// BinarySearch(array, 10);
11-
// printAllPossibleCombinations(array, 3);
12-
}
2+
public static void main(String[] args) {
3+
// System.out.println(factorial(3));
4+
// for (int i = 0; i < 10; i++) {
5+
// System.out.print(findFibonacciSeries(i) + " ");
6+
// }
7+
// System.out.println(reverseString("Hello"));
8+
// System.out.println(reverseNumber(456, 0));
9+
int[] array = {1, 2, 3, 4, 5};
10+
// BinarySearch(array, 10);
11+
// printAllPossibleCombinations(array, 3);
12+
int[][] mat =
13+
{
14+
{4, 7, 1, 6},
15+
{5, 7, 3, 9},
16+
{3, 2, 1, 2},
17+
{7, 1, 6, 3}
18+
};
1319

14-
static void combinationUtil(
15-
int[] arr,
16-
int arrayLength,
17-
int numberOfRegs,
18-
int currentIndex,
19-
int[] data,
20-
int startingIndex) {
21-
if (currentIndex == numberOfRegs) {
22-
for (int j = 0; j < numberOfRegs; j++) System.out.print(data[j] + " ");
23-
System.out.print("\n");
24-
return;
20+
int cost = 25;
21+
int noOfPaths = findCostMatchPaths(mat, cost, mat.length - 1, mat[0].length - 1);
22+
// System.out.println("Path Cost : " + cost + ", Count = " + noOfPaths);
23+
// System.out.println(!isPrimeNumber(29, 2));
2524
}
2625

27-
if (startingIndex >= arrayLength) return;
26+
private static boolean isPrimeNumber(int num, int div) {
27+
if (num % div == 0) return true;
28+
return div + 1 < num / 2 && isPrimeNumber(num, div + 1);
29+
}
30+
31+
private static int findCostMatchPaths(int[][] mat, int cost, int x, int y) {
32+
if (x > -1 && y > -1 && cost > 0) {
33+
if (x == 0 && y == 0) return mat[0][0] - cost == 0 ? 1 : 0;
34+
if (x == 0) findCostMatchPaths(mat, cost - mat[x][y], 0, y - 1);
35+
if (y == 0) findCostMatchPaths(mat, cost - mat[x][y], x - 1, 0);
36+
return findCostMatchPaths(mat, cost - mat[x][y], x - 1, y) + findCostMatchPaths(mat, cost - mat[x][y], x, y - 1);
37+
}
38+
return 0;
39+
}
40+
41+
static void combinationUtil(
42+
int[] arr,
43+
int arrayLength,
44+
int numberOfRegs,
45+
int currentIndex,
46+
int[] data,
47+
int startingIndex) {
48+
if (currentIndex == numberOfRegs) {
49+
for (int j = 0; j < numberOfRegs; j++) System.out.print(data[j] + " ");
50+
System.out.print("\n");
51+
return;
52+
}
53+
54+
if (startingIndex >= arrayLength) return;
2855

29-
data[currentIndex] = arr[startingIndex];
30-
combinationUtil(arr, arrayLength, numberOfRegs, currentIndex + 1, data, startingIndex + 1);
31-
combinationUtil(arr, arrayLength, numberOfRegs, currentIndex, data, startingIndex + 1);
32-
}
56+
data[currentIndex] = arr[startingIndex];
57+
combinationUtil(arr, arrayLength, numberOfRegs, currentIndex + 1, data, startingIndex + 1);
58+
combinationUtil(arr, arrayLength, numberOfRegs, currentIndex, data, startingIndex + 1);
59+
}
3360

34-
static void printAllPossibleCombinations(int[] arr, int numberOfIndex) {
35-
int[] data = new int[numberOfIndex];
61+
static void printAllPossibleCombinations(int[] arr, int numberOfIndex) {
62+
int[] data = new int[numberOfIndex];
3663

37-
combinationUtil(arr, arr.length, numberOfIndex, 0, data, 0);
38-
}
64+
combinationUtil(arr, arr.length, numberOfIndex, 0, data, 0);
65+
}
3966

40-
private static void BinarySearch(int[] array, int number) {
41-
int firstData = 0;
42-
int lastData = array.length - 1;
43-
int i = 0;
44-
while (firstData <= lastData) {
45-
System.out.println(i++);
46-
int middleData = (firstData + lastData) / 2;
47-
if (array[middleData] == number) {
48-
System.out.println(number + " found at " + middleData);
49-
return;
50-
} else if (array[middleData] < number) {
51-
firstData = middleData + 1;
52-
} else if (array[middleData] > number) {
53-
lastData = middleData - 1;
54-
}
67+
private static void BinarySearch(int[] array, int number) {
68+
int firstData = 0;
69+
int lastData = array.length - 1;
70+
int i = 0;
71+
while (firstData <= lastData) {
72+
System.out.println(i++);
73+
int middleData = (firstData + lastData) / 2;
74+
if (array[middleData] == number) {
75+
System.out.println(number + " found at " + middleData);
76+
return;
77+
} else if (array[middleData] < number) {
78+
firstData = middleData + 1;
79+
} else if (array[middleData] > number) {
80+
lastData = middleData - 1;
81+
}
82+
}
5583
}
56-
}
5784

58-
private static int reverseNumber(int integer) {
59-
int number = 0;
60-
while (integer != 0) {
61-
number *= 10;
62-
number += (integer % 10);
63-
integer /= 10;
85+
private static int reverseNumber(int integer, int num) {
86+
return integer / 10 != 0 ? reverseNumber(integer / 10, (num + integer % 10) * 10) : num + integer;
6487
}
65-
return number;
66-
}
6788

68-
private static String reverseString(String text) {
69-
String reverseString = "";
70-
int StringLength = text.length();
71-
while (StringLength > 0) reverseString += text.charAt(--StringLength);
72-
return reverseString;
73-
}
89+
private static String reverseString(String text) {
90+
return text.length() == 0 ? "" : reverseString(text.substring(1)) + text.charAt(0);
91+
}
7492

75-
private static int findFibonacciSeries(int number) {
76-
return (number == 1 || number == 2)
77-
? 1
78-
: (number == 0) ? 0 : findFibonacciSeries(number - 1) + findFibonacciSeries(number - 2);
79-
}
93+
private static int findFibonacciSeries(int number) {
94+
return number != 1 && number != 2 ? (number == 0 ? 0 : findFibonacciSeries(number - 1) + findFibonacciSeries(number - 2)) : 1;
95+
}
8096

81-
private static int factorial(int number) {
82-
if (number == 0 || number == 1) return 1;
83-
return number * factorial(number - 1);
84-
}
97+
private static int factorial(int number) {
98+
return number != 0 && number != 1 ? number * factorial(number - 1) : 1;
99+
}
85100
}

0 commit comments

Comments
 (0)