I need help removing Min M and refixing this code to work properly. class Min { public int max, min; public Min(int max, int min) { this.max = max; this.min = min; } } class Main { public static void findMinElement(int[] nums, int left, int right, Min m) { if (left == right) { if (m.min > nums[right]) { m.min = nums[right]; } return; } if (right - left == 1) { if (nums[left] < nums[right]) { if (m.min > nums[left]) { m.min = nums[left]; } } else { if (m.min > nums[right]) { m.min = nums[right]; } } return; } int mid = (left + right) / 2; findMinElement(nums, left, mid, m); findMinElement(nums, mid + 1, right, m); } public static void main(String[] args) { int[] nums = { 16, 10, 6, 3, 2, 4, 8, 12, 15, 20 }; Min m = new Min(Integer.MIN_VALUE, Integer.MAX_VALUE); findMinElement(nums, 0, nums.length - 1, m); System.out.println("The minimum array element is " + m.min); } }
I need help removing Min M and refixing this code to work properly.
class Min
{
public int max, min;
public Min(int max, int min)
{
this.max = max;
this.min = min;
}
}
class Main
{
public static void findMinElement(int[] nums, int left, int right, Min m)
{
if (left == right)
{
if (m.min > nums[right]) {
m.min = nums[right];
}
return;
}
if (right - left == 1)
{
if (nums[left] < nums[right])
{
if (m.min > nums[left]) {
m.min = nums[left];
}
}
else {
if (m.min > nums[right]) {
m.min = nums[right];
}
}
return;
}
int mid = (left + right) / 2;
findMinElement(nums, left, mid, m);
findMinElement(nums, mid + 1, right, m);
}
public static void main(String[] args)
{
int[] nums = { 16, 10, 6, 3, 2, 4, 8, 12, 15, 20 };
Min m = new Min(Integer.MIN_VALUE, Integer.MAX_VALUE);
findMinElement(nums, 0, nums.length - 1, m);
System.out.println("The minimum array element is " + m.min);
}
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images