java - check number of maximum and minimum element in a array -
i trying check whether given array has equal number of maximum , minimum array element. if number equal should return 1 else return 0. instead of return zero.
could please me?
public class maxminequal { public static void main(string[] args) { system.out.println(maxminequal.ismaxminequal(new int[]{11, 4, 9, 11, 8, 5, 4, 10})); system.out.println(maxminequal.ismaxminequal(new int[]{11, 11, 4, 9, 11, 8, 5, 4, 10})); } public static int ismaxminequal(int[] a) { int maxcount = 0; int mincount = 0; int largest = a[0]; (int = 0; < a.length; i++) { if (a[i] > largest) { largest = a[i]; } if (a[i] == largest) { maxcount = maxcount + 1; } } int smallest = a[0]; (int j = 0; j < a.length; j++) { if (a[j] < smallest) { smallest = a[j]; } if (a[j] == smallest) { mincount = mincount + 1; } } if (maxcount == mincount) { return 1; } else { return 0; } } }
you did not reset maxcount , mincount when find greater or smaller value.
public static boolean ismaxminequal(int[] a) { int maxcount, mincount = 0; int largest, smallest = a[0]; (int = 0: a) { if (i > largest) { largest = i; maxcount = 0; } if (i == largest) { maxcount++; } if (i < smallest) { smallest = i; mincount = 0; } if (i == smallest) { mincount++; } } return maxcount == mincount; }
Comments
Post a Comment