diff --git a/Algorithms/DutchNationalFlagAlgo.java b/Algorithms/DutchNationalFlagAlgo.java new file mode 100644 index 00000000..53c109fb --- /dev/null +++ b/Algorithms/DutchNationalFlagAlgo.java @@ -0,0 +1,46 @@ +import java.util.Scanner; +public class DutchNationalFlagAlgo +{ + public static void sortColors(int[] nums) { + int mid=0; + int low=0; + int high=nums.length - 1; + while(mid<=high) + { + if(nums[mid]==0) + { + int temp = nums[low]; + nums[low] = nums[mid]; + nums[mid] = temp; + mid++; + low++; + } + else if (nums[mid]==1) + { + mid++; + } + else + { + int temp = nums[high]; + nums[high] = nums[mid]; + nums[mid] = temp; + high--; + } + } + } + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int n = sc.nextInt(); + int []arr = new int[n]; + for(int i=0;i= l) { + int mid = l + (r - l) / 2; + + // If the element is present at the + // middle itself + if (arr[mid] == x) + return mid; + + // If element is smaller than mid, then + // it can only be present in left subarray + if (arr[mid] > x) + return binarySearch(arr, l, mid - 1, x); + + // Else the element can only be present + // in right subarray + return binarySearch(arr, mid + 1, r, x); + } + + // We reach here when element is not present + // in array + return -1; + } + + + public static void main(String args[]) + { + BinarySearch ob = new BinarySearch(); + int arr[] = { 2, 3, 4, 10, 40 }; + int n = arr.length; + int x = 10; + int result = ob.binarySearch(arr, 0, n - 1, x); + if (result == -1) + System.out.println("Element not present"); + else + System.out.println("Element found at index " + result); + } +} + diff --git a/Algorithms/sudoku.java b/Algorithms/sudoku.java new file mode 100644 index 00000000..348473c4 --- /dev/null +++ b/Algorithms/sudoku.java @@ -0,0 +1,142 @@ +/*Java Program to solve Sudoku problem using Backtracking*/ +public class Main +{ + public static boolean isValid(int[][] puzzle,int row, int col,int number) + { + // Row has the unique + for (int d = 0; d < puzzle.length; d++) + { + + /* If the number we are trying to + place is already present in + that row, then return false;*/ + if (puzzle[row][d] == number) { + return false; + } + } + // Column has the unique numberbers + for (int r = 0; r < puzzle.length; r++) + { + /* If the number we are trying to + place is already present in + that column, then return false;*/ + if (puzzle[r][col] == number) + { + return false; + } + } + + //Corresponding square has unique number + int sqrt = (int)Math.sqrt(puzzle.length); + int bRow = row - row % sqrt; + int bCol = col - col % sqrt; + for (int r = bRow; + r < bRow + sqrt; r++) + { + for (int d = bCol; + d < bCol + sqrt; d++) + { + if (puzzle[r][d] == number) + { + return false; + } + } + } + // if there is no clash, it's safe + return true; + } + public static boolean solution(int[][] puzzle, 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 (puzzle[i][j] == 0) + { + row = i; + col = j; + isEmpty = false; + break; + } + } + if (!isEmpty) { + break; + } + } + // No empty space left + if (isEmpty) + { + return true; + } + // Else backtrack for each-row + for (int number = 1; number <= n; number++) + { + if (isValid(puzzle, row, col, number)) + { + puzzle[row][col] = number; + if (solution(puzzle, n)) + { + // print(puzzle, n); + return true; + } + else + { + // replace it + puzzle[row][col] = 0; + } + } + } + return false; + } + //To Print the Sudoku + public static void printSudoku(int[][] puzzle, int N) + { + for (int r = 0; r < N; r++) + { + for (int d = 0; d < N; d++) + { + System.out.print(puzzle[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[]) + { + //Here 0 represents the unassigned value + int[][] puzzle = new int[][] { + { 3, 0, 5, 4, 0, 2, 0, 6, 0 }, + { 4, 9, 0, 7, 6, 0, 1, 0, 8 }, + { 6, 0, 0, 1, 0, 3, 2, 4, 5 }, + { 0, 0, 3, 9, 0, 0, 5, 8, 0 }, + { 9, 6, 0, 0, 5, 8, 7, 0, 3 }, + { 0, 8, 1, 3, 0, 4, 0, 9, 2 }, + { 0, 5, 0, 6, 0, 1, 4, 0, 0 }, + { 2, 0, 0, 5, 4, 9, 0, 7, 0 }, + { 1, 4, 9, 0, 0, 7, 3, 0, 6 } + }; + int N = puzzle.length; + System.out.println("Before Solving"); + printSudoku(puzzle, N); + System.out.println(""); + System.out.println("After Solving"); + if (solution(puzzle, N)) + { + printSudoku(puzzle, N); + } + else + { + System.out.println("No solution exists"); + } + } +} diff --git a/Dynamic Programming/KadaneAlorithm.java b/Dynamic Programming/KadaneAlorithm.java new file mode 100644 index 00000000..e5857ac6 --- /dev/null +++ b/Dynamic Programming/KadaneAlorithm.java @@ -0,0 +1,19 @@ + +class KadaneAlorithm{ + + // arr: input array + // n: size of array + //Function to find the sum of contiguous subarray with maximum sum. + int maxSubarraySum(int arr[]){ + int n = arr.length; + int localMax = 0; + int globalMax = Integer.MIN_VALUE; + + for(int i = 0; i < n; i++){ + localMax = Math.max(arr[i], localMax + arr[i]); + if(localMax > globalMax){ + globalMax = localMax; + } + } + return globalMax; + }