From 4fb4fd3b6a88d96194eabe593697162d25cf3584 Mon Sep 17 00:00:00 2001 From: Abhinav kumar <126642111+Abhinavcode13@users.noreply.github.com> Date: Thu, 24 Aug 2023 07:49:05 +0530 Subject: [PATCH 01/22] Create Graph_Dijstra.java --- Graphs/Graph_Dijstra.java | 79 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 Graphs/Graph_Dijstra.java diff --git a/Graphs/Graph_Dijstra.java b/Graphs/Graph_Dijstra.java new file mode 100644 index 00000000..f40ac596 --- /dev/null +++ b/Graphs/Graph_Dijstra.java @@ -0,0 +1,79 @@ +import java.util.*; + +class Graph { + private int V; // Number of vertices + private List> adj; // Adjacency list + + public Graph(int V) { + this.V = V; + adj = new ArrayList<>(V); + for (int i = 0; i < V; i++) { + adj.add(new ArrayList<>()); + } + } + + // Add an edge to the graph + public void addEdge(int source, int destination, int weight) { + Node node = new Node(destination, weight); + adj.get(source).add(node); + } + + public void dijkstra(int source) { + int[] distance = new int[V]; + Arrays.fill(distance, Integer.MAX_VALUE); + distance[source] = 0; + + PriorityQueue pq = new PriorityQueue<>(V, Comparator.comparingInt(node -> node.weight)); + pq.add(new Node(source, 0)); + + while (!pq.isEmpty()) { + int u = pq.poll().vertex; + + for (Node neighbor : adj.get(u)) { + int v = neighbor.vertex; + int w = neighbor.weight; + + if (distance[u] != Integer.MAX_VALUE && distance[u] + w < distance[v]) { + distance[v] = distance[u] + w; + pq.add(new Node(v, distance[v])); + } + } + } + + // Print the shortest distances from the source + System.out.println("Shortest distances from source vertex " + source + ":"); + for (int i = 0; i < V; i++) { + System.out.println("Vertex " + i + ": " + distance[i]); + } + } + + private static class Node { + int vertex; + int weight; + + Node(int vertex, int weight) { + this.vertex = vertex; + this.weight = weight; + } + } +} + +public class DijkstraAlgorithm { + public static void main(String[] args) { + int V = 6; // Number of vertices + Graph graph = new Graph(V); + + // Add edges and their weights + graph.addEdge(0, 1, 2); + graph.addEdge(0, 2, 4); + graph.addEdge(1, 2, 1); + graph.addEdge(1, 3, 7); + graph.addEdge(2, 4, 3); + graph.addEdge(3, 4, 1); + graph.addEdge(3, 5, 5); + graph.addEdge(4, 5, 2); + + int source = 0; // Source vertex + graph.dijkstra(source); + } +} From 1e68a1a18baa00d544b614d3be1fe07387555fee Mon Sep 17 00:00:00 2001 From: Abhinav kumar <126642111+Abhinavcode13@users.noreply.github.com> Date: Fri, 25 Aug 2023 19:53:32 +0530 Subject: [PATCH 02/22] Create Basic_operations.py closes #1661 --- Math/Basic_operations.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 Math/Basic_operations.py diff --git a/Math/Basic_operations.py b/Math/Basic_operations.py new file mode 100644 index 00000000..e4d8cf96 --- /dev/null +++ b/Math/Basic_operations.py @@ -0,0 +1,34 @@ +def divide(dividend, divisor): + if divisor == 0: + raise ZeroDivisionError("Division by zero") + if dividend == 0: + return 0 + + negative = (dividend < 0) ^ (divisor < 0) + dividend = abs(dividend) + divisor = abs(divisor) + + quotient = 0 + while dividend >= divisor: + dividend -= divisor + quotient += 1 + + if negative: + quotient = -quotient + + INT_MAX = 2**31 - 1 + INT_MIN = -2**31 + if quotient > INT_MAX: + return INT_MAX + elif quotient < INT_MIN: + return INT_MIN + else: + return quotient + +# Take user input for dividend and divisor +dividend = int(input("Enter the dividend: ")) +divisor = int(input("Enter the divisor: ")) + +# Perform the division and print the result +result = divide(dividend, divisor) +print(f"The result of {dividend} divided by {divisor} is: {result}") From 9f263782e0a1e084ca2909d63c52af94e69c241d Mon Sep 17 00:00:00 2001 From: Abhinav kumar <126642111+Abhinavcode13@users.noreply.github.com> Date: Sat, 26 Aug 2023 22:45:55 +0530 Subject: [PATCH 03/22] Create op.js --- Math/op.js | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 Math/op.js diff --git a/Math/op.js b/Math/op.js new file mode 100644 index 00000000..b4ec4442 --- /dev/null +++ b/Math/op.js @@ -0,0 +1,40 @@ +function divide(dividend, divisor) { + // Handle special cases + if (divisor === 0) { + throw new Error("Division by zero"); + } + if (dividend === 0) { + return 0; + } + + // Determine the sign of the result + const negativeResult = (dividend < 0) ^ (divisor < 0); + + // Make dividend and divisor positive for easier calculations + dividend = Math.abs(dividend); + divisor = Math.abs(divisor); + + let quotient = 0; + + while (dividend >= divisor) { + dividend -= divisor; + quotient++; + } + + if (negativeResult) { + quotient = -quotient; + } + + // Check for overflow + if (quotient < -(2**31) || quotient > 2**31 - 1) { + return 2**31 - 1; // Return INT_MAX for overflow + } + + return quotient; +} + +// Example usage: +const dividend = 10; +const divisor = 3; +const result = divide(dividend, divisor); +console.log(result); // Output should be 3 From 8d5806a8ec6e451571beb991d95d1300ba005da3 Mon Sep 17 00:00:00 2001 From: Abhinav kumar <126642111+Abhinavcode13@users.noreply.github.com> Date: Sun, 27 Aug 2023 09:32:13 +0530 Subject: [PATCH 04/22] Create Diljstra.go --- Graphs/Diljstra.go | 84 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 Graphs/Diljstra.go diff --git a/Graphs/Diljstra.go b/Graphs/Diljstra.go new file mode 100644 index 00000000..8802aa27 --- /dev/null +++ b/Graphs/Diljstra.go @@ -0,0 +1,84 @@ +package main + +import ( + "fmt" + "math" +) + +// Define a struct to represent a graph. +type Graph struct { + nodes map[string]map[string]float64 +} + +// Add an edge to the graph. +func (g *Graph) AddEdge(node1, node2 string, weight float64) { + if g.nodes == nil { + g.nodes = make(map[string]map[string]float64) + } + if g.nodes[node1] == nil { + g.nodes[node1] = make(map[string]float64) + } + if g.nodes[node2] == nil { + g.nodes[node2] = make(map[string]float64) + } + g.nodes[node1][node2] = weight + g.nodes[node2][node1] = weight // Assuming an undirected graph +} + +// Dijkstra's algorithm to find the shortest path. +func Dijkstra(graph Graph, startNode string) map[string]float64 { + distances := make(map[string]float64) + visited := make(map[string]bool) + + for node := range graph.nodes { + distances[node] = math.Inf(1) + } + + distances[startNode] = 0 + + for { + var closestNode string + var shortestDistance float64 = math.Inf(1) + + for node, distance := range distances { + if !visited[node] && distance < shortestDistance { + closestNode = node + shortestDistance = distance + } + } + + if closestNode == "" { + break + } + + visited[closestNode] = true + + for neighbor, weight := range graph.nodes[closestNode] { + if newDistance := distances[closestNode] + weight; newDistance < distances[neighbor] { + distances[neighbor] = newDistance + } + } + } + + return distances +} + +func main() { + // Create a graph. + g := Graph{} + g.AddEdge("A", "B", 1) + g.AddEdge("A", "C", 4) + g.AddEdge("B", "C", 2) + g.AddEdge("B", "D", 5) + g.AddEdge("C", "D", 1) + g.AddEdge("D", "E", 3) + g.AddEdge("E", "F", 2) + + // Find the shortest distances from node "A" to all other nodes. + shortestDistances := Dijkstra(g, "A") + + // Print the shortest distances. + for node, distance := range shortestDistances { + fmt.Printf("Shortest distance from A to %s: %v\n", node, distance) + } +} From b17534b19e3ca2e8d821cee0f45529450989f4fe Mon Sep 17 00:00:00 2001 From: Abhinav kumar <126642111+Abhinavcode13@users.noreply.github.com> Date: Mon, 28 Aug 2023 07:57:29 +0530 Subject: [PATCH 05/22] Create Count.go closes #499 --- Math/Count.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 Math/Count.go diff --git a/Math/Count.go b/Math/Count.go new file mode 100644 index 00000000..2a89980c --- /dev/null +++ b/Math/Count.go @@ -0,0 +1,20 @@ +class Solution { +public: + int countNumbersWithUniqueDigits(int n) { + if (n == 0) { + return 1; // There's only one number with 0 digits, which is 0. + } + + int result = 10; // For n = 1, there are 10 numbers with unique digits (0-9). + int uniqueDigits = 9; + int availableDigits = 9; + + for (int i = 2; i <= n && availableDigits > 0; i++) { + uniqueDigits *= availableDigits; + result += uniqueDigits; + availableDigits--; + } + + return result; + } +}; From 4bdc16ba40b57d98f0490ccf44b87bca6e94d793 Mon Sep 17 00:00:00 2001 From: Abhinav kumar <126642111+Abhinavcode13@users.noreply.github.com> Date: Wed, 30 Aug 2023 19:53:06 +0530 Subject: [PATCH 06/22] Create 2D_sorted.go closes #270 --- 2D Arrays (Matrix)/2D_sorted.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 2D Arrays (Matrix)/2D_sorted.go diff --git a/2D Arrays (Matrix)/2D_sorted.go b/2D Arrays (Matrix)/2D_sorted.go new file mode 100644 index 00000000..fbb74145 --- /dev/null +++ b/2D Arrays (Matrix)/2D_sorted.go @@ -0,0 +1,25 @@ +func searchMatrix(matrix [][]int, target int) bool { + if len(matrix) == 0 || len(matrix[0]) == 0 { + return false + } + + rows, cols := len(matrix), len(matrix[0]) + left, right := 0, rows*cols-1 + + for left <= right { + mid := left + (right-left)/2 + // Convert the 1D index back to 2D coordinates + row, col := mid/cols, mid%cols + midValue := matrix[row][col] + + if midValue == target { + return true + } else if midValue < target { + left = mid + 1 + } else { + right = mid - 1 + } + } + + return false +} From 9b077701cf27f305e7a1f560d81173a394fcbf7d Mon Sep 17 00:00:00 2001 From: Abhinav kumar <126642111+Abhinavcode13@users.noreply.github.com> Date: Fri, 1 Sep 2023 20:25:28 +0530 Subject: [PATCH 07/22] Create N_queen.js closes #365' --- Famous Algorithms/N_queen.js | 59 ++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 Famous Algorithms/N_queen.js diff --git a/Famous Algorithms/N_queen.js b/Famous Algorithms/N_queen.js new file mode 100644 index 00000000..b83980b3 --- /dev/null +++ b/Famous Algorithms/N_queen.js @@ -0,0 +1,59 @@ +function solveNQueens(n) { + const board = new Array(n).fill().map(() => new Array(n).fill('.')); // Create an empty NxN chessboard + + const solutions = []; + + function isSafe(row, col) { + // Check if no other queens are in the same column + for (let i = 0; i < row; i++) { + if (board[i][col] === 'Q') { + return false; + } + } + + // Check upper-left diagonal + for (let i = row, j = col; i >= 0 && j >= 0; i--, j--) { + if (board[i][j] === 'Q') { + return false; + } + } + + // Check upper-right diagonal + for (let i = row, j = col; i >= 0 && j < n; i--, j++) { + if (board[i][j] === 'Q') { + return false; + } + } + + return true; + } + + function solve(row) { + if (row === n) { + // Found a valid solution, push a copy of the board to the solutions array + solutions.push(board.map(row => row.join(''))); + return; + } + + for (let col = 0; col < n; col++) { + if (isSafe(row, col)) { + board[row][col] = 'Q'; // Place a queen + solve(row + 1); // Recursively move to the next row + board[row][col] = '.'; // Backtrack by removing the queen + } + } + } + + solve(0); // Start solving from the first row + + return solutions; +} + +// Example usage: +const n = 4; // Change this to the desired board size +const solutions = solveNQueens(n); + +console.log(`Solutions for ${n}-Queens:`); +for (const solution of solutions) { + console.log(solution); +} From 694f51ad70c43f0ad9905009a9ceb6027e507b45 Mon Sep 17 00:00:00 2001 From: Abhinav kumar <126642111+Abhinavcode13@users.noreply.github.com> Date: Sun, 3 Sep 2023 07:40:54 +0530 Subject: [PATCH 08/22] Create plaindrome_str.cpp #1373 closes --- Strings/plaindrome_str.cpp | 53 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 Strings/plaindrome_str.cpp diff --git a/Strings/plaindrome_str.cpp b/Strings/plaindrome_str.cpp new file mode 100644 index 00000000..874d80b3 --- /dev/null +++ b/Strings/plaindrome_str.cpp @@ -0,0 +1,53 @@ +#include +#include + +using namespace std; + +bool isPalindrome(string s, int start, int end) { + while (start < end) { + if (s[start] != s[end]) { + return false; + } + start++; + end--; + } + return true; +} + +vector> partitionPalindrome(string s) { + vector> result; + vector current; + + backtrack(s, 0, current, result); + + return result; +} + +void backtrack(string s, int start, vector& current, vector>& result) { + if (start == s.length()) { + result.push_back(current); + return; + } + + for (int end = start; end < s.length(); end++) { + if (isPalindrome(s, start, end)) { + current.push_back(s.substr(start, end - start + 1)); + backtrack(s, end + 1, current, result); + current.pop_back(); + } + } +} + +int main() { + string s = "aab"; + vector> partitions = partitionPalindrome(s); + + for (auto partition : partitions) { + for (string palindrome : partition) { + cout << palindrome << " "; + } + cout << endl; + } + + return 0; +} From 2e60ea6ec9bf4543f6d63b51ce08677f55563c57 Mon Sep 17 00:00:00 2001 From: Abhinav kumar <126642111+Abhinavcode13@users.noreply.github.com> Date: Mon, 4 Sep 2023 20:59:03 +0530 Subject: [PATCH 09/22] Create circle.cpp closes #448 --- Math/circle.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Math/circle.cpp diff --git a/Math/circle.cpp b/Math/circle.cpp new file mode 100644 index 00000000..4e2567dc --- /dev/null +++ b/Math/circle.cpp @@ -0,0 +1,28 @@ +class Solution { + public int[] countPoints(int[][] points, int[][] queries) { + int[] answer = new int[queries.length]; + + for (int j = 0; j < queries.length; j++) { + int centerX = queries[j][0]; + int centerY = queries[j][1]; + int radius = queries[j][2]; + + for (int i = 0; i < points.length; i++) { + int pointX = points[i][0]; + int pointY = points[i][1]; + + // Check if the point (pointX, pointY) is inside the circle. + if (isInsideCircle(pointX, pointY, centerX, centerY, radius)) { + answer[j]++; + } + } + } + + return answer; + } + + private boolean isInsideCircle(int x, int y, int centerX, int centerY, int radius) { + int distanceSquared = (x - centerX) * (x - centerX) + (y - centerY) * (y - centerY); + return distanceSquared <= radius * radius; + } +} From 71760a594078603726fd10032bcd04067057e2d7 Mon Sep 17 00:00:00 2001 From: Abhinav kumar <126642111+Abhinavcode13@users.noreply.github.com> Date: Thu, 7 Sep 2023 21:32:50 +0530 Subject: [PATCH 10/22] Create Maximum_freq.py #292 --- Arrays/Maximum_freq.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 Arrays/Maximum_freq.py diff --git a/Arrays/Maximum_freq.py b/Arrays/Maximum_freq.py new file mode 100644 index 00000000..d0868caf --- /dev/null +++ b/Arrays/Maximum_freq.py @@ -0,0 +1,25 @@ +class Solution { + public int majorityElement(int[] nums) { + int candidate = nums[0]; // Initialize the candidate as the first element + int count = 1; // Initialize the count of the current candidate as 1 + + // Iterate through the array starting from the second element + for (int i = 1; i < nums.length; i++) { + if (nums[i] == candidate) { + // If the current element is the same as the candidate, increment the count + count++; + } else if (count > 0) { + // If the current element is different from the candidate and count is positive, + // decrement the count since we have a matching pair (candidate vs. current element) + count--; + } else { + // If count becomes zero, update the candidate to the current element and set count to 1 + candidate = nums[i]; + count = 1; + } + } + + // At the end, the candidate will be the majority element + return candidate; + } +} From 39f8faec2a4bec9f7d06e53161489c037ecde5ef Mon Sep 17 00:00:00 2001 From: Abhinav kumar <126642111+Abhinavcode13@users.noreply.github.com> Date: Sat, 9 Sep 2023 19:06:40 +0530 Subject: [PATCH 11/22] Create n_queen.cpp --- Backtracking/n_queen.cpp | 80 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 Backtracking/n_queen.cpp diff --git a/Backtracking/n_queen.cpp b/Backtracking/n_queen.cpp new file mode 100644 index 00000000..d681dda0 --- /dev/null +++ b/Backtracking/n_queen.cpp @@ -0,0 +1,80 @@ +#include +#include + +using namespace std; + +// Function to check if it's safe to place a queen at board[row][col] +bool isSafe(vector& board, int row, int col, int N) { + // Check the row on the left side + for (int i = 0; i < col; ++i) { + if (board[row][i] == 'Q') { + return false; + } + } + + // Check upper diagonal on the left side + for (int i = row, j = col; i >= 0 && j >= 0; --i, --j) { + if (board[i][j] == 'Q') { + return false; + } + } + + // Check lower diagonal on the left side + for (int i = row, j = col; i < N && j >= 0; ++i, --j) { + if (board[i][j] == 'Q') { + return false; + } + } + + return true; +} + +// Recursive function to solve N-Queens problem +bool solveNQueens(vector& board, int col, int N) { + if (col == N) { + // All queens are placed successfully + return true; + } + + for (int i = 0; i < N; ++i) { + if (isSafe(board, i, col, N)) { + // Place queen + board[i][col] = 'Q'; + + // Recur to place the rest of the queens + if (solveNQueens(board, col + 1, N)) { + return true; + } + + // If placing queen in board[i][col] doesn't lead to a solution, backtrack + board[i][col] = '.'; + } + } + + // If no queen can be placed in this column, return false + return false; +} + +// Function to solve N-Queens problem and print the solution +void solveNQueens(int N) { + vector board(N, string(N, '.')); + + if (solveNQueens(board, 0, N)) { + // Print the solution + for (int i = 0; i < N; ++i) { + cout << board[i] << endl; + } + } else { + cout << "No solution exists." << endl; + } +} + +int main() { + int N; + cout << "Enter the number of queens (N): "; + cin >> N; + + solveNQueens(N); + + return 0; +} From f5d0e5e27d3986e43ca71019025f1e18bf70304d Mon Sep 17 00:00:00 2001 From: Abhinav kumar <126642111+Abhinavcode13@users.noreply.github.com> Date: Sun, 10 Sep 2023 15:52:36 +0530 Subject: [PATCH 12/22] Create Rotatedarr.cpp --- Arrays/Rotatedarr.cpp | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 Arrays/Rotatedarr.cpp diff --git a/Arrays/Rotatedarr.cpp b/Arrays/Rotatedarr.cpp new file mode 100644 index 00000000..6d7ca494 --- /dev/null +++ b/Arrays/Rotatedarr.cpp @@ -0,0 +1,30 @@ +class Solution { +public: + int search(std::vector& nums, int target) { + int low = 0, high = nums.size() - 1; + + while (low <= high) { + int mid = (low + high) / 2; + + if (nums[mid] == target) { + return mid; + } + + if (nums[low] <= nums[mid]) { + if (nums[low] <= target && target < nums[mid]) { + high = mid - 1; + } else { + low = mid + 1; + } + } else { + if (nums[mid] < target && target <= nums[high]) { + low = mid + 1; + } else { + high = mid - 1; + } + } + } + + return -1; + } +}; From e4d0b0f79eb0f1098f79aa22ef146ee6a160372b Mon Sep 17 00:00:00 2001 From: Abhinav kumar <126642111+Abhinavcode13@users.noreply.github.com> Date: Mon, 11 Sep 2023 23:35:26 +0530 Subject: [PATCH 13/22] Create MaxConcatenatedstr.java --- Strings/MaxConcatenatedstr.java | 47 +++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 Strings/MaxConcatenatedstr.java diff --git a/Strings/MaxConcatenatedstr.java b/Strings/MaxConcatenatedstr.java new file mode 100644 index 00000000..4aef02bd --- /dev/null +++ b/Strings/MaxConcatenatedstr.java @@ -0,0 +1,47 @@ +class Solution { +public: + // i: arr[i] we are currently looking at + // mask: Bitmask that represents all the characters that have been added to the current string + // If 0-th bit in mask is set, it means that we have added "a" in the current string + int solve(vector &arr, int i, int mask) { + int n = arr.size(); + + if (i >= n) + return 0; + + // Skip concatenating arr[i] + int curRes = solve(arr, i+1, mask) + + // Mask to keep track of the characters that are present in arr[i] + int curMask = 0; + + // Check whether any character in arr[i] is present in current string, i.e. + // Check whether (arr[i]-'a')-th bit is set in mask + // If any existing character's bit is set, it means that we cannot concatenate arr[i] + // to the given string and so return curRes only which contains the result of skipping arr[i] + // Also, use curMask to maintain the characters in arr[i] that have been seen. + // It is possible that arr[i] itself has duplicate characters in which case, we will not be able to concatenate arr[i] + // So check whether (c-'a')-th bit is set in curMask and after that set the (c-'a')-th bit in curMask + for (char &c: arr[i]) { + if (mask & (1 << (c - 'a'))) + return curRes; + + if (curMask & (1 << (c - 'a'))) + return curRes; + + curMask |= (1 << (c - 'a')); + } + + // All the bits that were set in curMask will be now set in mask, + // in order to add all characters of arr[i] to the current string + mask |= curMask; + + // We make a call to i+1 with the updated mask and arr[i]'s length being added + curRes = max(curRes, (int) arr[i].length() + solve(arr, i+1, mask)); + return curRes; + } + + int maxLength(vector& arr) { + return solve(arr, 0, 0); + } +}; From 1430f6e244acb6f63859d7297f87c5f528755860 Mon Sep 17 00:00:00 2001 From: Abhinav kumar <126642111+Abhinavcode13@users.noreply.github.com> Date: Tue, 12 Sep 2023 23:41:08 +0530 Subject: [PATCH 14/22] Create MaxpathBinaryTree.cpp --- Trees/MaxpathBinaryTree.cpp | 60 +++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 Trees/MaxpathBinaryTree.cpp diff --git a/Trees/MaxpathBinaryTree.cpp b/Trees/MaxpathBinaryTree.cpp new file mode 100644 index 00000000..4183551e --- /dev/null +++ b/Trees/MaxpathBinaryTree.cpp @@ -0,0 +1,60 @@ +#include +#include // For max function + +using namespace std; + +// Definition for a binary tree node. +struct TreeNode { + int val; + TreeNode* left; + TreeNode* right; + TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} +}; + +class Solution { +public: + int maxPathSum(TreeNode* root) { + int maxSum = INT_MIN; // Initialize with the minimum possible value + findMaxPathSum(root, maxSum); + return maxSum; + } + +private: + int findMaxPathSum(TreeNode* node, int& maxSum) { + if (!node) { + return 0; + } + + // Calculate the maximum path sum for the left and right subtrees + int leftMax = max(0, findMaxPathSum(node->left, maxSum)); + int rightMax = max(0, findMaxPathSum(node->right, maxSum)); + + // Calculate the maximum path sum that includes the current node + int currentMax = node->val + leftMax + rightMax; + + // Update the overall maximum path sum + maxSum = max(maxSum, currentMax); + + // Return the maximum path sum that can be extended from this node + return node->val + max(leftMax, rightMax); + } +}; + +int main() { + // Create a sample binary tree + TreeNode* root = new TreeNode(10); + root->left = new TreeNode(2); + root->right = new TreeNode(10); + root->left->left = new TreeNode(20); + root->left->right = new TreeNode(1); + root->right->right = new TreeNode(-25); + root->right->right->left = new TreeNode(3); + root->right->right->right = new TreeNode(4); + + Solution solution; + int maxSum = solution.maxPathSum(root); + + cout << "Maximum Path Sum: " << maxSum << endl; + + return 0; +} From 1d6a57f4a3f7ef82d5e2183ef7712d38fb8c2de9 Mon Sep 17 00:00:00 2001 From: Abhinav kumar <126642111+Abhinavcode13@users.noreply.github.com> Date: Thu, 14 Sep 2023 18:36:32 +0530 Subject: [PATCH 15/22] Create Hammingdistance.cpp --- Math/Hammingdistance.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 Math/Hammingdistance.cpp diff --git a/Math/Hammingdistance.cpp b/Math/Hammingdistance.cpp new file mode 100644 index 00000000..68f2687f --- /dev/null +++ b/Math/Hammingdistance.cpp @@ -0,0 +1,14 @@ +lass Solution { +public: + int totalHammingDistance(vector& nums) { + int n=nums.size(), ans=0; + for(int i=0;i<32;i++){ + int count=0; + for(int k=0;k>i)&1; + } + ans += count*(n-count); + } + return ans; + } +}; From a81b6b64d908b5e71def5a7d6e29da1babd6adb6 Mon Sep 17 00:00:00 2001 From: Abhinav kumar <126642111+Abhinavcode13@users.noreply.github.com> Date: Sat, 16 Sep 2023 10:32:19 +0530 Subject: [PATCH 16/22] Update Hammingdistance.cpp --- Math/Hammingdistance.cpp | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/Math/Hammingdistance.cpp b/Math/Hammingdistance.cpp index 68f2687f..7c9904ee 100644 --- a/Math/Hammingdistance.cpp +++ b/Math/Hammingdistance.cpp @@ -1,14 +1,34 @@ -lass Solution { +#include + +class Solution { public: - int totalHammingDistance(vector& nums) { - int n=nums.size(), ans=0; - for(int i=0;i<32;i++){ - int count=0; - for(int k=0;k>i)&1; + int totalHammingDistance(std::vector& nums) { + // Get the number of elements in the input vector + int n = nums.size(); + // Initialize the variable to store the total Hamming distance + int ans = 0; + + // Iterate through each bit position (from 0 to 31) + for (int i = 0; i < 32; i++) { + // Initialize a variable to count the number of set bits at the current bit position + int count = 0; + + // Iterate through all elements in the vector + for (int k = 0; k < n; k++) { + // Count the number of set bits at the current bit position for each element + count += (nums[k] >> i) & 1; } - ans += count*(n-count); + + // Update the total Hamming distance by adding the product of set bits and unset bits + ans += count * (n - count); } + + // Return the total Hamming distance return ans; } }; + +/* +This program defines a Solution class with a totalHammingDistance method that calculates the total Hamming distance for the input vector nums. The outer loop iterates over each bit position (from 0 to 31), and the inner loop counts the number of set bits at that bit position for all elements in the vector. It then updates the ans variable by adding the product of the count of set bits and the count of unset bits at that position. + +This code efficiently calculates the total Hamming distance for a vector of integers by considering each bit position separately. */ From abe9c4ba288636eb5d4a6434d6da332d1b8960e7 Mon Sep 17 00:00:00 2001 From: Abhinav kumar <126642111+Abhinavcode13@users.noreply.github.com> Date: Sat, 16 Sep 2023 10:33:48 +0530 Subject: [PATCH 17/22] Create Bloomfilter.cpp --- Hash Table/Bloomfilter.cpp | 66 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 Hash Table/Bloomfilter.cpp diff --git a/Hash Table/Bloomfilter.cpp b/Hash Table/Bloomfilter.cpp new file mode 100644 index 00000000..76de2d67 --- /dev/null +++ b/Hash Table/Bloomfilter.cpp @@ -0,0 +1,66 @@ +#include +#include +#include +#include + +class BloomFilter { +private: + int size; // Size of the Bloom Filter bitset + std::vector> hash_functions; + std::bitset<1000000> bitset; // Bitset to represent the Bloom Filter + +public: + // Constructor to initialize the Bloom Filter with a given size and hash functions + BloomFilter(int size, std::vector> hash_functions) + : size(size), hash_functions(hash_functions) {} + + // Function to add an element to the Bloom Filter + void add(const std::string& element) { + for (const auto& hash_function : hash_functions) { + size_t index = hash_function(element) % size; + bitset.set(index); + } + } + + // Function to check if an element may exist in the Bloom Filter + bool contains(const std::string& element) { + for (const auto& hash_function : hash_functions) { + size_t index = hash_function(element) % size; + if (!bitset.test(index)) { + return false; // If any bit is not set, the element is definitely not in the filter + } + } + return true; // If all bits are set, the element may exist in the filter (false positives are possible) + } +}; + +// Example hash function using std::hash +size_t hash1(const std::string& str) { + return std::hash{}(str); +} + +// Example hash function using a custom hash function +size_t hash2(const std::string& str) { + size_t hash = 0; + for (char c : str) { + hash = (hash * 31) + c; + } + return hash; +} + +int main() { + // Create a Bloom Filter with a size of 1000000 and two hash functions + BloomFilter bloomFilter(1000000, {hash1, hash2}); + + // Add elements to the Bloom Filter + bloomFilter.add("apple"); + bloomFilter.add("banana"); + bloomFilter.add("cherry"); + + // Check if elements may exist in the Bloom Filter + std::cout << "Contains 'apple': " << bloomFilter.contains("apple") << std::endl; // Should return 1 (true) + std::cout << "Contains 'grape': " << bloomFilter.contains("grape") << std::endl; // Should return 0 (false) + std::cout << "Contains 'cherry': " << bloomFilter.contains("cherry") << std::endl; // Should return 1 (true) + + return 0; +} From 4df56ba6e7700512517c2d8b5d184dc06441ee79 Mon Sep 17 00:00:00 2001 From: Abhinav kumar <126642111+Abhinavcode13@users.noreply.github.com> Date: Sun, 17 Sep 2023 19:34:35 +0530 Subject: [PATCH 18/22] Create MiddleofLL.py --- Linked List/MiddleofLL.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 Linked List/MiddleofLL.py diff --git a/Linked List/MiddleofLL.py b/Linked List/MiddleofLL.py new file mode 100644 index 00000000..bf232d90 --- /dev/null +++ b/Linked List/MiddleofLL.py @@ -0,0 +1,32 @@ +class ListNode: + def __init__(self, value=0, next=None): + self.value = value + self.next = next + +def find_middle(head): + # Initialize slow and fast pointers to the head of the linked list + slow_ptr = head + fast_ptr = head + + # Traverse the linked list with slow and fast pointers + while fast_ptr is not None and fast_ptr.next is not None: + # Move slow pointer one step at a time + slow_ptr = slow_ptr.next + # Move fast pointer two steps at a time + fast_ptr = fast_ptr.next.next + + # When the fast pointer reaches the end, the slow pointer will be at the middle + return slow_ptr + +# Create a sample linked list: 1 -> 2 -> 3 -> 4 -> 5 +head = ListNode(1) +head.next = ListNode(2) +head.next.next = ListNode(3) +head.next.next.next = ListNode(4) +head.next.next.next.next = ListNode(5) + +# Find the middle node of the linked list +middle_node = find_middle(head) + +# Print the value of the middle node +print("Middle Node:", middle_node.value) # Output: Middle Node: 3 From 3c07abd87e79e5698172e0f5d0717ba259956d94 Mon Sep 17 00:00:00 2001 From: p1kalys Date: Tue, 19 Sep 2023 10:02:24 +0530 Subject: [PATCH 19/22] Added a program in Hash Table. --- .../Count_Pairs_of_Points_With_Distance_k.py | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 Hash Table/Count_Pairs_of_Points_With_Distance_k.py diff --git a/Hash Table/Count_Pairs_of_Points_With_Distance_k.py b/Hash Table/Count_Pairs_of_Points_With_Distance_k.py new file mode 100644 index 00000000..85a40bfa --- /dev/null +++ b/Hash Table/Count_Pairs_of_Points_With_Distance_k.py @@ -0,0 +1,33 @@ +#Given a 2D integer array coordinates and an integer k, where coordinates[i] = [xi, yi] are the coordinates of the ith point in a 2D plane. + +#We define the distance between two points (x1, y1) and (x2, y2) as (x1 XOR x2) + (y1 XOR y2) where XOR is the bitwise XOR operation. + +#Return the number of pairs (i, j) such that i < j and the distance between points i and j is equal to k. + +def count_pairs(coordinates, k): + result = 0 + seen = {} + + for x, y in coordinates: + for split in range(k + 1): + x_complement = x ^ split + y_complement = y ^ (k - split) + result += seen.get((x_complement, y_complement), 0) + seen[x, y] = seen.get((x, y), 0) + 1 + + return result + +# Take user input for coordinates +coordinates = [] +num_coordinates = int(input("Enter the number of coordinates: ")) + +for i in range(num_coordinates): + x = int(input(f"Enter x-coordinate for point {i + 1}: ")) + y = int(input(f"Enter y-coordinate for point {i + 1}: ")) + coordinates.append((x, y)) + +# Take user input for k +k = int(input("Enter the value of k: ")) + +pair_count = count_pairs(coordinates, k) +print(f"Number of pairs that satisfy the condition: {pair_count}") From 0479b65c0edf76542ea6ee108e53a36cf1631801 Mon Sep 17 00:00:00 2001 From: Abhinav kumar <126642111+Abhinavcode13@users.noreply.github.com> Date: Sat, 23 Sep 2023 11:24:39 +0530 Subject: [PATCH 20/22] Create Jobassign.cpp --- Arrays/Jobassign.cpp | 95 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 Arrays/Jobassign.cpp diff --git a/Arrays/Jobassign.cpp b/Arrays/Jobassign.cpp new file mode 100644 index 00000000..ddda1007 --- /dev/null +++ b/Arrays/Jobassign.cpp @@ -0,0 +1,95 @@ +#include +#include + +#define N 4 // Number of workers and tasks + +void hungarianAlgorithm(int costMatrix[N][N]); + +int main() { + int costMatrix[N][N] = { + {9, 2, 7, 8}, + {6, 4, 3, 7}, + {5, 8, 1, 8}, + {7, 6, 9, 4} + }; + + hungarianAlgorithm(costMatrix); + + return 0; +} + +void hungarianAlgorithm(int costMatrix[N][N]) { + int i, j; + int numWorkers = N, numTasks = N; + + int minCost, minCostIdx; + int rowCover[N] = {0}; + int colCover[N] = {0}; + int assignment[N][2] = {0}; // Stores the assignment + + // Step 1: Subtract the smallest value in each row from all elements in that row + for (i = 0; i < numWorkers; i++) { + minCost = INT_MAX; + for (j = 0; j < numTasks; j++) { + if (costMatrix[i][j] < minCost) { + minCost = costMatrix[i][j]; + } + } + for (j = 0; j < numTasks; j++) { + costMatrix[i][j] -= minCost; + } + } + + // Step 2: Find a zero in the cost matrix and mark the row and column + for (i = 0; i < numWorkers; i++) { + for (j = 0; j < numTasks; j++) { + if (costMatrix[i][j] == 0 && !rowCover[i] && !colCover[j]) { + assignment[i][0] = i; + assignment[i][1] = j; + rowCover[i] = 1; + colCover[j] = 1; + } + } + } + + // Step 3: Check if all rows are covered + int rowCoveredCount = 0; + for (i = 0; i < numWorkers; i++) { + rowCoveredCount += rowCover[i]; + } + + if (rowCoveredCount == numWorkers) { + // All rows are covered, we have the optimal assignment + printf("Optimal Assignment:\n"); + for (i = 0; i < numWorkers; i++) { + printf("Worker %d -> Task %d\n", assignment[i][0] + 1, assignment[i][1] + 1); + } + return; + } else { + // Proceed to step 4 + } + + // Step 4: Find the minimum uncovered value (minCost) in the cost matrix + minCost = INT_MAX; + for (i = 0; i < numWorkers; i++) { + for (j = 0; j < numTasks; j++) { + if (!rowCover[i] && !colCover[j] && costMatrix[i][j] < minCost) { + minCost = costMatrix[i][j]; + } + } + } + + // Step 5: Subtract minCost from all uncovered elements and add it to all elements at the intersection of covering lines + for (i = 0; i < numWorkers; i++) { + for (j = 0; j < numTasks; j++) { + if (!rowCover[i] && !colCover[j]) { + costMatrix[i][j] -= minCost; + } else if (rowCover[i] && colCover[j]) { + costMatrix[i][j] += minCost; + } + } + } + + // Continue to step 3 + hungarianAlgorithm(costMatrix); +} From 9f3ff7ee43b10cc038fcc7738ec429c695801b2c Mon Sep 17 00:00:00 2001 From: Evangelos-Dimos Date: Fri, 29 Sep 2023 16:01:12 +0300 Subject: [PATCH 21/22] Scheduling Algortihms --- Scheduling Algortihms/fcfs.c | 132 ++++++++++++++++++++++++++ Scheduling Algortihms/sjf.c | 162 ++++++++++++++++++++++++++++++++ Scheduling Algortihms/srtf.c | 176 +++++++++++++++++++++++++++++++++++ 3 files changed, 470 insertions(+) create mode 100644 Scheduling Algortihms/fcfs.c create mode 100644 Scheduling Algortihms/sjf.c create mode 100644 Scheduling Algortihms/srtf.c diff --git a/Scheduling Algortihms/fcfs.c b/Scheduling Algortihms/fcfs.c new file mode 100644 index 00000000..1af2fc20 --- /dev/null +++ b/Scheduling Algortihms/fcfs.c @@ -0,0 +1,132 @@ +/* + +The First-Come, First-Served (FCFS) scheduling algorithm is one of the simplest process scheduling algorithms used in operating systems. It follows the principle of serving processes in the order they arrive in the ready queue. In FCFS, the process that arrives first will be the first one to be executed, and it continues until the process completes its execution or enters a waiting state. + +Algorithm Description: + +When a process enters the ready queue, it is added to the end of the queue (at the "tail" of the queue). + +The CPU scheduler selects the process at the front of the queue (the "head" of the queue) for execution. + +The selected process runs on the CPU until it completes its execution, enters a waiting state (e.g., I/O operation), or its time quantum (if there is a time-sharing system) expires. + +Once the currently running process finishes, the CPU scheduler selects the process at the front of the queue as the next process to run. This process will continue until it completes or enters a waiting state, and so on. + +This process of selecting and executing processes continues until all processes in the queue have executed. + +Example: +Let's illustrate the FCFS algorithm with a simple example. Consider three processes, P1, P2, and P3, with their respective burst times (the time they need to complete execution): + +Process P1: Burst time = 2 ms +Process P2: Burst time = 7 ms +Process P3: Burst time = 4 ms +Here's how FCFS will schedule and execute these processes: + +Initially, the ready queue is empty. + +Process P1 arrives first, so it is added to the ready queue: + +Ready Queue: [P1] +Execution: P1 (2 ms) +Process P1 completes its execution. Now, process P2 is selected because it's at the front of the queue: + +Ready Queue: [P2] +Execution: P2 (7 ms) +Process P2 completes its execution. Finally, process P3 is selected: + +Ready Queue: [P3] +Execution: P3 (4 ms) +Process P3 completes its execution. + +The order of execution in FCFS is P1 -> P2 -> P3. FCFS is non-preemptive, meaning once a process starts execution, it continues until it finishes or enters a waiting state. It can suffer from the "convoy effect," where a long process at the head of the queue can block shorter processes behind it. + +*/ + +#include +#include + +// Struct for the info of each process +typedef struct +{ + + int process_id; + int arrival_time; + int burst_time; + +} process; + + +int main() { + + // Number of processes + int number=3; + + // Size of the array which will show the order of execution of the processes + process *arr; + arr = malloc(number * sizeof(process)); + + // Initialize for our example + arr[0].process_id = 1; + arr[0].arrival_time = 0; + arr[0].burst_time = 2; + + arr[1].process_id = 2; + arr[1].arrival_time = 1; + arr[1].burst_time = 7; + + arr[2].process_id = 3; + arr[2].arrival_time = 3; + arr[2].burst_time = 4; + + //Sorting the struct by arrival time + process temp; + for(int i=0; i P2 -> P3. SJF aims to minimize the average waiting time for processes, making it an effective algorithm when you have information about the burst times of processes. However, it can suffer from starvation if a long job continually arrives after shorter jobs. +*/ + +#include +#include + +// Struct for the info of each process +typedef struct +{ + + int process_id; + int arrival_time; + int burst_time; + +} process; + + +int main() { + + // Number of processes + int number=3; + + // Size of the array which will show the order of execution of the processes + process *arr; + arr = malloc(number * sizeof(process)); + + // Initialize for our example + arr[0].process_id = 1; + arr[0].arrival_time = 0; + arr[0].burst_time = 7; + + arr[1].process_id = 2; + arr[1].arrival_time = 2; + arr[1].burst_time = 3; + + arr[2].process_id = 3; + arr[2].arrival_time = 4; + arr[2].burst_time = 8; + + //Sorting the struct by arrival time + process temp; + for(int i=0; i= arr[j].arrival_time && arr[j].burst_time <= min) + { + temp = arr[k]; + arr[k] = arr[j]; + arr[j] = temp; + } + } + k++; //increase index + } + + //Variable for the sum of burst time + int sum_of_burst_time=0; + + //Calculate sum of burst time + for (int i=0; i P2 -> P1 -> P3. SRTF provides optimal turnaround times but may suffer from frequent context switches due to its preemptive nature. +*/ + +#include +#include + +// Struct for the info of each process +typedef struct +{ + + int process_id; + int arrival_time; + int burst_time; + +} process; + + +int main() { + + // Number of processes + int number=3; + + // Size of the array which will show the order of execution of the processes + process *arr; + arr = malloc(number * sizeof(process)); + + // Initialize for our example + arr[0].process_id = 1; + arr[0].arrival_time = 0; + arr[0].burst_time = 7; + + arr[1].process_id = 2; + arr[1].arrival_time = 2; + arr[1].burst_time = 3; + + arr[2].process_id = 3; + arr[2].arrival_time = 4; + arr[2].burst_time = 8; + + //Sorting the struct by arrival time + process temp; + for (int i=0; i Date: Fri, 29 Sep 2023 18:25:37 +0300 Subject: [PATCH 22/22] Scheduling --- Scheduling/fcfs.c | 132 ++++++++++++++++++++++++++++++++++ Scheduling/sjf.c | 162 ++++++++++++++++++++++++++++++++++++++++++ Scheduling/srtf.c | 176 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 470 insertions(+) create mode 100644 Scheduling/fcfs.c create mode 100644 Scheduling/sjf.c create mode 100644 Scheduling/srtf.c diff --git a/Scheduling/fcfs.c b/Scheduling/fcfs.c new file mode 100644 index 00000000..1af2fc20 --- /dev/null +++ b/Scheduling/fcfs.c @@ -0,0 +1,132 @@ +/* + +The First-Come, First-Served (FCFS) scheduling algorithm is one of the simplest process scheduling algorithms used in operating systems. It follows the principle of serving processes in the order they arrive in the ready queue. In FCFS, the process that arrives first will be the first one to be executed, and it continues until the process completes its execution or enters a waiting state. + +Algorithm Description: + +When a process enters the ready queue, it is added to the end of the queue (at the "tail" of the queue). + +The CPU scheduler selects the process at the front of the queue (the "head" of the queue) for execution. + +The selected process runs on the CPU until it completes its execution, enters a waiting state (e.g., I/O operation), or its time quantum (if there is a time-sharing system) expires. + +Once the currently running process finishes, the CPU scheduler selects the process at the front of the queue as the next process to run. This process will continue until it completes or enters a waiting state, and so on. + +This process of selecting and executing processes continues until all processes in the queue have executed. + +Example: +Let's illustrate the FCFS algorithm with a simple example. Consider three processes, P1, P2, and P3, with their respective burst times (the time they need to complete execution): + +Process P1: Burst time = 2 ms +Process P2: Burst time = 7 ms +Process P3: Burst time = 4 ms +Here's how FCFS will schedule and execute these processes: + +Initially, the ready queue is empty. + +Process P1 arrives first, so it is added to the ready queue: + +Ready Queue: [P1] +Execution: P1 (2 ms) +Process P1 completes its execution. Now, process P2 is selected because it's at the front of the queue: + +Ready Queue: [P2] +Execution: P2 (7 ms) +Process P2 completes its execution. Finally, process P3 is selected: + +Ready Queue: [P3] +Execution: P3 (4 ms) +Process P3 completes its execution. + +The order of execution in FCFS is P1 -> P2 -> P3. FCFS is non-preemptive, meaning once a process starts execution, it continues until it finishes or enters a waiting state. It can suffer from the "convoy effect," where a long process at the head of the queue can block shorter processes behind it. + +*/ + +#include +#include + +// Struct for the info of each process +typedef struct +{ + + int process_id; + int arrival_time; + int burst_time; + +} process; + + +int main() { + + // Number of processes + int number=3; + + // Size of the array which will show the order of execution of the processes + process *arr; + arr = malloc(number * sizeof(process)); + + // Initialize for our example + arr[0].process_id = 1; + arr[0].arrival_time = 0; + arr[0].burst_time = 2; + + arr[1].process_id = 2; + arr[1].arrival_time = 1; + arr[1].burst_time = 7; + + arr[2].process_id = 3; + arr[2].arrival_time = 3; + arr[2].burst_time = 4; + + //Sorting the struct by arrival time + process temp; + for(int i=0; i P2 -> P3. SJF aims to minimize the average waiting time for processes, making it an effective algorithm when you have information about the burst times of processes. However, it can suffer from starvation if a long job continually arrives after shorter jobs. +*/ + +#include +#include + +// Struct for the info of each process +typedef struct +{ + + int process_id; + int arrival_time; + int burst_time; + +} process; + + +int main() { + + // Number of processes + int number=3; + + // Size of the array which will show the order of execution of the processes + process *arr; + arr = malloc(number * sizeof(process)); + + // Initialize for our example + arr[0].process_id = 1; + arr[0].arrival_time = 0; + arr[0].burst_time = 7; + + arr[1].process_id = 2; + arr[1].arrival_time = 2; + arr[1].burst_time = 3; + + arr[2].process_id = 3; + arr[2].arrival_time = 4; + arr[2].burst_time = 8; + + //Sorting the struct by arrival time + process temp; + for(int i=0; i= arr[j].arrival_time && arr[j].burst_time <= min) + { + temp = arr[k]; + arr[k] = arr[j]; + arr[j] = temp; + } + } + k++; //increase index + } + + //Variable for the sum of burst time + int sum_of_burst_time=0; + + //Calculate sum of burst time + for (int i=0; i P2 -> P1 -> P3. SRTF provides optimal turnaround times but may suffer from frequent context switches due to its preemptive nature. +*/ + +#include +#include + +// Struct for the info of each process +typedef struct +{ + + int process_id; + int arrival_time; + int burst_time; + +} process; + + +int main() { + + // Number of processes + int number=3; + + // Size of the array which will show the order of execution of the processes + process *arr; + arr = malloc(number * sizeof(process)); + + // Initialize for our example + arr[0].process_id = 1; + arr[0].arrival_time = 0; + arr[0].burst_time = 7; + + arr[1].process_id = 2; + arr[1].arrival_time = 2; + arr[1].burst_time = 3; + + arr[2].process_id = 3; + arr[2].arrival_time = 4; + arr[2].burst_time = 8; + + //Sorting the struct by arrival time + process temp; + for (int i=0; i