From abd892fb246b8c9c1fd07798fe2a3d9180ded23e Mon Sep 17 00:00:00 2001 From: Yashmita <55138349+yashmita@users.noreply.github.com> Date: Sat, 20 Mar 2021 03:51:42 +0530 Subject: [PATCH 1/9] Added code for Book Allocation problem --- Code/Python/Book_Allocation.py | 171 +++++++++++++++++++++++++++++++++ 1 file changed, 171 insertions(+) create mode 100644 Code/Python/Book_Allocation.py diff --git a/Code/Python/Book_Allocation.py b/Code/Python/Book_Allocation.py new file mode 100644 index 000000000..44691fb8b --- /dev/null +++ b/Code/Python/Book_Allocation.py @@ -0,0 +1,171 @@ +''' +PROBLEM STATEMENT: + +Given an array of integers of size n, where n denotes number of books and each element +of an array denotes the number of pages in the ith book, also given another integer +denoting number of students. The task is to allocate books to the given number of students +so that maximum number of pages allocated to a student is minimum. A book will be +allocated to exactly one student and each student has to be allocated atleast one book. +Allotment should be in contiguous order, for example, a student cannot be allocated +book 1 and book 3, skipping book 2. Calculate and return that minimum possible number. +Return -1 if a valid assignment is not possible. +''' + +# Function to check whether it is possible to allocate books using the current minimum value +def IsPossible(lis, n, stud, minval): + + students = 1 + sumval = 0 + + # Iterating over the books + for i in range(n): + + # If the number of pages in a book is greater than the minimum value, then the minimum value chosen + # cannot be used to alloacte books + if (lis[i] > minval): + return False + + # Counting number of students required for allocating minval pages + if (sumval + lis[i] > minval): + + #incrementing students by 1 + students += 1 + + # Updating current value of sum + sumval = lis[i] + + # If more number of students are required than the given number of students, + # then also it is not possible to allocate books + if (students > stud): + return False + + else: + sumval += lis[i] + + return True + +# Function to find the maximum number of pages allocated to a student is minimum +def MinPages(lis, n, stud): + + sum = 0 + + # If number of students are more than number of books, we'll return -1 because + # all the students won't get a book + if (n < stud): + return -1 + + #Finding the sum of all the pages + for i in range(n): + sum += lis[i] + + # Binary search will be applied over the number of pages + # low will be initialized with 0 while high will be initialized with the total number of pages + # ans variable will store the number of maximum pages that will be assigned to a student and + # because that should be minimum, it is initialized by a large integer value + low, high = 0, sum + ans = 10**9; + + #The loop will run until low is less than or equal to high + while (low <= high): + + # Considering middle element to be currently minimum + # and checking whether it is possible to allocate books using mid value as the minimum number of pages + mid = (low + high) // 2 + + if (IsPossible(lis, n, stud, mid)): + + #If possible, them comparing the mid value with the answer that we have so far + ans = min(ans, mid) + + # Because the number of pages are given in ascending order and we are looking for the + # minimum value possible, so for the next possible answer, we will reduce our search by decreasing high to mid-1 + high = mid - 1 + + else: + # If it is not possible to allocate books using mid value as the minimum value, then we will + # look for the minimum element in the other half that is low will be increased to mid + 1 + low = mid + 1 + + #Returning the final answer + return ans + +# Driver Code + +# User inputs the the number of books + +n = int(input("Enter the size of an array: ")) +lis = [] +print("Enter ", n ," elements:") + +# User inputs number of pages in each book in ascending order + +for i in range(n): + num = int(input()) + lis.append(num) + +# User inputs the number of students among which the books will be allocated + +stud = int(input("Enter the number of students: ")) + +# A function call to MinPages + +print("Minimum number of pages are: ", MinPages(lis, n, stud)) + + +''' +TEST CASES: + +1. +Input: +n = 4 +lis = [12, 34, 67, 90] +stud = 2 + +Output: 113 + +Explanation: +There are 2 students. Books can be allocated in the following way: +1) [12] and [34, 67, 90] +student 1 gets 12 pages +student 2 gets 34 + 67 + 90 = 191 pages +Maximum of the two = 191 +2) [12, 34] and [67, 90] +student 1 gets 12 + 34 = 36 pages +student 2 gets 67 + 90 = 157 pages +Maximum of the two = 157 pages +3) [12, 34, 67] and [90] +student 1 gets 12 + 34 + 67 = 113 pages +student 2 gets 90 pages +Maximum of the two = 113 pages + +Out of the 3 cases, Option 3 has the minimum pages = 113. + +2. +Input: +n = 4 +lis = [5, 17, 100, 11] +stud = 4 + +Output: 100 + +Explanation: +There are 4 students and 4 books. Therefore, each student will get one book with maximum +pages = 100. + +3. +Input: +n = 5 +lis = [34, 56, 67, 78, 89] +stud = 6 + +Output: -1 + +Explanation: +Because number of students are more than number of books. Therefore, there is no valid +assignment possible. + + +TIME COMPLEXITY: O(N*LOG(Sum of pages)) +SPACE COMPLEXITY: O(1) +''' + From 3c8abec0fd64b031e09fa24db660f5a67848d699 Mon Sep 17 00:00:00 2001 From: Yashmita <55138349+yashmita@users.noreply.github.com> Date: Sat, 20 Mar 2021 03:54:59 +0530 Subject: [PATCH 2/9] Updated comments --- Code/Python/Book_Allocation.py | 36 ++++++++++++---------------------- 1 file changed, 12 insertions(+), 24 deletions(-) diff --git a/Code/Python/Book_Allocation.py b/Code/Python/Book_Allocation.py index 44691fb8b..cfccc2a63 100644 --- a/Code/Python/Book_Allocation.py +++ b/Code/Python/Book_Allocation.py @@ -1,14 +1,10 @@ ''' PROBLEM STATEMENT: -Given an array of integers of size n, where n denotes number of books and each element -of an array denotes the number of pages in the ith book, also given another integer -denoting number of students. The task is to allocate books to the given number of students -so that maximum number of pages allocated to a student is minimum. A book will be -allocated to exactly one student and each student has to be allocated atleast one book. -Allotment should be in contiguous order, for example, a student cannot be allocated -book 1 and book 3, skipping book 2. Calculate and return that minimum possible number. -Return -1 if a valid assignment is not possible. +Given an array of integers of size n, where n denotes number of books and each element of an array denotes the number of pages in the ith book, also given another integer +denoting number of students. The task is to allocate books to the given number of students so that maximum number of pages allocated to a student is minimum. A book will be +allocated to exactly one student and each student has to be allocated atleast one book. Allotment should be in contiguous order, for example, a student cannot be allocated +book 1 and book 3, skipping book 2. Calculate and return that minimum possible number. Return -1 if a valid assignment is not possible. ''' # Function to check whether it is possible to allocate books using the current minimum value @@ -20,8 +16,7 @@ def IsPossible(lis, n, stud, minval): # Iterating over the books for i in range(n): - # If the number of pages in a book is greater than the minimum value, then the minimum value chosen - # cannot be used to alloacte books + # If the number of pages in a book is greater than the minimum value, then the minimum value chosen cannot be used to alloacte books if (lis[i] > minval): return False @@ -34,8 +29,7 @@ def IsPossible(lis, n, stud, minval): # Updating current value of sum sumval = lis[i] - # If more number of students are required than the given number of students, - # then also it is not possible to allocate books + # If more number of students are required than the given number of students, then also it is not possible to allocate books if (students > stud): return False @@ -49,8 +43,7 @@ def MinPages(lis, n, stud): sum = 0 - # If number of students are more than number of books, we'll return -1 because - # all the students won't get a book + # If number of students are more than number of books, we'll return -1 because all the students won't get a book if (n < stud): return -1 @@ -58,18 +51,15 @@ def MinPages(lis, n, stud): for i in range(n): sum += lis[i] - # Binary search will be applied over the number of pages - # low will be initialized with 0 while high will be initialized with the total number of pages - # ans variable will store the number of maximum pages that will be assigned to a student and - # because that should be minimum, it is initialized by a large integer value + # Binary search will be applied over the number of pages and low will be initialized with 0 while high will be initialized with the total number of pages + # ans variable will store the number of maximum pages that will be assigned to a student and because that should be minimum, it is initialized by a large integer value low, high = 0, sum ans = 10**9; #The loop will run until low is less than or equal to high while (low <= high): - # Considering middle element to be currently minimum - # and checking whether it is possible to allocate books using mid value as the minimum number of pages + # Considering middle element to be currently minimum and checking whether it is possible to allocate books using mid value as the minimum number of pages mid = (low + high) // 2 if (IsPossible(lis, n, stud, mid)): @@ -77,13 +67,11 @@ def MinPages(lis, n, stud): #If possible, them comparing the mid value with the answer that we have so far ans = min(ans, mid) - # Because the number of pages are given in ascending order and we are looking for the - # minimum value possible, so for the next possible answer, we will reduce our search by decreasing high to mid-1 + # Because the number of pages are given in ascending order and we are looking for the minimum value possible, so for the next possible answer, we will reduce our search by decreasing high to mid-1 high = mid - 1 else: - # If it is not possible to allocate books using mid value as the minimum value, then we will - # look for the minimum element in the other half that is low will be increased to mid + 1 + # If it is not possible to allocate books using mid value as the minimum value, then we will look for the minimum element in the other half that is low will be increased to mid + 1 low = mid + 1 #Returning the final answer From 8c7cfb7f6f1ae71a88db9009b1c643d738d519d3 Mon Sep 17 00:00:00 2001 From: Yashmita <55138349+yashmita@users.noreply.github.com> Date: Sat, 20 Mar 2021 04:07:46 +0530 Subject: [PATCH 3/9] Added code for right side view of binary tree --- Code/C++/Right_Side_View_of_Binary_Tree.cpp | 132 ++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 Code/C++/Right_Side_View_of_Binary_Tree.cpp diff --git a/Code/C++/Right_Side_View_of_Binary_Tree.cpp b/Code/C++/Right_Side_View_of_Binary_Tree.cpp new file mode 100644 index 000000000..94b56e9cc --- /dev/null +++ b/Code/C++/Right_Side_View_of_Binary_Tree.cpp @@ -0,0 +1,132 @@ +/* +PROBLEM STATEMENT: + +Given a binary tree, print the right side view of the tree. The right side view of the binary tree +includes the nodes which are visible when the tree is viewed from the right side. +For example: + 20 + / \ + 4 7 + / \ / + 1 8 34 + / \ + 64 32 + +Right side view: 20 7 34 32 +The input will be in the form of preorder and -1 denotes a null node. +*/ + + +#include +using namespace std; + +// Class for the structure of tree +class node{ + public: + //value stored at each node + int data; + //pointer to refer to the left child + node* left; + //pointer to refer the right child + node* right; + + //constructor for initializing values + node(int d){ + data = d; + left = NULL; + right = NULL; + } +}; + +//Function to build the tree and it returns the root of the tree +node* BuildTree(){ + + //user inputs the values in preorder and enters -1 to denote a null node + int d; + cin>>d; + if(d == -1) + return NULL; + + //A new node created with value d + node* root = new node(d); + + //Recursive call to the left and right child of the root + root->left = BuildTree(); + root->right = BuildTree(); + + return root; +} + +//Function to print the right side view of the binary tree +void RightView(node* root, int level, int &maxlevel){ + + //base case + if(root == NULL) + return; + + //checking the current level is greater than maxlevel so far + if(level > maxlevel){ + + //Displays the values which appear from the right side view of a tree + cout<data<<" "; + + //Updating maxlevel + maxlevel = level; + } + + //recursive cases for right and left childs + RightView(root->right, level + 1, maxlevel); + RightView(root->left, level + 1, maxlevel); +} + +//Driver function +int main(){ + + cout<<"enter elemnets: "; + + //Function call to build the tree + node* root = BuildTree(); + + //maxlevel ensures that no two nodes on the same level of the tree are printed + int maxlevel = -1; + + //Function call to display the right side view of the tree + //The parameters passed are: the root node, the 0th level of the tree, and maxlevel + RightView(root, 0, maxlevel); + + return 0; +} + + +/* +TEST CASES: + +1. +Input: 20 4 1 64 -1 -1 -1 8 -1 -1 7 34 -1 32 -1 -1 -1 +Output: 20 7 34 32 +Explanation: The tree will look like this + + 20 <---- + / \ + 4 7 <---- + / \ / + 1 8 34 <---- + / \ + 64 32 <---- + +2. +Input: 10 7 8 -1 -1 9 -1 -1 15 18 -1 -1 19 -1 -1 +Output: 10 15 19 +Explanation: The tree will look like this + + 10 <---- + / \ + 7 15 <---- + / \ / \ + 8 9 18 19 <---- + + +TIME COMPLEXITY: O(N), where N is the number of nodes in the tree. +SPACE COMPLEXITY: O(N), due to the stack space used during recursive calls. + +*/ \ No newline at end of file From a4758197fd463eb1c97073ec19c65c34748bc71d Mon Sep 17 00:00:00 2001 From: Yashmita <55138349+yashmita@users.noreply.github.com> Date: Sat, 20 Mar 2021 04:08:41 +0530 Subject: [PATCH 4/9] Delete Right_Side_View_of_Binary_Tree.cpp --- Code/C++/Right_Side_View_of_Binary_Tree.cpp | 132 -------------------- 1 file changed, 132 deletions(-) delete mode 100644 Code/C++/Right_Side_View_of_Binary_Tree.cpp diff --git a/Code/C++/Right_Side_View_of_Binary_Tree.cpp b/Code/C++/Right_Side_View_of_Binary_Tree.cpp deleted file mode 100644 index 94b56e9cc..000000000 --- a/Code/C++/Right_Side_View_of_Binary_Tree.cpp +++ /dev/null @@ -1,132 +0,0 @@ -/* -PROBLEM STATEMENT: - -Given a binary tree, print the right side view of the tree. The right side view of the binary tree -includes the nodes which are visible when the tree is viewed from the right side. -For example: - 20 - / \ - 4 7 - / \ / - 1 8 34 - / \ - 64 32 - -Right side view: 20 7 34 32 -The input will be in the form of preorder and -1 denotes a null node. -*/ - - -#include -using namespace std; - -// Class for the structure of tree -class node{ - public: - //value stored at each node - int data; - //pointer to refer to the left child - node* left; - //pointer to refer the right child - node* right; - - //constructor for initializing values - node(int d){ - data = d; - left = NULL; - right = NULL; - } -}; - -//Function to build the tree and it returns the root of the tree -node* BuildTree(){ - - //user inputs the values in preorder and enters -1 to denote a null node - int d; - cin>>d; - if(d == -1) - return NULL; - - //A new node created with value d - node* root = new node(d); - - //Recursive call to the left and right child of the root - root->left = BuildTree(); - root->right = BuildTree(); - - return root; -} - -//Function to print the right side view of the binary tree -void RightView(node* root, int level, int &maxlevel){ - - //base case - if(root == NULL) - return; - - //checking the current level is greater than maxlevel so far - if(level > maxlevel){ - - //Displays the values which appear from the right side view of a tree - cout<data<<" "; - - //Updating maxlevel - maxlevel = level; - } - - //recursive cases for right and left childs - RightView(root->right, level + 1, maxlevel); - RightView(root->left, level + 1, maxlevel); -} - -//Driver function -int main(){ - - cout<<"enter elemnets: "; - - //Function call to build the tree - node* root = BuildTree(); - - //maxlevel ensures that no two nodes on the same level of the tree are printed - int maxlevel = -1; - - //Function call to display the right side view of the tree - //The parameters passed are: the root node, the 0th level of the tree, and maxlevel - RightView(root, 0, maxlevel); - - return 0; -} - - -/* -TEST CASES: - -1. -Input: 20 4 1 64 -1 -1 -1 8 -1 -1 7 34 -1 32 -1 -1 -1 -Output: 20 7 34 32 -Explanation: The tree will look like this - - 20 <---- - / \ - 4 7 <---- - / \ / - 1 8 34 <---- - / \ - 64 32 <---- - -2. -Input: 10 7 8 -1 -1 9 -1 -1 15 18 -1 -1 19 -1 -1 -Output: 10 15 19 -Explanation: The tree will look like this - - 10 <---- - / \ - 7 15 <---- - / \ / \ - 8 9 18 19 <---- - - -TIME COMPLEXITY: O(N), where N is the number of nodes in the tree. -SPACE COMPLEXITY: O(N), due to the stack space used during recursive calls. - -*/ \ No newline at end of file From 7298ed75c46fce019742c049f022b1528e4b8491 Mon Sep 17 00:00:00 2001 From: Yashmita <55138349+yashmita@users.noreply.github.com> Date: Thu, 1 Apr 2021 19:43:06 +0530 Subject: [PATCH 5/9] Added Sudoku Solver --- Code/Java/Sudoku_Solver.java | 149 +++++++++++++++++++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 Code/Java/Sudoku_Solver.java diff --git a/Code/Java/Sudoku_Solver.java b/Code/Java/Sudoku_Solver.java new file mode 100644 index 000000000..91d91faa6 --- /dev/null +++ b/Code/Java/Sudoku_Solver.java @@ -0,0 +1,149 @@ +/* +PROBLEM STATEMENT: +Given an nxn sized incomplete Sudoku in which 0 represents an empty cell. The task is to print it's solution. +*/ + +import java.util.Scanner; +class Sudoku_Solver +{ + public static boolean CanPlace(int[][] mat, int n, int i, int j, int num) + { + //check for rows and columns + for(int x = 0; x < n; x++) + { + if(mat[x][j] == num || mat[i][x] == num) + return false; + } + + //check for the subgrid + int root = (int)Math.sqrt(n); + int sx = (i/root)*root; //starting x coordinate of a subgrid + int sy = (j/root)*root; //starting y coordinate of a subgrid + for(int x = sx; x < sx+root; x++) + { + for(int y = sy; y < sy+root; y++) + { + if(mat[x][y] == num) + return false; + } + } + + //else if it is possible to place num at mat[i][j], return true + return true; + } + + public static boolean SudokuSolver(int[][] mat, int n, int i, int j) + { + //base case + if(i == n) + { + //print solution + for (i = 0; i < n; i++) + { + for (j = 0; j < n; j++) + { + System.out.print(mat[i][j]); + System.out.print(" "); + } + System.out.print("\n"); + } + return true; + } + + //if a row ends, recursive call to next row + if(j == n) + return SudokuSolver(mat, n, i+1, 0); + + //if the cell is pre-filled, recursive call to the next cell + if(mat[i][j] != 0) + return SudokuSolver(mat, n, i, j+1); + + //Loop to fill any one of the number from 1 to n in a cell + for(int num = 1; num <= n; num++) + { + //A function call to check whether it is possible to place num in the given cell or not + if(CanPlace(mat, n, i, j, num)) + { + mat[i][j] = num; + //recursive call for the next cell + boolean solvenext = SudokuSolver(mat, n, i, j+1); + if(solvenext) + return true; + } + } + + //if none of the number can be placed then, backtrack + mat[i][j] = 0; + return false; + } + + // Driver Code + public static void main(String[] args) + { + //Size input from user + System.out.print("Enter size of the Sudoku: "); + Scanner sc = new Scanner(System.in); + int n = sc.nextInt(); + System.out.println("Enter elements of Sudoku: "); + int mat[][] = new int[n][n]; + + //Elements input from user + for(int i = 0; i < n; i++) + { + for(int j = 0; j < n; j++) + { + mat[i][j] = sc.nextInt(); + } + } + + //function call to solve sudoku + if(!SudokuSolver(mat, n, 0, 0)) + System.out.print("No Solution!"); + } +} + +/* +TEST CASES: +1. +Input: +Enter size of the Sudoku: 9 +Enter elements of Sudoku: +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 +Output: +3 1 6 5 7 8 4 9 2 +5 2 9 1 3 4 7 6 8 +4 8 7 6 2 9 5 3 1 +2 6 3 4 1 5 9 8 7 +9 7 4 8 6 3 1 2 5 +8 5 1 7 9 2 6 4 3 +1 3 8 9 4 7 2 5 6 +6 9 2 3 5 1 8 7 4 +7 4 5 2 8 6 3 1 9 + +2. +Input: +Enter size of the Sudoku: 9 +Enter elements of Sudoku: +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 4 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 +Output: +No Solution! + +TIME COMPLEXITY: O(9^(n*n)), as there will be 9 options from 1 to 9 to fill in an empty cell +SPACE COMPLEXITY: O(n*n) +*/ \ No newline at end of file From 4c5e824c84fc6f041b524dd0b716c2cb552f7a3f Mon Sep 17 00:00:00 2001 From: Yashmita <55138349+yashmita@users.noreply.github.com> Date: Thu, 1 Apr 2021 19:47:39 +0530 Subject: [PATCH 6/9] Update readme.md --- Algorithm/Backtracking/readme.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Algorithm/Backtracking/readme.md b/Algorithm/Backtracking/readme.md index 7f3007a4e..175b1cd47 100644 --- a/Algorithm/Backtracking/readme.md +++ b/Algorithm/Backtracking/readme.md @@ -28,3 +28,4 @@ unsolvable. ## Questions : * Permutation of a string ----> [C++](/Code/C++/permutation_of_a_string.cpp) +* Sudoku Solver ----> [Java](/Code/Java/Sudoku_Solver.java) From d99b8556f43de3d8715d2bc13335a968d9fa7966 Mon Sep 17 00:00:00 2001 From: Yashmita <55138349+yashmita@users.noreply.github.com> Date: Thu, 1 Apr 2021 19:52:50 +0530 Subject: [PATCH 7/9] Delete Book_Allocation.py --- Code/Python/Book_Allocation.py | 159 --------------------------------- 1 file changed, 159 deletions(-) delete mode 100644 Code/Python/Book_Allocation.py diff --git a/Code/Python/Book_Allocation.py b/Code/Python/Book_Allocation.py deleted file mode 100644 index cfccc2a63..000000000 --- a/Code/Python/Book_Allocation.py +++ /dev/null @@ -1,159 +0,0 @@ -''' -PROBLEM STATEMENT: - -Given an array of integers of size n, where n denotes number of books and each element of an array denotes the number of pages in the ith book, also given another integer -denoting number of students. The task is to allocate books to the given number of students so that maximum number of pages allocated to a student is minimum. A book will be -allocated to exactly one student and each student has to be allocated atleast one book. Allotment should be in contiguous order, for example, a student cannot be allocated -book 1 and book 3, skipping book 2. Calculate and return that minimum possible number. Return -1 if a valid assignment is not possible. -''' - -# Function to check whether it is possible to allocate books using the current minimum value -def IsPossible(lis, n, stud, minval): - - students = 1 - sumval = 0 - - # Iterating over the books - for i in range(n): - - # If the number of pages in a book is greater than the minimum value, then the minimum value chosen cannot be used to alloacte books - if (lis[i] > minval): - return False - - # Counting number of students required for allocating minval pages - if (sumval + lis[i] > minval): - - #incrementing students by 1 - students += 1 - - # Updating current value of sum - sumval = lis[i] - - # If more number of students are required than the given number of students, then also it is not possible to allocate books - if (students > stud): - return False - - else: - sumval += lis[i] - - return True - -# Function to find the maximum number of pages allocated to a student is minimum -def MinPages(lis, n, stud): - - sum = 0 - - # If number of students are more than number of books, we'll return -1 because all the students won't get a book - if (n < stud): - return -1 - - #Finding the sum of all the pages - for i in range(n): - sum += lis[i] - - # Binary search will be applied over the number of pages and low will be initialized with 0 while high will be initialized with the total number of pages - # ans variable will store the number of maximum pages that will be assigned to a student and because that should be minimum, it is initialized by a large integer value - low, high = 0, sum - ans = 10**9; - - #The loop will run until low is less than or equal to high - while (low <= high): - - # Considering middle element to be currently minimum and checking whether it is possible to allocate books using mid value as the minimum number of pages - mid = (low + high) // 2 - - if (IsPossible(lis, n, stud, mid)): - - #If possible, them comparing the mid value with the answer that we have so far - ans = min(ans, mid) - - # Because the number of pages are given in ascending order and we are looking for the minimum value possible, so for the next possible answer, we will reduce our search by decreasing high to mid-1 - high = mid - 1 - - else: - # If it is not possible to allocate books using mid value as the minimum value, then we will look for the minimum element in the other half that is low will be increased to mid + 1 - low = mid + 1 - - #Returning the final answer - return ans - -# Driver Code - -# User inputs the the number of books - -n = int(input("Enter the size of an array: ")) -lis = [] -print("Enter ", n ," elements:") - -# User inputs number of pages in each book in ascending order - -for i in range(n): - num = int(input()) - lis.append(num) - -# User inputs the number of students among which the books will be allocated - -stud = int(input("Enter the number of students: ")) - -# A function call to MinPages - -print("Minimum number of pages are: ", MinPages(lis, n, stud)) - - -''' -TEST CASES: - -1. -Input: -n = 4 -lis = [12, 34, 67, 90] -stud = 2 - -Output: 113 - -Explanation: -There are 2 students. Books can be allocated in the following way: -1) [12] and [34, 67, 90] -student 1 gets 12 pages -student 2 gets 34 + 67 + 90 = 191 pages -Maximum of the two = 191 -2) [12, 34] and [67, 90] -student 1 gets 12 + 34 = 36 pages -student 2 gets 67 + 90 = 157 pages -Maximum of the two = 157 pages -3) [12, 34, 67] and [90] -student 1 gets 12 + 34 + 67 = 113 pages -student 2 gets 90 pages -Maximum of the two = 113 pages - -Out of the 3 cases, Option 3 has the minimum pages = 113. - -2. -Input: -n = 4 -lis = [5, 17, 100, 11] -stud = 4 - -Output: 100 - -Explanation: -There are 4 students and 4 books. Therefore, each student will get one book with maximum -pages = 100. - -3. -Input: -n = 5 -lis = [34, 56, 67, 78, 89] -stud = 6 - -Output: -1 - -Explanation: -Because number of students are more than number of books. Therefore, there is no valid -assignment possible. - - -TIME COMPLEXITY: O(N*LOG(Sum of pages)) -SPACE COMPLEXITY: O(1) -''' - From 6885c74429a34ce73a2b29725551a1eec42e9744 Mon Sep 17 00:00:00 2001 From: Yashmita <55138349+yashmita@users.noreply.github.com> Date: Thu, 1 Apr 2021 19:53:42 +0530 Subject: [PATCH 8/9] Update Sudoku_Solver.java --- Code/Java/Sudoku_Solver.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Code/Java/Sudoku_Solver.java b/Code/Java/Sudoku_Solver.java index 91d91faa6..edec60c38 100644 --- a/Code/Java/Sudoku_Solver.java +++ b/Code/Java/Sudoku_Solver.java @@ -146,4 +146,4 @@ public static void main(String[] args) TIME COMPLEXITY: O(9^(n*n)), as there will be 9 options from 1 to 9 to fill in an empty cell SPACE COMPLEXITY: O(n*n) -*/ \ No newline at end of file +*/ From 1c8b0a5106d2ec22dc50db02ae5d4e89fe2320b4 Mon Sep 17 00:00:00 2001 From: Yashmita <55138349+yashmita@users.noreply.github.com> Date: Fri, 2 Apr 2021 01:33:51 +0530 Subject: [PATCH 9/9] Update Sudoku_Solver.java --- Code/Java/Sudoku_Solver.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Code/Java/Sudoku_Solver.java b/Code/Java/Sudoku_Solver.java index edec60c38..81401fc5f 100644 --- a/Code/Java/Sudoku_Solver.java +++ b/Code/Java/Sudoku_Solver.java @@ -6,6 +6,7 @@ import java.util.Scanner; class Sudoku_Solver { + //A function to check whether a number between 1 to n can be placed at the specified cell or not public static boolean CanPlace(int[][] mat, int n, int i, int j, int num) { //check for rows and columns @@ -32,6 +33,7 @@ public static boolean CanPlace(int[][] mat, int n, int i, int j, int num) return true; } + //A function to solve the Sudoku, once solved it prints the solution and returns true, otherwise false public static boolean SudokuSolver(int[][] mat, int n, int i, int j) { //base case @@ -144,6 +146,6 @@ public static void main(String[] args) Output: No Solution! -TIME COMPLEXITY: O(9^(n*n)), as there will be 9 options from 1 to 9 to fill in an empty cell -SPACE COMPLEXITY: O(n*n) +TIME COMPLEXITY: O(9^(n*n)), as there will be 9 options from 1 to 9 to fill in an empty cell. +SPACE COMPLEXITY: O(n*n), where n denotes the size of Sudoku grid */