How to find maximum two number from given arrays of numbers without using Extra Space.
SOURCE : MaxmTwoNumber.javapublic 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); }}INPUTInteger i[] = { 22, 76, 8, 78, 89, 65, 45, 87 };OUTPUThighest ::892nd highest ::87

0 comments :
Post a Comment