diff --git a/Java/containerWithMostWater.java b/Java/containerWithMostWater.java new file mode 100644 index 000000000..2301838c7 --- /dev/null +++ b/Java/containerWithMostWater.java @@ -0,0 +1,49 @@ +import java.util.*; +import java.util.Scanner; + +public class ContainerWithMostWater { + + public static void main(String[] args) { + + Scanner sc = new Scanner(System.in); + + // Get inputs + System.out.println("Enter the number of lines:"); + int n = sc.nextInt(); + + int[] height = new int[n]; + System.out.println("Enter the heights of the lines:"); + for (int i = 0; i < n; i++) { + height[i] = sc.nextInt(); + } + + int maxArea = findMaxArea(height); + + System.out.println("The maximum water that can be contained is: " + maxArea); + } + + + public static int findMaxArea(int[] height) { + int left = 0; + int right = height.length - 1; + int maxArea = 0; + + while (left < right) { + int width = right - left; + int minHeight = Math.min(height[left], height[right]); + int area = width * minHeight; + + maxArea = Math.max(maxArea, area); + + // Move the pointer pointing to the smaller line + if (height[left] < height[right]) { + left++; + } else { + right--; + } + } + + return maxArea; + } +} + diff --git a/Java/twoSum.java b/Java/twoSum.java new file mode 100644 index 000000000..9835fb011 --- /dev/null +++ b/Java/twoSum.java @@ -0,0 +1,44 @@ +package twosum; + +import java.util.Scanner; + +public class twoSum { + + public static void main(String[] args) { + + Scanner sc = new Scanner(System.in); + + // Get inputs + System.out.println("Enter the size of the array:"); + int n = sc.nextInt(); + + int[] arr = new int[n]; + System.out.println("Enter " + n + " integers:"); + for (int i = 0; i < n; i++) { + arr[i] = sc.nextInt(); + } + + System.out.println("Enter the target sum:"); + int target = sc.nextInt(); + + boolean found = false; + + // Find two numbers that add up to target + for (int i = 0; i < n; i++) { + for (int j = i + 1; j < n; j++) { + if (arr[i] + arr[j] == target) { + System.out.println("Two numbers found at indices " + i + " and " + j); + System.out.println("Numbers are: " + arr[i] + " + " + arr[j] + " = " + target); + found = true; + break; + } + } + if (found) break; + } + + if (!found) { + System.out.println("No two numbers found with the given target sum."); + } + } +} + diff --git a/Java/validParentheses.java b/Java/validParentheses.java new file mode 100644 index 000000000..1420f5f30 --- /dev/null +++ b/Java/validParentheses.java @@ -0,0 +1,46 @@ +import java.util.Scanner; +import java.util.Stack; + +public class validParentheses { + + public static void main(String[] args) { + + Scanner sc = new Scanner(System.in); + + // Get input + System.out.println("Enter a string of brackets:"); + String str = sc.nextLine(); + + boolean isValid = checkValidParentheses(str); + + if (isValid) { + System.out.println("The parentheses are valid!"); + } else { + System.out.println("The parentheses are NOT valid!"); + } + } + + + public static boolean checkValidParentheses(String s) { + Stack stack = new Stack<>(); + + for (char ch : s.toCharArray()) { + if (ch == '(' || ch == '{' || ch == '[') { + stack.push(ch); + } else if (ch == ')' || ch == '}' || ch == ']') { + if (stack.isEmpty()) return false; + + char top = stack.pop(); + + if ((ch == ')' && top != '(') || + (ch == '}' && top != '{') || + (ch == ']' && top != '[')) { + return false; + } + } + } + + return stack.isEmpty(); + } +} +