diff --git a/Java/Arrays/TrappingRainWater.java b/Java/Arrays/TrappingRainWater.java new file mode 100644 index 0000000..47de8ab --- /dev/null +++ b/Java/Arrays/TrappingRainWater.java @@ -0,0 +1,45 @@ +import java.util.Scanner; + +class Solution { + public static int trap(int[] height) { + int n = height.length; + int res = 0; + int[] lmax = new int [n]; + int[] rmax = new int [n]; + lmax[0]=height[0]; + for(int i = 1;i=0;i--){ + rmax[i]=Math.max(height[i],rmax[i+1]); + } + + for(int i =1;i= nums[low] && target < nums[mid]) { + high = mid - 1; + } else { + low = mid + 1; + } + } else { + if (target > nums[mid] && target <= nums[high]) { + low = mid + 1; + } else { + high = mid - 1; + } + } + } + + return -1; + } + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + SearchInRotatedSortedArray searchObj = new SearchInRotatedSortedArray(); + + System.out.print("Enter the number of elements in the rotated sorted array: "); + int n = scanner.nextInt(); + int[] nums = new int[n]; + + System.out.println("Enter the elements of the rotated sorted array:"); + for (int i = 0; i < n; i++) { + nums[i] = scanner.nextInt(); + } + + System.out.print("Enter the target element to search for: "); + int target = scanner.nextInt(); + + int result = searchObj.search(nums, target); + + if (result != -1) { + System.out.println("Target element " + target + " is found at index " + result); + } else { + System.out.println("Target element " + target + " is not found in the array."); + } + + scanner.close(); + } +} \ No newline at end of file