image_2021-10-18_14-59-46.png
45.2 KB
#N1464. Maximum Product of Two Elements in an Array
problem link=>https://leetcode.com/problems/maximum-product-of-two-elements-in-an-array/
#solution
P.s. The code of Bubble sort is just written in the comments for note)
problem link=>https://leetcode.com/problems/maximum-product-of-two-elements-in-an-array/
#solution
class Solution {
public int maxProduct(int[] nums) {
/*int temp, n=nums.length; //Bubble Sort
for(int i=1; i<n; i++){
for(int j=0; j<n-i; j++){
if(nums[j]>nums[j+1]){
temp=nums[j];
nums[j]=nums[j+1];
nums[j+1]=temp;
}
}
}*/
Arrays.sort(nums);
return (nums[nums.length-2]-1)*(nums[nums.length-1]-1);
}
}
‼️P.s. I simply used the sort() method of the Arrays class to sort array rather than using Bubble sort. Because I checked that sort() method is more efficent than Bubble sort.P.s. The code of Bubble sort is just written in the comments for note)