## Thursday, December 30, 2010

### Hollow Array, Flip Flop Array & Rhino Number Answers in Java

1. An array is said to be hollow if it contains 3 or more zeros in the middle that are preceded and followed by the same number of non-zero elements. Furthermore, all the zeroes in the array must be in the middle of the array. Write a function named isHollow that accepts an integer array and returns 1 if it is a hollow array, otherwise it returns 0.
Examples:
 if the input array is is hollow? reason {1,2,0,0,0,3,4} yes 2 non-zeros precede and follow 3 zeros in the middle {1,1,1,1,0,0,0,0,0,2,1,2,18} yes 4 non-zeros precede and follow the 5 zeros in the middle {1, 2, 0, 0, 3, 4} no There are only 2 zeroes in the middle; at least 3 are required {1,2,0,0,0,3,4,5} no The number of preceding non-zeros(2) is not equal to the number of following non-zeros(3) {3,8,3,0,0,0,3,3} no The number of preceding non-zeros(3) is not equal to the number of following non-zeros(2) {1,2,0,0,0,3,4,0} no Not all zeros are in the middle {0,1,2,0,0,0,3,4} no Not all zeros are in the middle {0,0,0} yes The number of preceding non-zeros is 0 which equals the number of following non-zeros. And there are three zeros "in the middle".

Code To Find Hollow Array:

public class Hollow {
public static void main(String args[]) {
int a[] = {1,1,1,1,0,0,0,2,1,2,18};
System.out.println(isHollow(a));
}

static int isHollow(int a[]) {
int precCnt=0, zeroIndex = 0, zeroCnt = 0;
int followIndex = 0, followCnt = 0;

for(int i=0 ; i < a.length ; i++) {
if(a[i] != 0) {
precCnt++;
}
if(a[i]==0) {
zeroIndex = i;
break;
}
}

for(int i=zeroIndex ; i < a.length ; i++) {
if(a[i] == 0) {
zeroCnt++;
}
if(a[i] != 0) {
followIndex = i;
break;
}
}

for(int i=followIndex ; i < a.length ; i++) {
if(a[i] != 0) {
followCnt++;
}
if(a[i] == 0) {
break;
}
}

if (precCnt == followCnt && zeroCnt > 2) {
return 1;            }

return 0;
}
}

2. Define a flipflop array to be one whose elements alternate between even and odd values or vice versa. A flipflop array must have at least two elements. For example {0, 3, 4, -7} and {1, 2, 3, 4} are flipflop arrays but {2, 2, 3, 4}, {1, 2, 3, 3, 4} and {2} are not.
Write a function called isFlipFlop that returns 1 if its argument is a flipflop array, otherwise it returns 0.

Code To Find FlipFlop Array:

public class FlipFlop {
public static void main(String args[]) {
int a[] = {0, 3, 4, -7};
System.out.println(isFlipFlop(a));
}

static int isFlipFlop(int a[]) {

if(a.length > 1) {

int evenIndexStart = -1, oddIndexStart = -1;

if(a%2 == 0) {
evenIndexStart = 0;
oddIndexStart = 1;
} else {
oddIndexStart = 0;
evenIndexStart = 1;
}

for(int i = 1; i < a.length; i++) {
if(a[i] % 2 == 0){
if(evenIndexStart == 0) {
if(i % 2 == 1) {
return 0;
}
} else {
if(i % 2 == 0) {
return 0;
}
}
} else {
if(oddIndexStart == 0) {
if(i % 2 == 1) {
return 0;
}
} else {
if(i % 2 == 0) {
return 0;
}
}
}
}
return 1;
}
return 0;
}
}

3. A prime number is an integer that is divisible only by 1 and itself. A rhino number is a prime number whose last digit is 9 and the next prime number that follows it also ends with the digit 9. For example 139 is a rhino number because:
1. it is a prime
2. it ends in 9
3. The next prime number after it is 149 which also ends in 9. Note that 140, 141, 142, 143, 144, 145, 146, 147 and 148 are not prime so 149 is the next prime number after 139.
Write a method named isRhinoNumber which takes an integer argument n and returns 1 if it is a rhino number otherwise it return 0. So isRhinoNumber(139) would return 1.
The function signature is
int isRhinoNumber(int n)

Some examples of numbers that are not rhino numbers are
 Number Reason why it is not a rhino number 6 It is not a prime 17 Although it is prime, its last digit is not 9 19 It is prime and the last digit is 9 but the next prime number is 23 which does not end with 9 199 The next prime number, 211, does not end with 9

Code To Find Rhino Number:

public class RhinoNumber {

public static void main(String args[]) {
System.out.println(isRhinoNumber(139));
}

static int isRhinoNumber(int n) {
if(n % 10 == 9) {
if(isPrime(n) == 1) {
for(int nextNum=n+1;;nextNum++){
if(isPrime(nextNum) == 1) {
if(nextNum % 10 == 9) {
return 1;
}
return 0;
}
}
}
}
return 0;
}

static int isPrime(int num) {
int count;
for (count=2; count < num ;count++ ){
int n = num%count;
if (n==0){
break;
}
}
if(count == num){
return 1;
}
return 0;
}