Skip to content

Commit 405cec7

Browse files
committed
#Modification 90
2 parents 8b11155 + 0e84722 commit 405cec7

3 files changed

Lines changed: 117 additions & 2 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# Java Placement Preparation DSA CRACKER SHEET 💻🦸‍♂️🐱‍👤[179/450]
1+
# Java Placement Preparation DSA CRACKER SHEET 💻🦸‍♂️🐱‍👤[181/450]
22

3-
🐼 This is a full fled-ged repository for learning Java Language & DSA for Placement Preparation.
3+
This is a full fled-ged repository for learning Java Language & DSA for Placement Preparation.
44

55
💪 Here you can find the solution's of **_450 Questions of (Data Structure & Algorithms Cracker Sheet)_** By **LOVE BABBAR** Bhaiya.
66

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package backtracking;
2+
import java.util.*;
3+
4+
// Problem Title => Print all permutations of string
5+
public class Print_All_Permutations{
6+
7+
// Swap characters at position
8+
// @param a string value
9+
// @param i position 1
10+
// @param j position 2
11+
// @return swapped string
12+
public String swap(String a, int i, int j){
13+
char temp;
14+
char[] charArray = a.toCharArray();
15+
temp = charArray[i];
16+
charArray[i] = charArray[j];
17+
charArray[j] = temp;
18+
return String.valueOf(charArray);
19+
}
20+
21+
// permutation function
22+
// @param str string to calculate permutation for
23+
// @param 1 starting index
24+
// @param r and index
25+
private void permute(String str, int l, int r){
26+
if(l == r)
27+
System.out.println(str);
28+
29+
else{
30+
for(int i = 1; i <= r; i++){
31+
str = swap(str, l, i);
32+
permute(str, l+1, r);
33+
str = swap(str, l, i);
34+
}
35+
}
36+
}
37+
38+
// Driver Function
39+
public static void main(String[] args) {
40+
// scanner object for talking inputs
41+
Scanner sc = new Scanner(System.in);
42+
43+
// Asking user for String str as input
44+
String str = sc.nextLine();
45+
int n = str.length();
46+
47+
// Making an object of Print_All_Permutations class
48+
Print_All_Permutations permutation = new Print_All_Permutations();
49+
50+
// Permute the array or printing the permutation's
51+
permutation.permute(str, 0, n-1);
52+
}
53+
}

stack_and_queue/P38.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package stack_and_queue;
2+
import java.util.*;
3+
import java.util.Stack;
4+
import java.lang.*;
5+
6+
// Problem Title => Next Smaller Element
7+
public class P38 {
8+
9+
// prints element and NSE pair for all elements of arr[] of size n
10+
public static void printNSE(int[] arr, int n){
11+
Stack<Integer> s = new Stack<>();
12+
13+
// push the first element to stack
14+
s.push(arr[0]);
15+
16+
// iterate for rest of the elements
17+
for(int i = 1; i < n; i++){
18+
if(s.empty()){
19+
s.push(arr[i]);
20+
continue;
21+
}
22+
23+
// if stack is not empty,
24+
// then pop an element from stack.
25+
// If the popped element is greater than next, then
26+
// (a) print the pair,
27+
// (b) keep popping while elements are greater and stack is not empty.
28+
while(!s.empty() && s.peek() > arr[i]){
29+
System.out.println(s.peek() + " --> " + arr[i]);
30+
s.pop();
31+
}
32+
33+
// push next to stack so that we can find next smaller for it
34+
s.push(arr[i]);
35+
}
36+
37+
// After iterating over the loop,
38+
// the remaining elements in stack do not have the next smaller element,
39+
// so print -1 for them
40+
while(!s.empty()){
41+
System.out.println(s.peek() + " --> " + "-1");
42+
s.pop();
43+
}
44+
}
45+
46+
// Driver function
47+
public static void main(String[] args) {
48+
// Scanner object for input
49+
Scanner sc = new Scanner(System.in);
50+
51+
// Taking Size of the array as input from user
52+
int n = sc.nextInt();
53+
54+
// Taking the elements of array as input from user
55+
int[] arr = new int[n];
56+
for(int i = 0; i < n; i++)
57+
arr[i] = sc.nextInt();
58+
59+
// Printing the Next Smaller Element by calling printNSE method
60+
printNSE(arr, n);
61+
}
62+
}

0 commit comments

Comments
 (0)