Skip to content

Commit ec7d68e

Browse files
committed
#Modification 14
1 parent 273a4a9 commit ec7d68e

21 files changed

Lines changed: 1067 additions & 89 deletions

arrays/Array_Problem_13.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package arrays;
2+
import java.util.*;
3+
4+
/*
5+
* Kadane's Algorithm to find the maxSubarray Sum in O(n)[Linear Time Complexity]
6+
*/
7+
public class Array_Problem_13 {
8+
public static void main(String[] args) {
9+
@SuppressWarnings("resource")
10+
Scanner sc = new Scanner(System.in);
11+
int n = sc.nextInt();
12+
int a[] = new int[n];
13+
14+
for(int i=0;i<n;i++)
15+
a[i] = sc.nextInt();
16+
17+
int maxSum = 0;
18+
int currentSum = 0;
19+
20+
for(int i = 0; i < n; i++)
21+
{
22+
currentSum += a[i];
23+
if (currentSum < 0) {
24+
currentSum = 0;
25+
}
26+
maxSum = Math.max(maxSum, currentSum);
27+
28+
System.out.println(maxSum);
29+
}
30+
}
31+
32+
}

arrays/Array_Problem_14.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package arrays;
2+
3+
import java.util.*;
4+
5+
public class Array_Problem_14 {
6+
public static void mergeIntervals(Interval a[]) {
7+
if (a.length <= 0) {
8+
return;
9+
}
10+
11+
Stack<Interval> stack = new Stack<>();
12+
Arrays.sort(a.new Comparator<Interval>() {
13+
public int compare(Interval i1, Interval i2) {
14+
return i1.start - i2.start;
15+
}
16+
});
17+
18+
stack.push(a[0]);
19+
}
20+
21+
public static void main(String[] args) {
22+
Interval a[] = new Interval[4];
23+
24+
}
25+
}
26+
27+
/**
28+
* Interval
29+
*/
30+
class Interval {
31+
32+
int start, end;
33+
34+
Interval(int start, int end) {
35+
this.start = start;
36+
this.end = end;
37+
}
38+
}

arrays/KadanesAlgorithm.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public static void main(String[] args) {
1010
@SuppressWarnings("resource")
1111
Scanner sc = new Scanner(System.in);
1212
int n = sc.nextInt();
13-
int a[]=new int[n];
13+
int a[] = new int[n];
1414

1515
for(int i=0;i<n;i++)
1616
a[i] = sc.nextInt();

backtracking/Sudoku_Solver.java

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package backtracking;
2+
3+
public class Sudoku_Solver {
4+
5+
static int N = 9;
6+
7+
static boolean solveSudoku(int grid[][], int row, int col) {
8+
if (row == N - 1 && col == N)
9+
return true;
10+
11+
if (col == N) {
12+
row++;
13+
col = 0;
14+
}
15+
16+
if (grid[row][col] != 0)
17+
return solveSudoku(grid, row, col + 1);
18+
19+
for (int num = 1; num < 10; num++) {
20+
21+
if (isSafe(grid, row, col, num)) {
22+
23+
grid[row][col] = num;
24+
if (solveSudoku(grid, row, col + 1))
25+
return true;
26+
}
27+
grid[row][col] = 0;
28+
}
29+
return false;
30+
}
31+
32+
static void print(int[][] grid) {
33+
for (int i = 0; i < N; i++) {
34+
for (int j = 0; j < N; j++)
35+
System.out.println(grid[i][j] + " ");
36+
System.err.println();
37+
}
38+
}
39+
40+
static boolean isSafe(int[][] grid, int row, int col, int num) {
41+
for (int x = 0; x <= 8; x++) {
42+
if (grid[row][x] == num)
43+
return false;
44+
}
45+
46+
for (int x = 0; x <= 8; x++) {
47+
if (grid[x][col] == num)
48+
return false;
49+
}
50+
51+
int startRow = row - row % 3, startCol = col - col % 3;
52+
for (int i = 0; i < 3; i++) {
53+
for (int j = 0; j < 3; j++) {
54+
if (grid[i + startRow][j + startCol] == num)
55+
return false;
56+
}
57+
}
58+
59+
return true;
60+
}
61+
62+
public static void main(String[] args) {
63+
int grid[][] = { { 3, 0, 6, 5, 0, 8, 4, 0, 0 }, { 5, 2, 0, 0, 0, 0, 0, 0, 0 }, { 0, 8, 7, 0, 0, 0, 0, 8, 0 },
64+
{ 0, 0, 3, 0, 1, 3, 0, 0, 5 }, { 9, 0, 0, 8, 6, 0, 6, 0, 0 }, { 0, 5, 0, 0, 9, 0, 6, 0, 0 },
65+
{ 1, 3, 0, 0, 0, 0, 2, 5, 0 }, { 0, 0, 0, 0, 0, 0, 0, 7, 4 }, { 0, 0, 5, 2, 0, 6, 3, 0, 0 }, };
66+
67+
if (solveSudoku(grid, 0, 0))
68+
print(grid);
69+
else
70+
System.out.println("No Solution exists");
71+
}
72+
}

stack/Postfix.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public static void main(String[] args) {
7070
Scanner sc = new Scanner(System.in);
7171
String exp = sc.nextLine();
7272
sc.close();
73-
73+
@SuppressWarnings("unused")
7474
Postfix p = new Postfix();
7575
System.out.println(infixToPostfix(exp));
7676
}

t/trees/BST_Deletion.java

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
package trees;
2+
3+
//Java program to demonstrate delete operation in binary search tree
4+
class BST_Deletion{
5+
6+
// Class to make a Node
7+
class Node {
8+
int key;
9+
Node left, right;
10+
11+
public Node(int item) {
12+
key = item;
13+
left = right = null;
14+
}
15+
}
16+
17+
// Root of BST
18+
Node root;
19+
20+
// Constructor
21+
BST_Deletion() {
22+
root = null;
23+
}
24+
25+
// This method mainly calls deleteRec()
26+
void deleteKey(int key) {
27+
root = deleteRec(root, key);
28+
}
29+
30+
/* A recursive function to insert a new key in BST */
31+
Node deleteRec(Node root, int key) {
32+
/* Base Case: If the tree is empty */
33+
if (root == null) return root;
34+
35+
/* Otherwise, recur down the tree */
36+
if (key < root.key)
37+
root.left = deleteRec(root.left, key);
38+
else if (key > root.key)
39+
root.right = deleteRec(root.right, key);
40+
41+
// if key is same as root's key, then This is the node
42+
// to be deleted
43+
else
44+
{
45+
// node with only one child or no child
46+
if (root.left == null)
47+
return root.right;
48+
else if (root.right == null)
49+
return root.left;
50+
51+
// node with two children: Get the inorder successor (smallest
52+
// in the right subtree)
53+
root.key = minValue(root.right);
54+
55+
// Delete the inorder successor
56+
root.right = deleteRec(root.right, root.key);
57+
}
58+
59+
return root;
60+
}
61+
62+
int minValue(Node root)
63+
{
64+
int minv = root.key;
65+
while (root.left != null)
66+
{
67+
minv = root.left.key;
68+
root = root.left;
69+
}
70+
return minv;
71+
}
72+
73+
// This method mainly calls insertRec()
74+
void insert(int key)
75+
{
76+
root = insertRec(root, key);
77+
}
78+
79+
/* A recursive function to insert a new key in BST */
80+
Node insertRec(Node root, int key)
81+
{
82+
83+
/* If the tree is empty, return a new node */
84+
if (root == null)
85+
{
86+
root = new Node(key);
87+
return root;
88+
}
89+
90+
/* Otherwise, recur down the tree */
91+
if (key < root.key)
92+
root.left = insertRec(root.left, key);
93+
else if (key > root.key)
94+
root.right = insertRec(root.right, key);
95+
96+
/* return the (unchanged) node pointer */
97+
return root;
98+
}
99+
100+
// This method mainly calls InorderRec()
101+
void inorder()
102+
{
103+
inorderRec(root);
104+
}
105+
106+
// A utility function to do inorder traversal of BST
107+
void inorderRec(Node root)
108+
{
109+
if (root != null)
110+
{
111+
inorderRec(root.left);
112+
System.out.print(root.key + " ");
113+
inorderRec(root.right);
114+
}
115+
}
116+
117+
// Driver Program to test above functions
118+
public static void main(String[] args)
119+
{
120+
BST_Deletion tree = new BST_Deletion();
121+
122+
/* Let us create following BST
123+
50
124+
/ \
125+
30 70
126+
/ \ / \
127+
20 40 60 80 */
128+
129+
tree.insert(50);
130+
tree.insert(30);
131+
tree.insert(20);
132+
tree.insert(40);
133+
tree.insert(70);
134+
tree.insert(60);
135+
tree.insert(80);
136+
137+
System.out.println("Inorder traversal of the given tree");
138+
tree.inorder();
139+
140+
System.out.println("\nDelete 20");
141+
tree.deleteKey(20);
142+
143+
System.out.println("Inorder traversal of the modified tree");
144+
tree.inorder();
145+
146+
System.out.println("\nDelete 30");
147+
tree.deleteKey(30);
148+
149+
System.out.println("Inorder traversal of the modified tree");
150+
tree.inorder();
151+
152+
System.out.println("\nDelete 50");
153+
tree.deleteKey(50);
154+
155+
System.out.println("Inorder traversal of the modified tree");
156+
tree.inorder();
157+
}
158+
}

0 commit comments

Comments
 (0)