Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 21dcb80

Browse files
added maxDifference in an array
1 parent 83b5242 commit 21dcb80

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

‎ArraysAndStrings/Tutorial.md‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,23 @@ public boolean findNum (int[][] arr, int num) {
196196
**Find the first non repeating character in a String**
197197

198198
**Find all the palindrome pairs in an array of words**
199+
200+
**Find maximum difference in an array i.e a[j] -a[i] such that index j > index i**
201+
202+
```
203+
public int maxDiff(int[] a) {
204+
int maxDifference = -1;
205+
int currDifference = -1;
206+
int minNumber = a[0];
207+
208+
for(int i = 0; i < a.length; i++) {
209+
currDifference = a[i] - minNumber;
210+
if (maxDifference < currDifference)
211+
maxDifference = currDifference;
212+
if (minNumber > a[i])
213+
minNumber = a[i];
214+
}
215+
216+
return maxDifference;
217+
}
218+
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /