|
| 1 | +package com.maharishiuniversity.admit; |
| 2 | + |
| 3 | +/* |
| 4 | + 3. An array is defined to be complete if the conditions (a), (d) and (e) below hold. |
| 5 | + a. The array contains even numbers |
| 6 | + b. Let min be the smallest even number in the array. |
| 7 | + c. Let max be the largest even number in the array. |
| 8 | + d. min does not equal max |
| 9 | + e. All numbers between min and max are in the array |
| 10 | + */ |
| 11 | + |
| 12 | +public class CompleteArray { |
| 13 | + |
| 14 | + static int isComplete(int[] a) { |
| 15 | + |
| 16 | + boolean hasEven = false; |
| 17 | + int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE; |
| 18 | + |
| 19 | + for (int num : a) { |
| 20 | + |
| 21 | + if (num % 2 == 0) { |
| 22 | + if (hasEven == false) { |
| 23 | + hasEven = true; |
| 24 | + } |
| 25 | + if (num > max) { |
| 26 | + max = num; |
| 27 | + } |
| 28 | + if (num < min) { |
| 29 | + min = num; |
| 30 | + } |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + if (hasEven == false) |
| 35 | + return 0; // non-complete array |
| 36 | + |
| 37 | + if (min == max) { |
| 38 | + return 0; // non-complete array |
| 39 | + } else { |
| 40 | + // checking condition (e) |
| 41 | + // checking all numbers between max and min in given array |
| 42 | + for (int val = min + 1; val <= max; val++) { |
| 43 | + |
| 44 | + boolean exists = false; |
| 45 | + |
| 46 | + // see if val is in array |
| 47 | + for (int i = 0; i < a.length; i++) { |
| 48 | + |
| 49 | + if (val == a[i]) { |
| 50 | + exists = true; |
| 51 | + break; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + if (exists == false) |
| 56 | + return 0; |
| 57 | + } |
| 58 | + } |
| 59 | + return 1; // complete-array |
| 60 | + } |
| 61 | + |
| 62 | + public static void main(String[] args) { |
| 63 | + int[] arr = { -5, 6, 2, 3, 2, 4, 5, 11, 8, 7 }; |
| 64 | + int[] arr2 = { 5, 7, 9, 13 }; |
| 65 | + |
| 66 | + System.out.println(isComplete(arr)); |
| 67 | + System.out.println(isComplete(arr2)); |
| 68 | + } |
| 69 | +} |
0 commit comments