How to find maximum two number from given arrays of numbers without using Extra Space.

How to find maximum two number from given arrays of numbers without using Extra Space.
SOURCE : MaxmTwoNumber.java

public class MaxmTwoNumber {
public static void main(String[] args) {
Integer i[] = { 22, 76, 8, 78, 89, 65, 45, 87 };

MaxmTwoNumber mm = new MaxmTwoNumber();
mm.maxn(i);

}

public void maxn(Integer[] num) {

int maxone = 0;
int maxtwo = 0;
for (int n : num) {
if (maxone < n) {
maxtwo = maxone;
maxone = n;

} else {
if (maxtwo < n) {
maxtwo = n;
}

}

}

System.out.println("highest ::" + maxone);
System.out.println("2nd highest ::" + maxtwo);
}

}



INPUT

Integer i[] = { 22, 76, 8, 78, 89, 65, 45, 87 };



OUTPUT

highest ::89
2nd highest ::87







Sandeep Kumar D

Hi, I have written and developed this post so that most of people will be benefited. I'm committed to provide easy and in-depth tutorials on various technologies.I hope it will help you a lot.

- Sandeep Kumar D

Follow Me @Google+




SHARE

    Blogger Comment
    Facebook Comment

0 comments :

Post a Comment