Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 158c3a6

Browse files
committed
some dsa questions
1 parent 9c5de50 commit 158c3a6

9 files changed

+160
-2
lines changed

src/binod/CalculateFactorial.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package binod;
22
public class CalculateFactorial {
33
public static void main(String[] args) {
4-
int fac=calcFactorial(5);
5-
System.out.println(fac);
4+
System.out.println(calcFactorial(5));
65
}
76
public static int calcFactorial(int n){
87
if(n==1||n==0){

src/binod/FibonacciSeries.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package binod;
2+
public class FibonacciSeries {
3+
//Q. Fibonacci Series till nth term
4+
public static void main(String[] args) {
5+
int a = 0;
6+
int b = 1;
7+
int n = 7;
8+
System.out.println(a);
9+
System.out.println(b);
10+
fib(a, b, n - 2);
11+
}
12+
public static void fib(int a, int b, int n) {
13+
if (n == 0) {
14+
return;
15+
}
16+
int c = a + b;
17+
System.out.println(c);
18+
fib(b, c, n - 1);
19+
}
20+
}
21+

src/binod/FindOccurance.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package binod;
2+
public class FindOccurance {
3+
public static int first = -1;
4+
public static int last = -1;
5+
public static void findOccurance(String str, int idx, char element) {
6+
if (idx == str.length()) {
7+
System.out.println(first);
8+
System.err.println(last);
9+
return;
10+
}
11+
char currChar = str.charAt(idx);
12+
if (currChar == element) {
13+
if (first == -1) {
14+
first = idx;
15+
} else {
16+
last = idx;
17+
}
18+
}
19+
findOccurance(str, idx + 1, element);
20+
}
21+
public static void main(String[] args) {
22+
String str = "abaacdaefaah";
23+
findOccurance(str, 0,'a');
24+
}
25+
}

src/binod/IsSorted.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package binod;
2+
public class IsSorted {
3+
public static boolean isSorted(int arr[], int idx) {
4+
if (idx == arr.length - 1) {
5+
return true;
6+
}
7+
if (arr[idx] < arr[idx + 1]) {
8+
//array is sorted till now
9+
return isSorted(arr, idx + 1);
10+
} else {
11+
return false;
12+
}
13+
}
14+
public static void main(String[] args) {
15+
int arr[] = {1, 3, 5};
16+
System.out.println(isSorted(arr, 0));
17+
}
18+
}

src/binod/MoveAllX.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package binod;
2+
public class MoveAllX {
3+
public static void moveAllX(String str, int idx, int count, String newString) {
4+
if (idx == str.length()) {
5+
for (int i = 0; i < count; i++) {
6+
newString += 'x';
7+
}
8+
System.out.println(newString);
9+
return;
10+
}
11+
char currChar = str.charAt(idx);
12+
if (currChar == 'x') {
13+
count++;
14+
moveAllX(str, idx + 1, count, newString);
15+
} else {
16+
newString += currChar;
17+
moveAllX(str, idx + 1, count, newString);
18+
}
19+
}
20+
public static void main(String args[]) {
21+
String str = "axbcxxd";
22+
moveAllX(str, 0, 0, "");
23+
}
24+
}

src/binod/RemoveDuplicates.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package binod;
2+
public class RemoveDuplicates {
3+
public static boolean[] map = new boolean[26];
4+
public static void removeDuplicates(String str, int idx, String newString) {
5+
if (idx == str.length()) {
6+
System.out.println(newString);
7+
return;
8+
}
9+
char currChar = str.charAt(idx);
10+
if (map[currChar - 'a'] == true) {
11+
removeDuplicates(str, idx + 1, newString);
12+
} else {
13+
newString += currChar;
14+
map[currChar - 'a'] = true;
15+
removeDuplicates(str, idx + 1, newString);
16+
}
17+
}
18+
public static void main(String args[]){
19+
String str="abbccda";
20+
removeDuplicates(str, 0, "");
21+
}
22+
}

src/binod/ReverseStringRecursion.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package binod;
2+
public class ReverseStringRecursion {
3+
public static void printReverse(String name, int idx) {
4+
if (idx == 0) {
5+
System.out.print(name.charAt(idx));
6+
return;
7+
}
8+
System.out.print(name.charAt(idx));
9+
printReverse(name, idx - 1);
10+
}
11+
public static void main(String[] args) {
12+
String name = "binod";
13+
printReverse(name, name.length()-1);
14+
}
15+
}

src/binod/SubSequences.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package binod;
2+
public class SubSequences {
3+
public static void subSequences(String str, int idx, String newString) {
4+
if (idx == str.length()) {
5+
System.err.println(newString);
6+
return;
7+
}
8+
char currChar = str.charAt(idx);
9+
//to be
10+
subSequences(str, idx + 1, newString + currChar);
11+
//or not to be
12+
subSequences(str, idx + 1, newString);
13+
}
14+
public static void main(String[] args) {
15+
String str = "abc";
16+
subSequences(str, 0, "");
17+
}
18+
}

src/binod/TowerOfHonai.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package binod;
2+
public class TowerOfHonai {
3+
public static void towerOfHonoi(int n, String src, String helper, String dest){
4+
if(n==1){
5+
System.out.println("transfer disk "+ n + " from "+src+ " to "+dest);
6+
return;
7+
}
8+
towerOfHonoi(n-1, src, dest, helper);
9+
System.out.println("transfer disk "+ n + " from "+src+" to "+dest);
10+
towerOfHonoi(n-1, helper, src, dest);
11+
}
12+
public static void main(String[] args) {
13+
int n=5;
14+
towerOfHonoi(n, "S", "H", "D");
15+
}
16+
}

0 commit comments

Comments
 (0)