From 0bace319078f3544488fc751d2716cbc461f0a7e Mon Sep 17 00:00:00 2001 From: Aman Date: Tue, 23 Oct 2018 15:18:55 +0530 Subject: [PATCH 1/5] Modified Readme --- README.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 5c037c2..d338867 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,4 @@ # Competitve-programming -This Notebook contains Most frequently used Algorithm and concept used in Competive coding +This Notebook contains Most frequently used Algorithm and concepts used in Competitive coding and some cool functions often used in competitive programming. -and some cool functions often used in competitive programming. - -All the codes are written in Python3 and Java 8 +All the codes are written in Python3 and Java 8. From b20184ef41026618304d0591b5fc41fe0ff22b1d Mon Sep 17 00:00:00 2001 From: Aman Date: Tue, 23 Oct 2018 15:23:58 +0530 Subject: [PATCH 2/5] Knights tour added --- src/cc/Knights Tour.java | 89 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 src/cc/Knights Tour.java diff --git a/src/cc/Knights Tour.java b/src/cc/Knights Tour.java new file mode 100644 index 0000000..e093dd7 --- /dev/null +++ b/src/cc/Knights Tour.java @@ -0,0 +1,89 @@ +// Java program for Knight Tour problem +class KnightTour { + static int N = 8; + + /* A utility function to check if i,j are + valid indexes for N*N chessboard */ + static boolean isSafe(int x, int y, int sol[][]) { + return (x >= 0 && x < N && y >= 0 && + y < N && sol[x][y] == -1); + } + + /* A utility function to print solution + matrix sol[N][N] */ + static void printSolution(int sol[][]) { + for (int x = 0; x < N; x++) { + for (int y = 0; y < N; y++) + System.out.print(sol[x][y] + " "); + System.out.println(); + } + } + + /* This function solves the Knight Tour problem + using Backtracking. This function mainly + uses solveKTUtil() to solve the problem. It + returns false if no complete tour is possible, + otherwise return true and prints the tour. + Please note that there may be more than one + solutions, this function prints one of the + feasible solutions. */ + static boolean solveKT() { + int sol[][] = new int[8][8]; + + /* Initialization of solution matrix */ + for (int x = 0; x < N; x++) + for (int y = 0; y < N; y++) + sol[x][y] = -1; + + /* xMove[] and yMove[] define next move of Knight. + xMove[] is for next value of x coordinate + yMove[] is for next value of y coordinate */ + int xMove[] = {2, 1, -1, -2, -2, -1, 1, 2}; + int yMove[] = {1, 2, 2, 1, -1, -2, -2, -1}; + + // Since the Knight is initially at the first block + sol[0][0] = 0; + + /* Start from 0,0 and explore all tours using + solveKTUtil() */ + if (!solveKTUtil(0, 0, 1, sol, xMove, yMove)) { + System.out.println("Solution does not exist"); + return false; + } else + printSolution(sol); + + return true; + } + + /* A recursive utility function to solve Knight + Tour problem */ + static boolean solveKTUtil(int x, int y, int movei, + int sol[][], int xMove[], + int yMove[]) { + int k, next_x, next_y; + if (movei == N * N) + return true; + + /* Try all next moves from the current coordinate + x, y */ + for (k = 0; k < 8; k++) { + next_x = x + xMove[k]; + next_y = y + yMove[k]; + if (isSafe(next_x, next_y, sol)) { + sol[next_x][next_y] = movei; + if (solveKTUtil(next_x, next_y, movei + 1, + sol, xMove, yMove)) + return true; + else + sol[next_x][next_y] = -1;// backtracking + } + } + + return false; + } + + /* Driver program to test above functions */ + public static void main(String args[]) { + solveKT(); + } +} From dbd248e7a408efb55ca485266d0bc41b032866d3 Mon Sep 17 00:00:00 2001 From: Aman Date: Tue, 23 Oct 2018 15:27:18 +0530 Subject: [PATCH 3/5] Hamiltonian cycle added --- src/cc/Hamiltonian Cycle.java | 147 ++++++++++++++++++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 src/cc/Hamiltonian Cycle.java diff --git a/src/cc/Hamiltonian Cycle.java b/src/cc/Hamiltonian Cycle.java new file mode 100644 index 0000000..ece9760 --- /dev/null +++ b/src/cc/Hamiltonian Cycle.java @@ -0,0 +1,147 @@ +/* Java program for solution of Hamiltonian Cycle problem + using backtracking */ +class HamiltonianCycle +{ + final int V = 5; + int path[]; + + /* A utility function to check if the vertex v can be + added at index 'pos'in the Hamiltonian Cycle + constructed so far (stored in 'path[]') */ + boolean isSafe(int v, int graph[][], int path[], int pos) + { + /* Check if this vertex is an adjacent vertex of + the previously added vertex. */ + if (graph[path[pos - 1]][v] == 0) + return false; + + /* Check if the vertex has already been included. + This step can be optimized by creating an array + of size V */ + for (int i = 0; i < pos; i++) + if (path[i] == v) + return false; + + return true; + } + + /* A recursive utility function to solve hamiltonian + cycle problem */ + boolean hamCycleUtil(int graph[][], int path[], int pos) + { + /* base case: If all vertices are included in + Hamiltonian Cycle */ + if (pos == V) + { + // And if there is an edge from the last included + // vertex to the first vertex + if (graph[path[pos - 1]][path[0]] == 1) + return true; + else + return false; + } + + // Try different vertices as a next candidate in + // Hamiltonian Cycle. We don't try for 0 as we + // included 0 as starting point in in hamCycle() + for (int v = 1; v < V; v++) + { + /* Check if this vertex can be added to Hamiltonian + Cycle */ + if (isSafe(v, graph, path, pos)) + { + path[pos] = v; + + /* recur to construct rest of the path */ + if (hamCycleUtil(graph, path, pos + 1) == true) + return true; + + /* If adding vertex v doesn't lead to a solution, + then remove it */ + path[pos] = -1; + } + } + + /* If no vertex can be added to Hamiltonian Cycle + constructed so far, then return false */ + return false; + } + + /* This function solves the Hamiltonian Cycle problem using + Backtracking. It mainly uses hamCycleUtil() to solve the + problem. It returns false if there is no Hamiltonian Cycle + possible, otherwise return true and prints the path. + Please note that there may be more than one solutions, + this function prints one of the feasible solutions. */ + int hamCycle(int graph[][]) + { + path = new int[V]; + for (int i = 0; i < V; i++) + path[i] = -1; + + /* Let us put vertex 0 as the first vertex in the path. + If there is a Hamiltonian Cycle, then the path can be + started from any point of the cycle as the graph is + undirected */ + path[0] = 0; + if (hamCycleUtil(graph, path, 1) == false) + { + System.out.println("\nSolution does not exist"); + return 0; + } + + printSolution(path); + return 1; + } + + /* A utility function to print solution */ + void printSolution(int path[]) + { + System.out.println("Solution Exists: Following" + + " is one Hamiltonian Cycle"); + for (int i = 0; i < V; i++) + System.out.print(" " + path[i] + " "); + + // Let us print the first vertex again to show the + // complete cycle + System.out.println(" " + path[0] + " "); + } + + // driver program to test above function + public static void main(String args[]) + { + HamiltonianCycle hamiltonian = + new HamiltonianCycle(); + /* Let us create the following graph + (0)--(1)--(2) + | / \ | + | / \ | + | / \ | + (3)-------(4) */ + int graph1[][] = {{0, 1, 0, 1, 0}, + {1, 0, 1, 1, 1}, + {0, 1, 0, 0, 1}, + {1, 1, 0, 0, 1}, + {0, 1, 1, 1, 0}, + }; + + // Print the solution + hamiltonian.hamCycle(graph1); + + /* Let us create the following graph + (0)--(1)--(2) + | / \ | + | / \ | + | / \ | + (3) (4) */ + int graph2[][] = {{0, 1, 0, 1, 0}, + {1, 0, 1, 1, 1}, + {0, 1, 0, 0, 1}, + {1, 1, 0, 0, 0}, + {0, 1, 1, 0, 0}, + }; + + // Print the solution + hamiltonian.hamCycle(graph2); + } +} From 814efe351b2e880a22e55eaadddad7ce7b1c3000 Mon Sep 17 00:00:00 2001 From: Aman Date: Tue, 23 Oct 2018 15:31:27 +0530 Subject: [PATCH 4/5] Tug Of war added --- src/cc/TugofWar.java | 122 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 src/cc/TugofWar.java diff --git a/src/cc/TugofWar.java b/src/cc/TugofWar.java new file mode 100644 index 0000000..3707ea5 --- /dev/null +++ b/src/cc/TugofWar.java @@ -0,0 +1,122 @@ +// Java program for Tug of war +import java.util.*; +import java.lang.*; +import java.io.*; + +class TugOfWar +{ + public int min_diff; + + // function that tries every possible solution + // by calling itself recursively + void TOWUtil(int arr[], int n, boolean curr_elements[], + int no_of_selected_elements, boolean soln[], + int sum, int curr_sum, int curr_position) + { + // checks whether the it is going out of bound + if (curr_position == n) + return; + + // checks that the numbers of elements left + // are not less than the number of elements + // required to form the solution + if ((n / 2 - no_of_selected_elements) > + (n - curr_position)) + return; + + // consider the cases when current element + // is not included in the solution + TOWUtil(arr, n, curr_elements, + no_of_selected_elements, soln, sum, + curr_sum, curr_position+1); + + // add the current element to the solution + no_of_selected_elements++; + curr_sum = curr_sum + arr[curr_position]; + curr_elements[curr_position] = true; + + // checks if a solution is formed + if (no_of_selected_elements == n / 2) + { + // checks if the solution formed is + // better than the best solution so + // far + if (Math.abs(sum / 2 - curr_sum) < + min_diff) + { + min_diff = Math.abs(sum / 2 - + curr_sum); + for (int i = 0; i < n; i++) + soln[i] = curr_elements[i]; + } + } + else + { + // consider the cases where current + // element is included in the + // solution + TOWUtil(arr, n, curr_elements, + no_of_selected_elements, + soln, sum, curr_sum, + curr_position + 1); + } + + // removes current element before + // returning to the caller of this + // function + curr_elements[curr_position] = false; + } + + // main function that generate an arr + void tugOfWar(int arr[]) + { + int n = arr.length; + + // the boolen array that contains the + // inclusion and exclusion of an element + // in current set. The number excluded + // automatically form the other set + boolean[] curr_elements = new boolean[n]; + + // The inclusion/exclusion array for + // final solution + boolean[] soln = new boolean[n]; + + min_diff = Integer.MAX_VALUE; + + int sum = 0; + for (int i = 0; i < n; i++) + { + sum += arr[i]; + curr_elements[i] = soln[i] = false; + } + + // Find the solution using recursive + // function TOWUtil() + TOWUtil(arr, n, curr_elements, 0, + soln, sum, 0, 0); + + // Print the solution + System.out.print("The first subset is: "); + for (int i = 0; i < n; i++) + { + if (soln[i] == true) + System.out.print(arr[i] + " "); + } + System.out.print("\nThe second subset is: "); + for (int i = 0; i < n; i++) + { + if (soln[i] == false) + System.out.print(arr[i] + " "); + } + } + + // Driver program to test above functions + public static void main (String[] args) + { + int arr[] = {23, 45, -34, 12, 0, 98, + -99, 4, 189, -1, 4}; + TugOfWar a = new TugOfWar(); + a.tugOfWar(arr); + } +} From 8810fbed77f03b47e0f68ad3b581c7eba36386a2 Mon Sep 17 00:00:00 2001 From: Aman Date: Tue, 23 Oct 2018 15:35:44 +0530 Subject: [PATCH 5/5] Sudoku added --- src/cc/Sudoku.java | 155 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 155 insertions(+) create mode 100644 src/cc/Sudoku.java diff --git a/src/cc/Sudoku.java b/src/cc/Sudoku.java new file mode 100644 index 0000000..61b531f --- /dev/null +++ b/src/cc/Sudoku.java @@ -0,0 +1,155 @@ +/* A Backtracking program in +Java to solve Sudoku problem */ +class GFG +{ +public static boolean isSafe(int[][] board, + int row, int col, + int num) +{ + // row has the unique (row-clash) + for (int d = 0; d < board.length; d++) + { + // if the number we are trying to + // place is already present in + // that row, return false; + if (board[row][d] == num) + { + return false; + } + } + + // column has the unique numbers (column-clash) + for (int r = 0; r < board.length; r++) + { + // if the number we are trying to + // place is already present in + // that column, return false; + + if (board[r][col] == num) + { + return false; + } + } + + // corresponding square has + // unique number (box-clash) + int sqrt = (int) Math.sqrt(board.length); + int boxRowStart = row - row % sqrt; + int boxColStart = col - col % sqrt; + + for (int r = boxRowStart; + r < boxRowStart + sqrt; r++) + { + for (int d = boxColStart; + d < boxColStart + sqrt; d++) + { + if (board[r][d] == num) + { + return false; + } + } + } + + // if there is no clash, it's safe + return true; +} + +public static boolean solveSudoku(int[][] board, int n) +{ + int row = -1; + int col = -1; + boolean isEmpty = true; + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + if (board[i][j] == 0) + { + row = i; + col = j; + + // we still have some remaining + // missing values in Sudoku + isEmpty = false; + break; + } + } + if (!isEmpty) + { + break; + } + } + + // no empty space left + if (isEmpty) + { + return true; + } + + // else for each-row backtrack + for (int num = 1; num <= n; num++) + { + if (isSafe(board, row, col, num)) + { + board[row][col] = num; + if (solveSudoku(board, n)) + { + // print(board, n); + return true; + } + else + { + board[row][col] = 0; // replace it + } + } + } + return false; +} + +public static void print(int[][] board, int N) +{ + // we got the answer, just print it + for (int r = 0; r < N; r++) + { + for (int d = 0; d < N; d++) + { + System.out.print(board[r][d]); + System.out.print(" "); + } + System.out.print("\n"); + + if ((r + 1) % (int) Math.sqrt(N) == 0) + { + System.out.print(""); + } + } +} + +// Driver Code +public static void main(String args[]) +{ + + int[][] board = new int[][] + { + {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, 3, 1}, + {0, 0, 3, 0, 1, 0, 0, 8, 0}, + {9, 0, 0, 8, 6, 3, 0, 0, 5}, + {0, 5, 0, 0, 9, 0, 6, 0, 0}, + {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} + }; + int N = board.length; + + if (solveSudoku(board, N)) + { + print(board, N); // print solution + } + else + { + System.out.println("No solution"); + } +} +}