Skip to content

Commit 54e0890

Browse files
committed
#Modification 70
1 parent 0b9f82f commit 54e0890

6 files changed

Lines changed: 127 additions & 43 deletions

File tree

arrays/Majority_Elements._linear.java

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,30 @@
11
package arrays;
2+
import java.util.*;
3+
24
// Find the majority element in java, B
35
class Majority_Elements_linear {
4-
private static void findMajority(int[] arr)
5-
{
6-
HashMap<Integer,Integer> map = new HashMap<Integer, Integer>();
76

8-
for(int i = 0; i < arr.length; i++) {
9-
if (map.containsKey(arr[i])) {
10-
int count = map.get(arr[i]) +1;
11-
if (count > arr.length /2) {
12-
System.out.println("Majority found :- " + arr[i]);
7+
private static void findMajority(int[] arr) {
8+
HashMap<Integer,Integer> map = new HashMap<>();
9+
10+
for (int j : arr) {
11+
if (map.containsKey(j)) {
12+
int count = map.get(j) + 1;
13+
if (count > arr.length / 2) {
14+
System.out.println("Majority found :- " + j);
1315
return;
1416
} else
15-
map.put(arr[i], count);
16-
17-
}
18-
else
19-
map.put(arr[i],1);
17+
map.put(j, count);
18+
} else
19+
map.put(j, 1);
2020
}
2121
System.out.println(" No Majority element");
2222
}
2323

2424

2525
/* Driver program to test the above functions */
26-
public static void main(String[] args)
27-
{
28-
int a[] = new int[]{2,2,2,2,5,5,2,3,3};
26+
public static void main(String[] args) {
27+
int[] a = new int[] {2,2,2,2,5,5,2,3,3};
2928

3029
findMajority(a);
3130
}

graphs/Graph_Problem_01_i.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ static void printGraph(ArrayList<ArrayList<Integer>> adj) {
1515

1616
for(int i = 0; i < adj.size(); i++) {
1717

18-
System.out.println("\n Adjacency list of vertex" + i);
18+
System.out.println("\nAdjacency list of vertex " + i);
1919
System.out.print("head");
2020

2121
for(int j = 0; j < adj.get(i).size(); j++)
@@ -30,10 +30,10 @@ public static void main(String[] args) {
3030

3131
// Creating a graph with 5 vertices
3232
int V = 5;
33-
ArrayList<ArrayList<Integer>> adj = new ArrayList<ArrayList<Integer>>(V);
33+
ArrayList<ArrayList<Integer>> adj = new ArrayList<>(V);
3434

3535
for(int i = 0; i < V; i++)
36-
adj.add(new ArrayList<Integer>());
36+
adj.add(new ArrayList<>());
3737

3838
// Adding edges one by one
3939
addEdge(adj, 0, 1);

graphs/Graph_Problem_01_ii.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@
33

44
public class Graph_Problem_01_ii {
55

6-
private LinkedList<Integer> adj[];
7-
8-
@SuppressWarnings("unchecked")
6+
private final LinkedList<Integer>[] adj;
7+
98
public Graph_Problem_01_ii(int v) {
109

1110
//array of Linked List
12-
adj = new LinkedList[v];
11+
adj = new LinkedList[v];
1312

1413
for(int i = 0; i < v; i++)
15-
adj[i] = new LinkedList<Integer>();
14+
adj[i] = new LinkedList<>();
1615
}
1716

1817
public void addEdge(int source, int destination) {
@@ -32,7 +31,7 @@ public static void main(String[] args) {
3231

3332
System.out.println("Enter " + e + " edges");
3433

35-
for(int i=0;i<e;i++) {
34+
for(int i = 0; i < e; i++) {
3635
int source = sc.nextInt();
3736
int destination = sc.nextInt();
3837
graph_Problem_01_ii.addEdge(source, destination);

graphs/Graph_Problem_02.java

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package graphs;
22
import java.util.*;
33

4-
/*
5-
* Problem Title :-> Implement BFS Algorithm of Traversal of Graph.
6-
*/
4+
// Problem Title -> Implement BFS Algorithm of Traversal of Graph.
75
public class Graph_Problem_02 {
86
// No. of vertices.
9-
private int V;
7+
private final int V;
8+
109
// Adjacency List
11-
private LinkedList<Integer>[] adj;
10+
private final LinkedList<Integer>[] adj;
1211

1312
@SuppressWarnings({ "unchecked", "rawtypes" })
1413
// Constructor
@@ -26,13 +25,16 @@ void addEdge(int v, int w) {
2625

2726
// prints BFS traversal from a given source s
2827
void BFS(int s) {
29-
// Mark all the verices as not visited(By Default set as false)
30-
boolean visited[] = new boolean[V];
28+
// Mark all the vertices as not visited(By Default set as false)
29+
boolean[] visited = new boolean[V];
3130

3231
// Create a queue for BFS
3332
LinkedList<Integer> queue = new LinkedList<>();
34-
33+
34+
// mark visited array as true
3535
visited[s] = true;
36+
37+
// add it to queue
3638
queue.add(s);
3739

3840
while(queue.size() != 0) {
@@ -41,12 +43,9 @@ void BFS(int s) {
4143
System.out.println(s + " ");
4244

4345
// Get all adjacent vertices of the dequeued vertex s.
44-
// If a adjacent has not been visited,
45-
// then mark it visited & enqueue it.
46-
Iterator<Integer> i = adj[s].listIterator();
47-
while(i.hasNext()) {
48-
int n = i.next();
49-
if(!visited[n]) {
46+
for (int n : adj[s]) {
47+
// If an adjacent has not been visited, then mark it visited & enqueue it.
48+
if (!visited[n]) {
5049
visited[n] = true;
5150
queue.add(n);
5251
}
@@ -55,21 +54,25 @@ void BFS(int s) {
5554
}
5655

5756
public static void main(String[] args) {
58-
57+
58+
// Taking input
5959
System.out.println("Enter number of vertices and edges");
6060
Scanner sc = new Scanner(System.in);
6161

6262
int v = sc.nextInt();
6363
int e = sc.nextInt();
64-
64+
int s = sc.nextInt();
65+
66+
// invoking the class by making object
6567
Graph_Problem_02 g = new Graph_Problem_02(v);
66-
68+
6769
for(int i = 0; i < e; i++) {
6870
v = sc.nextInt();
6971
e = sc.nextInt();
7072
g.addEdge(v, e);
7173
}
72-
74+
7375
sc.close();
76+
g.BFS(s);
7477
}
7578
}

miscellaneous/BFS.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package miscellaneous;
2+
3+
import java.util.LinkedList;
4+
5+
public class BFS {
6+
private final int V;
7+
8+
private final LinkedList<Integer>[] adj;
9+
10+
// constructor
11+
BFS(int v){
12+
V = v;
13+
adj = new LinkedList[v];
14+
for(int i = 0; i < v; ++i)
15+
adj[i] = new LinkedList();
16+
}
17+
18+
public static void main(String[] args) {
19+
20+
}
21+
}

ssp/ssp/Problem_1.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package ssp.ssp;
2+
3+
// Problem TItle => Implement stack from scratch
4+
public class Problem_1 {
5+
public static void main(String[] args) {
6+
7+
}
8+
}
9+
10+
/**
11+
* Stack
12+
*/
13+
class Stack {
14+
static final int MAX = 100;
15+
int top;
16+
int[] a = new int[MAX];
17+
18+
Stack() {
19+
top = -1;
20+
}
21+
22+
boolean isEmpty() {
23+
return (top < 0);
24+
}
25+
26+
boolean pop(int x) {
27+
if (top >= (MAX - 1)) {
28+
System.out.println("Stack Overflow");
29+
return false;
30+
} else {
31+
a[++top] = x;
32+
System.out.println(x + " pushed into stack");
33+
return true;
34+
}
35+
}
36+
37+
int push() {
38+
if (top < 0) {
39+
System.out.println("Stack Underflow");
40+
return 0;
41+
} else {
42+
int x = a[top--];
43+
return x;
44+
}
45+
}
46+
47+
int peek() {
48+
if (top < 0) {
49+
System.out.println("Stack Underflow");
50+
return 0;
51+
} else {
52+
int x = a[top];
53+
return x;
54+
}
55+
}
56+
57+
void print() {
58+
for (int i = top; i > -1; i--) {
59+
System.out.print(" " + a[i]);
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)