12)package Backtracking;
public class Minmax {
public static int min(int arr[]){
int min = Integer.MAX_VALUE;
for(int i=0;i<=arr.length-1;i++){
if(arr[i] < min){
min = arr[i];
}
}
return min;
}
public static int max(int arr[]){
int max = Integer.MIN_VALUE;
for(int i=0;i<=arr.length-1;i++){
if(arr[i] > max){
max = arr[i];
}
}
return max;
}
public static void main(String[] args) {
int arr[] ={ 5,56,3,67,24,7};
System.out.println(max(arr)) ;
System.out.println(min(arr)) ;
}
}
13)package Backtracking;
public class Minmax {
public static void reverseArray(int arr[],int start,int end){
int temp;
if(start > end){
return ;
}
temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
reverseArray(arr, start+1, end-1);
}
public static void printArray(int arr[]){
for(int i=0;i<arr.length;i++){
System.out.print(arr[i] +" ");
}
}
public static void main(String[] args) {
int arr[] ={ 5,56,3,67,24,7};
reverseArray(arr, 0, arr.length-1);
printArray(arr);
}
}
14)class Solution {
public int maxSubArray(int[] nums) {
// Initialize the variables with the first element of the array
int maxSub = 0;
int maximum = 0;
// Iterate through the array starting from the second element
for (int i = 0; i < nums.length; i++) {
// Update maximum to the maximum of the current element or the sum of
maximum and the current element
if (maximum + nums[i] > nums[i]) {
maximum = maximum + nums[i];
} else {
maximum = nums[i];
}
// Update maxSub to the maximum of itself or maximum
if (maximum > maxSub) {
maxSub = maximum;
}
}
// Return the maximum subarray sum found
return maxSub;
}
}
15)import java.util.Arrays;
class Solution {
public boolean containsDuplicate(int[] nums) {
Arrays.sort(nums); // Sort the array
for (int i = 1; i < nums.length; i++) {
if (nums[i] == nums[i - 1]) {
return true; // Found a duplicate
}
}
return false; // No duplicates found
}
}
16)class Solution{
public long findMinDiff (ArrayList<Integer> a, int n, int m)
{
Collections.sort(a);
int start = 0,end =0;
// Largest number of chocolates
int mind = Integer.MAX_VALUE;
// Find the subarray of size m such that
// difference between last (maximum in case
// of sorted) and first (minimum in case of
// sorted) elements of subarray is minimum.
for(int i=0; i+m-1<n; i++)
{
int diff = a.get(i+m-1) - a.get(i);
if(mind>diff)
{
mind = diff;
start = i;
end = i+m-1;
}
}
return a.get(end)-a.get(start);
}
}
17)class Solution {
public int search(int[] nums, int target) {
int left = 0;
int right = nums.length - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (nums[mid] == target) {
return mid;
}
// Check which side is properly sorted
if (nums[left] <= nums[mid]) {
// Left side is sorted
if (nums[left] <= target && target < nums[mid]) {
right = mid - 1;
} else {
left = mid + 1;
}
} else {
// Right side is sorted
if (nums[mid] < target && target <= nums[right]) {
left = mid + 1;
} else {
right = mid - 1;
}
}
}
return -1; // Target not found
}
}
19)public class Solution {
// Method to calculate the maximum profit with a single buy-sell transaction
public static int maxProfit(int[] prices) {
int buyPrice = Integer.MAX_VALUE;
int maxProfit = 0;
for (int i = 0; i < prices.length; i++) {
if (buyPrice < prices[i]) {
int profit = prices[i] - buyPrice;
maxProfit = Math.max(maxProfit, profit);
} else {
buyPrice = prices[i];
}
}
return maxProfit;
}
public static void main(String[] args) {
int[] prices = {7, 1, 5, 3, 6, 4};
System.out.println("Max profit is " + maxProfit(prices));
}
}
22)public class HelloWorld {
public static int trappedRainwater(int height[]) {
int n = height.length;
int leftMax[] = new int[n];
leftMax[0] = height[0];
for(int i=1;i<n;i++){
leftMax[i] = Math.max(height[i],leftMax[i-1] );
}
int rightMax [] = new int[n];
rightMax[n-1] = height[n-1];
for(int i=n-2;i>=0;i--){
rightMax[i] = Math.max(rightMax[i+1], height[i]);
}
int trappedWater= 0;
for(int i =0;i<n;i++){
int waterLevel = Math.min(leftMax[i], rightMax[i])-height[i];
trappedWater = trappedWater + waterLevel ;
}
return trappedWater ;
}
public static void main(String[] args) {
int height[] = {4,2,0,6,3,2,5};
System.out.println("Amount of water trapped" + trappedRainwater(height));
}
}
101)package Backtracking;
public class RatInMaze {
// Function to print the solution path in the maze matrix
public static void printSolution(int maze[][]) {
for (int i = 0; i < maze.length; i++) {
for (int j = 0; j < maze[i].length; j++) {
System.out.print(maze[i][j] + " ");
}
System.out.println();
}
}
// Function to check if maze[row][col] is a valid move
public static boolean isSafe(int maze[][], int row, int col) {
// Return true if row and col are within maze boundaries and the cell is
not blocked
return (row >= 0 && row < maze.length && col >= 0 && col < maze[row].length
&& maze[row][col] == 1);
}
// Function to solve the maze problem using backtracking
public static boolean solveMaze(int maze[][]) {
if (solveMazeUtil(maze, 0, 0)) {
printSolution(maze);
return true;
} else {
System.out.println("Solution doesn't exist");
return false;
}
}
// Utility function to solve the maze problem using backtracking
public static boolean solveMazeUtil(int maze[][], int row, int col) {
// If the rat has reached the destination
if (row == maze.length - 1 && col == maze[row].length - 1 && maze[row][col]
== 1) {
maze[row][col] = 9; // Mark the destination
return true;
}
// Check if maze[row][col] is a valid move
if (isSafe(maze, row, col)) {
// Mark row, col as part of the solution path
maze[row][col] = 9;
// Move down in the maze
if (solveMazeUtil(maze, row + 1, col))
return true;
// Move right in the maze
if (solveMazeUtil(maze, row, col + 1))
return true;
// Move up in the maze
if (solveMazeUtil(maze, row - 1, col))
return true;
// Move left in the maze
if (solveMazeUtil(maze, row, col - 1))
return true;
// If none of the above movements work then backtrack
maze[row][col] = 1;
return false;
}
return false;
}
public static void main(String args[]) {
int maze[][] = {
{ 1, 0, 0, 0 },
{ 1, 1, 0, 1 },
{ 0, 1, 0, 0 },
{ 1, 1, 1, 1 }
};
solveMaze(maze);
}