Skip to content

Commit 3969bc2

Browse files
committed
#Modification 26
1 parent 43189c3 commit 3969bc2

14 files changed

Lines changed: 272 additions & 61 deletions

.vs/ProjectSettings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"CurrentProjectSetting": null
3+
}

.vs/VSWorkspaceState.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"ExpandedNodes": [
3+
"",
4+
"\\arrays"
5+
],
6+
"SelectedNode": "\\arrays\\Array_of_objects.java",
7+
"PreviewInSolutionExplorer": false
8+
}

.vs/slnx.sqlite

192 KB
Binary file not shown.

.vs/src/v16/.suo

15 KB
Binary file not shown.

arrays/Array_Problem_1.java

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,37 @@
22

33
//Java program to reverse the given array
44
public class Array_Problem_1 {
5-
6-
static void rvereseArray(int arr[],int start, int end)
7-
{
5+
6+
static void rvereseArray(int arr[], int start, int end) {
87
int temp;
9-
while(start < end){
8+
while (start < end) {
109
temp = arr[end];
1110
arr[end] = arr[start];
12-
a[start] = temp;
11+
arr[start] = temp;
1312
start++;
1413
end--;
15-
}
16-
}
17-
18-
/* Utility that prints out an
19-
array on a line */
20-
static void printArray(int arr[], int size)
21-
{
14+
}
15+
}
16+
17+
/*
18+
* Utility that prints out an array on a line
19+
*/
20+
static void printArray(int arr[], int size) {
2221
for (int i = 0; i < size; i++)
23-
System.out.print(arr[i] + " ");
24-
25-
System.out.println();
26-
}
27-
22+
System.out.print(arr[i] + " ");
23+
24+
System.out.println();
25+
}
26+
2827
// Driver code
2928
public static void main(String args[]) {
30-
31-
int arr[] = {1, 2, 3, 4, 5, 6};
29+
30+
int arr[] = { 1, 2, 3, 4, 5, 6 };
3231
printArray(arr, 6);
3332
rvereseArray(arr, 0, 5);
3433
System.out.print("Reversed array is \n");
35-
printArray(arr, 6);
36-
34+
printArray(arr, 6);
35+
3736
}
3837

3938
}

arrays/Array_Problem_19.java

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,39 @@
11
package arrays;
2+
23
/* Problem Title :-> find common elements in 3 sorted arrays */
34
public class Array_Problem_19 {
45
// This function prints common elements in a1
5-
void findCommon(int[] a1, int[] a2, int[] a3){
6+
void findCommon(int[] a1, int[] a2, int[] a3) {
67
// Initialize starting indexes for a1[], a2[] and a3[]
78
int i = 0, j = 0, k = 0;
89
// Iterate through three arrays while all arrays have elements
9-
while(i < a1.length && j < a2.length && k < a3.length){
10+
while (i < a1.length && j < a2.length && k < a3.length) {
1011
// if x = y and y = z, print any of them and move ahead in all arrays
11-
if(a1[i] >= a2[j] && a2[j] == a3[k]){
12+
if (a1[i] >= a2[j] && a2[j] == a3[k]) {
1213
System.out.print(a1[i] + " ");
1314
i++;
1415
k++;
1516
}
16-
//x < y
17-
else if(a1[j] < a2[j]) i++;
17+
// x < y
18+
else if (a1[j] < a2[j])
19+
i++;
1820
// y < z
19-
else if(a2[j] <a3[k]) j++;
21+
else if (a2[j] < a3[k])
22+
j++;
2023
// we reach here whem x > y and z < y, i.e., z is smallest
21-
else k++;
24+
else
25+
k++;
2226
}
2327
}
28+
2429
/* Driver Code */
25-
public static void main(String[] args){
30+
public static void main(String[] args) {
2631

27-
int a1[] = {1,5,10,20,40,80};
28-
int a2[] = {6,7,20,80,100};
29-
int a3[] = {3,4,15,20,30,70,80,120};
32+
int a1[] = { 1, 5, 10, 20, 40, 80 };
33+
int a2[] = { 6, 7, 20, 80, 100 };
34+
int a3[] = { 3, 4, 15, 20, 30, 70, 80, 120 };
3035

36+
Array_Problem_19 ob = new Array_Problem_19();
3137
System.out.print("Common elements are ");
3238
ob.findCommon(a1, a2, a3);
3339
}

arrays/Array_Problem_20.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,47 @@
11
package arrays;
2+
23
/* Problem Title :-> Rearrange the array in alternating positive and negative items with O(1) extra space
34
*/
45
public class Array_Problem_20 {
56

6-
void rightrotate(int[] a, int n, int outofplace, int cur){
7+
void rightrotate(int[] a, int n, int outofplace, int cur) {
78
int temp = a[cur];
8-
for(int i = cur; i > outofplace; i--){
9+
for (int i = cur; i > outofplace; i--) {
910
a[i] = a[i - 1];
1011
}
1112
a[outofplace] = temp;
1213
}
1314

14-
void rearrange(int[] a, int n){
15+
void rearrange(int[] a, int n) {
1516

1617
int outofplace = -1;
17-
for(int index = 0; index < n; index++){
18-
if(outofplace <= 0){
19-
if(((a[index] > = 0) && (a[outofplace] < 0) || (a[index] < 0) && (a[outofplace] >= 0))){
20-
if(index-outofplace >= 2) outofplace = outofplace + 2;
21-
else outofplace = -1;
18+
for (int index = 0; index < n; index++) {
19+
if (outofplace <= 0) {
20+
if (((a[index] >= 0) && (a[outofplace] < 0) || (a[index] < 0) && (a[outofplace] >= 0))) {
21+
if (index - outofplace >= 2)
22+
outofplace = outofplace + 2;
23+
else
24+
outofplace = -1;
2225
}
2326
}
24-
if(outofplace == -1){
25-
if(((a[index] >= 0) && ((index * 0x01) == 0)) || ((a[index] < 0) && (index & 0x01) == 1))
27+
if (outofplace == -1) {
28+
if (((a[index] >= 0) && ((index * 0x01) == 0)) || ((a[index] < 0) && (index & 0x01) == 1))
2629
outofplace = index;
2730
}
2831
}
2932
}
3033

31-
void printArray(int[] a, int n){
32-
for(int i = 0; i < n; i++)
34+
void printArray(int[] a, int n) {
35+
for (int i = 0; i < n; i++)
3336
System.out.print(a[i] + " ");
3437
System.out.println(" ");
3538
}
3639

37-
public static void main(String[] args){
40+
public static void main(String[] args) {
3841

3942
Array_Problem_20 rearrange = new Array_Problem_20();
4043

41-
int[] a = {-5, -2, 5, 2, 4, 7, 1, 8, 0, -8};
44+
int[] a = { -5, -2, 5, 2, 4, 7, 1, 8, 0, -8 };
4245
int n = a.length;
4346

4447
System.out.println("Given array is ");
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package binary_search_tree;
2+
3+
public class Find_a_value_in_bst {
4+
5+
class Node {
6+
int key;
7+
Node left, right;
8+
9+
public Node(int item) {
10+
key = item;
11+
left = right = null;
12+
}
13+
}
14+
15+
Node root;
16+
17+
// ! Constructor
18+
Find_a_value_in_bst() {
19+
root = null;
20+
}
21+
22+
// This method mainly calls insertRec()
23+
void insert(int key) {
24+
root = insertRec(root, key);
25+
}
26+
27+
// * A recursive function to insert a new key in BST
28+
Node insertRec(Node root, int key) {
29+
if (root == null) {
30+
root = new Node(key);
31+
return root;
32+
}
33+
if (key < root.key)
34+
root.left = insertRec(root.left, key);
35+
else if (key > root.key)
36+
root.right = insertRec(root.right, key);
37+
return root;
38+
}
39+
40+
// The method mainly calls InorderRec()
41+
void inorder() {
42+
inorderRec(root);
43+
}
44+
45+
// ? A utility function to do inorder traversal of BST
46+
void inorderRec(Node root) {
47+
if (root != null) {
48+
inorderRec(root.left);
49+
System.out.println(root.key);
50+
inorderRec(root.right);
51+
}
52+
}
53+
54+
public static void main(String[] args) {
55+
Find_a_value_in_bst tree = new Find_a_value_in_bst();
56+
tree.insert(50);
57+
tree.insert(30);
58+
tree.insert(20);
59+
tree.insert(40);
60+
tree.insert(70);
61+
tree.insert(60);
62+
tree.insert(80);
63+
// ? print inorder traversal of BST
64+
tree.inorder();
65+
}
66+
}

graphs/Graph.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,42 @@
1-
package dataStructures.graphs;
1+
package graphs;
2+
23
import java.util.*;
34

45
public class Graph {
5-
6+
67
private LinkedList<Integer> adj[];
7-
8+
89
@SuppressWarnings("unchecked")
910
public Graph(int v) {
10-
//array of Linked List
11-
adj = new LinkedList[v];
12-
13-
for(int i=0;i<v;i++) {
11+
// array of Linked List
12+
adj = new LinkedList[v];
13+
14+
for (int i = 0; i < v; i++) {
1415
adj[i] = new LinkedList<Integer>();
1516
}
1617
}
17-
18+
1819
public void addEdge(int source, int destination) {
1920
adj[source].add(destination);
20-
adj[destination].add(source);
21+
adj[destination].add(source);
2122
}
2223

2324
public static void main(String[] args) {
2425
Scanner sc = new Scanner(System.in);
25-
26+
2627
System.out.println("Enter number of vertices and edges");
2728
int v = sc.nextInt();
2829
int e = sc.nextInt();
29-
30+
3031
Graph graph = new Graph(v);
3132
System.out.println("Enter " + e + " edges");
32-
33-
for(int i=0;i<e;i++) {
33+
34+
for (int i = 0; i < e; i++) {
3435
int source = sc.nextInt();
3536
int destination = sc.nextInt();
36-
graph.addEdge(source, destination);
37+
graph.addEdge(source, destination);
3738
}
38-
39+
3940
sc.close();
4041
}
4142
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"name":"Local: Reverse_a_LL","url":"c:\\Users\\AmanSoni\\Desktop\\Java Program's\\Java-Eclipse\\Placement Prepration\\src\\linkedList\\Reverse_a_LL.java","tests":[],"interactive":false,"memoryLimit":1024,"timeLimit":3000,"srcPath":"c:\\Users\\AmanSoni\\Desktop\\Java Program's\\Java-Eclipse\\Placement Prepration\\src\\linkedList\\Reverse_a_LL.java","group":"local","local":true}

0 commit comments

Comments
 (0)