Can anyone help me to find error in this code

72 views Asked by At
import java.util.Scanner;

public class MinMaxDivideConquer {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the number of elements: ");
        int n = scanner.nextInt();
        int[] arr = new int[n];
        System.out.println("Enter the elements: ");
        for (int i = 0; i < n; i++) {
            arr[i] = scanner.nextInt();
        }
        // Initialize parameters
        int i = 0;
        int j = n - 1;
        int min = arr[0];
        int max = arr[0];
        // Call the divide and conquer function
        findMinMax(arr, i, j, min, max);
        System.out.println("Minimum: " + min);
        System.out.println("Maximum: " + max);
    }

    public static void findMinMax(int[] arr, int i, int j, int min, int max) {
        if (i == j) {
            min = max = arr[i];
        } else if (i == j - 1) {
            if (arr[i] < arr[j]) {
                min = arr[i];
                max = arr[j];
            } else {
                min = arr[j];
                max = arr[i];
            }
        } else {
            int mid = (i + j) / 2;
            int min1 = 0;
            int max1 = 0;
            findMinMax(arr, i, mid, min, max);
            findMinMax(arr, mid + 1, j, min1, max1);
            if (min1 < min) {
                min = min1;
            }
            if (max1 > max) {
                max = max1;
            }
        }
    }
}

Using divide and conquer, I should get Max and min of the the array given by user but the first element is coming as Max and min.as the code should be basic and simple I am not using integer.min_value etc..,I couldn't find where the error was after these many trails .

2

There are 2 answers

0
Mohamed Ismail M On

you have called the recursive findMinMax method but it never returned and assigned back. In recursive the previous method data to be processed in the current method data. Next this min1 & max1 which are assigned zero , how about the second findMinMax called with same min max . there the logical mistakes happened. You have said its divide and conquer. In that sense you have divided into two groups lets say right and left form the left you have to find min & max and from the right you have to find min and max from the output of both left and right you have to check which is min and max, thats the last if else condition , there the failure arise in your code.

public class MinMaxDivideConquer {
public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    System.out.print("Enter the number of elements: ");
    int n = scanner.nextInt();
    int[] arr = new int[n];
    System.out.println("Enter the elements: ");
    for (int i = 0; i < n; i++) {
        arr[i] = scanner.nextInt();
    }

    // Initialize parameters using an array
    int i = 0;
    int j = n - 1;
    int[] minMax = findMinMax(arr, i, j);
    
    System.out.println("Minimum: " + minMax[0]);
    System.out.println("Maximum: " + minMax[1]);
}

public static int[] findMinMax(int[] arr, int i, int j) {
    int[] result = new int[2]; // Array to hold min and max

    if (i == j) {
        result[0] = arr[i]; // Minimum
        result[1] = arr[i]; // Maximum
    } else if (i == j - 1) {
        if (arr[i] < arr[j]) {
            result[0] = arr[i]; // Minimum
            result[1] = arr[j]; // Maximum
        } else {
            result[0] = arr[j]; // Minimum
            result[1] = arr[i]; // Maximum
        }
    } else {
        int mid = (i + j) / 2;
        int[] leftMinMax = findMinMax(arr, i, mid);
        int[] rightMinMax = findMinMax(arr, mid + 1, j);

        result[0] = Math.min(leftMinMax[0], rightMinMax[0]);
        result[1] = Math.max(leftMinMax[1], rightMinMax[1]);
    }

    return result;
}

}

0
Md Turin On

Lets break down the code,

In Java, primitive types are passed by value, so modifying min and max within the findMinMax method won't reflect the changes in the calling method. Instead, you should return an array or an object that contains the minimum and maximum values.

In this line of code, max and min value are not reflecting outside the method.

if (i == j) {
  min = max = arr[i];
}

Instead of this, we can return the array.

In the findMinMax method, you've assigned min1 and max1 to 0, which is incorrect. These should be initialized to the respective values from the array.

In this block of code, min1 and max1 are not initialized properly.

else {
  int mid = (i + j) / 2;
  int min1 = 0;
  int max1 = 0;
  findMinMax(arr, i, mid, min, max);
  findMinMax(arr, mid + 1, j, min1, max1);
  if (min1 < min) {
    min = min1;
  }
  if (max1 > max) {
    max = max1;
  }
}

Last of all, we could optimized code slightly by removing this block, it can be handle by last else block of code.

else if (i == j - 1) {
  if (arr[i] < arr[j]) {
    min = arr[i];
    max = arr[j];
  } else {
    min = arr[j];
    max = arr[i];
  }
}

So our full code:

import java.util.Scanner;
public class MinMaxDivideConquer {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the number of elements: ");
        int n = scanner.nextInt();
        int[] arr = new int[n];
        System.out.println("Enter the elements: ");
        for (int i = 0; i < n; i++) {
            arr[i] = scanner.nextInt();
        }
        // Call the divide and conquer function
        int[] result = findMinMax(arr, 0, n - 1);
        System.out.println("Minimum: " + result[0]);
        System.out.println("Maximum: " + result[1]);
    }

    public static int[] findMinMax(int[] arr, int i, int j) {
       int[] result = new int[2];
       if (i == j) {
           result[0] = arr[i]; 
           result[1] = arr[i];
       } else {
           int mid = (i + j) / 2;
           int[] leftResult = findMinMax(arr, i, mid);
           int[] rightResult = findMinMax(arr, mid + 1, j);
           result[0] = Math.min(leftResult[0], rightResult[0]); // Minimum
           result[1] = Math.max(leftResult[1], rightResult[1]); // Maximum
       }
       return result;
    }
}