Skip to content

Commit 7b9e417

Browse files
author
Prashant Jain
committed
Added problems in Two-Pointer
1 parent 3ec1483 commit 7b9e417

2 files changed

Lines changed: 104 additions & 0 deletions

File tree

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package in.knowledgegate.dsa.twopointer;
2+
3+
/**
4+
* You are given an integer array height of length n.
5+
* There are n vertical lines drawn such that
6+
* the two endpoints of the ith line are (i, 0)
7+
* and (i, height[i]).
8+
*
9+
* Find two lines that together with the x-axis form
10+
* a container, such that the container contains
11+
* the most water.
12+
*
13+
* Return the maximum amount of water a container
14+
* can store.
15+
*
16+
* Notice that you may not slant the container.
17+
*
18+
* Example 1:
19+
* Input: height = [1,8,6,2,5,4,8,3,7]
20+
* Output: 49
21+
* Explanation: The above vertical lines are
22+
* represented by array [1,8,6,2,5,4,8,3,7]. In this case,
23+
* the max area of water the container can contain is 49.
24+
*
25+
* Example 2:
26+
* Input: height = [1,1]
27+
* Output: 1
28+
*
29+
*
30+
* Constraints:
31+
* n == height.length
32+
* 2 <= n <= 105
33+
* 0 <= height[i] <= 104
34+
*/
35+
public class ContainerWithMostWater {
36+
public static void main(String[] args) {
37+
ContainerWithMostWater water =
38+
new ContainerWithMostWater();
39+
int[] height = new int[]{1,8,6,2,5,4,8,3,7};
40+
System.out.println("Output is:" + water.maxArea(height));
41+
}
42+
43+
public int maxArea(int[] height) {
44+
int maxArea = 0;
45+
int left = 0, right = height.length - 1;
46+
while (left < right) {
47+
int minHeight = Math.min(height[left],
48+
height[right]);
49+
int currArea =
50+
minHeight * (right - left);
51+
maxArea = Math.max(maxArea, currArea);
52+
53+
if (height[left] < height[right]) {
54+
left++;
55+
} else {
56+
right--;
57+
}
58+
}
59+
60+
return maxArea;
61+
}
62+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package in.knowledgegate.dsa.twopointer;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* Given an integer array nums, move all 0's to the
7+
* end of it while maintaining the relative order
8+
* of the non-zero elements.
9+
*
10+
* Note that you must do this in-place without making
11+
* a copy of the array.
12+
*
13+
* Example 1:
14+
* Input: nums = [0,1,0,3,12]
15+
* Output: [1,3,12,0,0]
16+
*
17+
* Example 2:
18+
* Input: nums = [0]
19+
* Output: [0]
20+
*
21+
* Constraints:
22+
* 1 <= nums.length <= 104
23+
* -231 <= nums[i] <= 231 - 1
24+
*/
25+
public class MoveZeros {
26+
public static void main(String[] args) {
27+
MoveZeros zeros = new MoveZeros();
28+
int[] nums = new int[]{0,1,0,3,12};
29+
zeros.moveZeroes(nums);
30+
System.out.println("Output is:" + Arrays.toString(nums));
31+
}
32+
33+
public void moveZeroes(int[] nums) {
34+
int prev = 0;
35+
for (int i = 0 ; i < nums.length; i++) {
36+
if (nums[i] != 0) {
37+
nums[prev++] = nums[i];
38+
nums[i] = 0;
39+
}
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)