Skip to content

Commit 4576cd2

Browse files
solves maximum product of two elements in a array
1 parent b7fc421 commit 4576cd2

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@
371371
| 1450 | [Number of Students Doing Homework at Given Time](https://leetcode.com/problems/number-of-students-doing-homework-at-a-given-time) | [![Java](assets/java.png)](src/NumberOfStudentsDoingHomeworkAtGivenTime.java) | |
372372
| 1455 | [Check If Word Occurs as Prefix of any Word in Sentence](https://leetcode.com/problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence) | [![Java](assets/java.png)](src/CheckIfAWordOccursAsAPrefixOfAnyWordInASentence.java) | |
373373
| 1460 | [Make 2 Arrays Equal by Reversing Sub Arrays](https://leetcode.com/problems/make-two-arrays-equal-by-reversing-sub-arrays) | [![Java](assets/java.png)](src/MakeTwoArraysEqualByReversingSubArrays.java) | |
374-
| 1464 | [Maximum Product of 2 Elements in Array](https://leetcode.com/problems/maximum-product-of-two-elements-in-an-array) | | |
374+
| 1464 | [Maximum Product of 2 Elements in Array](https://leetcode.com/problems/maximum-product-of-two-elements-in-an-array) | [![Java](assets/java.png)](src/MaximumProductOfTwoElementsInArray.java) | |
375375
| 1469 | [Find All Lonely Nodes](https://leetcode.com/problems/find-all-the-lonely-nodes) | | |
376376
| 1470 | [Shuffle The Array](https://leetcode.com/problems/shuffle-the-array) | | |
377377
| 1474 | [Delete N Nodes After M Nodes In A Linked List](https://leetcode.com/problems/delete-n-nodes-after-m-nodes-of-a-linked-list) | | |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
public class MaximumProductOfTwoElementsInArray {
2+
public int maxProduct(int[] array) {
3+
int max = array[0], secondMax = Integer.MIN_VALUE;
4+
for (int index = 1 ; index < array.length ; index++) {
5+
if (array[index] > max) {
6+
secondMax = max;
7+
max = array[index];
8+
} else if (array[index] > secondMax) {
9+
secondMax = array[index];
10+
}
11+
}
12+
return (max - 1) * (secondMax - 1) ;
13+
}
14+
}

0 commit comments

Comments
 (0)