1. **Sorting Algorithms**:
- Bubble Sort
- Insertion Sort
- Selection Sort
- Merge Sort (Divide and Conquer)
- QuickSort (Divide and Conquer)
2. **Searching Algorithms**:
- Linear Search
- Binary Search
3. **Graph Traversal Techniques**:
- Depth-First Search (DFS)
`java
void dfs(int node, boolean[] visited) {
if (!visited[node]) {
// Process the current node
for (int neighbor : adjList.get(node)) {
dfs(neighbor, visited);
}
}
}
`
- Breadth-First Search (BFS)
`java
void bfs(int start) {
Queue
boolean[] visited = new boolean[numVertices];
visited[start] = true;
queue.add(start);
while (!queue.isEmpty()) {
int current = queue.poll();
// Process the current node
for (int neighbor : adjList.get(current)) {
if (!visited[neighbor]) {
visited[neighbor] = true;
queue.add(neighbor);
}
}
}
}
4. **Dynamic Programming**:
- Memoization
- Tabulation
5. **Greedy Algorithms**:
- Fractional Knapsack Problem
- Minimum Spanning Tree (Kruskal's and Prim's)
`java
class Edge implements Comparable
int src, dest, weight;
public int compareTo(Edge other) {
return this.weight - other.weight;
}
}
void kruskalMST(int V, Edge[] edges) {
Arrays.sort(edges);
int parent[] = new int[V];
makeSet(parent);
}
void union(int x, int y, Edge e) {
// Implement the logic to unite sets
}
6. **Divide and Conquer**:
- Binary Search (as mentioned earlier)
- Merge Sort
7. **Backtracking**:
- N-Queens Problem
`java
boolean solveNQUtil(int board[][], int col) {
if (col >= n)
return true;
for (int i = 0; i < N; i++) {
// If it is safe to place this queen in board[i][col], then
if (isSafe(board, i, col)) {
/* Place the Queen */
board[i][col] = 1;
// recur to place rest of queens
if (solveNQUtil(board, col + 1))
return true;
}
/* If placing queen in board[i][col] doesn't lead to a solution
then remove queen from board[i][col] */
board[i][j]=0;
}
/* If the Queen can not be placed in any row in this column col then return false */
}
`
8. **Sliding Window**:
- Maximum Subarray Sum
`java
int maxSubArraySum(int a[],int size) {
int max_so_far = Integer.MIN_VALUE,
curr_max = 0;
for (int i=0; i < size; i++)
{
curr_max += a[i];
if(max_so_far < curr_max)
max_so_far = curr_max;
/* If current sum becomes negative, then change it to 0*/
if(curr_max < 0)
curr_max=0;
}
return max_so_far;
}
9. **Two Pointers**:
- Two Sum Problem
`java
int[] twoSum(int nums[],int target) {
HashMap
for (int i=0; i if(map.containsKey(target- nums[i])) return new int[] { map.get(target - nums[i]),i }; else map.put(nums[i], i); throw new IllegalArgumentException("No two sum solution"); } 10. **Bit Manipulation**: - Binary Representation and Operations By mastering these patterns, you'll be well-equipped to tackle a wide range of HackerRank problems efficiently.
`
Login to Continue, We will bring you back to this content 0