diff --git a/Task 1/Container with most water/Question.md b/Task 1/Container with most water/Question.md index bac07de7..689d67c2 100644 --- a/Task 1/Container with most water/Question.md +++ b/Task 1/Container with most water/Question.md @@ -1,3 +1,34 @@ -Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of the line i is at (i, ai) and (i, 0). Find two lines, which, together with the x-axis forms a container, such that the container contains the most water. +Given an array of n non-negative integers a1, a2, ..., an , where each element represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of a line i is at (i, ai) and (i, 0). Find two lines, which, together with the x-axis forms a container, such that the container contains the most water. Note: You may not slant the container. + +Example:
+a) Input: n=9
+lines = [1,8,6,2,5,4,8,3,7]
+Output: 49
+Explanation: The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water the container can contain is 49 which lies between the index(1,8) i.e, between the line 8 at 1st position and line 7 at 8th position. + + +b) Input: n=5
+lines = [4,3,2,1,4]
+Output: 16
+Explanation: The container containing most water lies between the index 0 and 4 i.e, between the line 4 at 0th position and line 4 at 4th position. + + +Approach: Two Pointer Approach + +The max area is calculated by the following formula: +Area= (j - i) * min(lines[i], lines[j])
+ +We should choose (i, j) so that Area is max. Note that i, j go through the range (1, n) and j > i. +The simple way is to take all possibilities of (i, j) and compare all obtained Area. The time complexity will be O(n)^2. + +But, What we gonna do is to choose all possibilities of (i, j) in a wise way. If: +lines[i] < lines[j] we will check the next (i+1, j) (or move i to the right) +lines[i] >= lines[j] we will check the next (i, j-1) (or move j to the left) +Here is the explaination for that: +When lines[i] < lines[j] , we don't need to calculate all (i, j-1), (i, j-2), .... Why? because these max areas will be smaller than our Area at (i, j). + +NOTE: The Time complexity of this solution is O(n). + + diff --git a/Task 1/Next_Greater_Element_II/NextGreaterSet.java b/Task 1/Next_Greater_Element_II/NextGreaterSet.java new file mode 100644 index 00000000..a5d5f67d --- /dev/null +++ b/Task 1/Next_Greater_Element_II/NextGreaterSet.java @@ -0,0 +1,69 @@ +import java.util.Arrays; + +public class NextGreaterSet +{ + // Utility function to swap two digit + static void swap(char ar[], int i, int j) + { + char temp = ar[i]; + ar[i] = ar[j]; + ar[j] = temp; + } + static void findNext(char ar[], int n) + { + int i; + + // I) Start from the right most digit + // and find the first digit that is smaller + // than the digit next to it. + for (i = n - 1; i > 0; i--) + { + if (ar[i] > ar[i - 1]) { + break; + } + } + + // If no such digit is found, then all + // digits are in descending order means + // there cannot be a greater number with + // same set of digits + if (i == 0) + { + System.out.println("Not possible"); + } + else + { + int x = ar[i - 1], min = i; + + // II) Find the smallest digit on right + // side of (i-1)'th digit that is greater + // than number[i-1] + for (int j = i + 1; j < n; j++) + { + if (ar[j] > x && ar[j] < ar[min]) + { + min = j; + } + } + + // III) Swap the above found smallest + // digit with number[i-1] + swap(ar, i - 1, min); + + // IV) Sort the digits after (i-1) + // in ascending order + Arrays.sort(ar, i, n); + System.out.print("Next number with same" + + " set of digits is "); + for (i = 0; i < n; i++) + System.out.print(ar[i]); + } + } + + public static void main(String[] args) + { + char digits[] = { '5','3','4','9','7','6' }; + int n = digits.length; + findNext(digits, n); + } +} diff --git a/Task 1/Next_Greater_Element_II/greater_number_II.md b/Task 1/Next_Greater_Element_II/greater_number_II.md new file mode 100644 index 00000000..3fd5f423 --- /dev/null +++ b/Task 1/Next_Greater_Element_II/greater_number_II.md @@ -0,0 +1,16 @@ +Given a number n, find the smallest number that has the same set of digits as n and is greater than n. If n is the greatest possible number with its set of digits, then print “not possible”. + +Examples: +For simplicity of implementation, we have considered the input number as a string. + +Input: n = "218765" +Output: "251678" + +Input: n = "1234" +Output: "1243" + +Input: n = "4321" +Output: "Not Possible" + +Input: n = "534976" +Output: "536479" \ No newline at end of file