0% found this document useful (1 vote)
456 views

Updated Compro Question Collections

There are three questions on the exam testing different skills: 1. Define and count "square pairs" from an array by returning the number that can be constructed where the sum of each pair is a perfect square. 2. Write a method to find the first "porcupine number" greater than a given input, where a porcupine number is a prime ending in 9 followed by the next prime also ending in 9. 3. Write a function to check if an array represents a "Guthrie sequence" generated from a starting number using the rules of multiplying/dividing until reaching 1.

Uploaded by

anuabc
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
456 views

Updated Compro Question Collections

There are three questions on the exam testing different skills: 1. Define and count "square pairs" from an array by returning the number that can be constructed where the sum of each pair is a perfect square. 2. Write a method to find the first "porcupine number" greater than a given input, where a porcupine number is a prime ending in 9 followed by the next prime also ending in 9. 3. Write a function to check if an array represents a "Guthrie sequence" generated from a starting number using the rules of multiplying/dividing until reaching 1.

Uploaded by

anuabc
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 91

There are three questions on the exam. You have two hours to finish.

Please do your own


work.
1. Write a function named primeCount with signature
int primeCount(int start, int end);

The function returns the number of primes between start and end inclusive. Recall that a prime is
a positive integer greater than 1 whose only integer factors are 1 and itself.

Examples
If start and end retur reason
is is n
10 30 6 The primes between 10 and 30 inclusive are 11, 13, 17,
19, 23 and 29
11 29 6 The primes between 11 and 29 inclusive are 11, 13, 17,
19, 23 and 29
20 22 0 20, 21, and 22 are all non-prime
1 1 0 By definition, 1 is not a prime number
5 5 1 5 is a prime number
6 2 0 start must be less than or equal to end
-10 6 3 primes are greater than 1 and 2, 3, 5 are prime

2. A Madhav array has the following property.


a[0] = a[1] + a[2] = a[3] + a[4] + a[5] = a[6] + a[7] + a[8] + a[9] = ...
The length of a Madhav array must be n*(n+1)/2 for some n.

Write a method named isMadhavArray that returns 1 if its array argument is a Madhav array,
otherwise it returns 0. If you are programming in Java or C# the function signature is
int isMadhavArray(int[ ] a)

If you are programming in C or C++, the function signature is


int isMadhavArray(int a[ ], int len) where len is the number of elements in a.

Examples
if a is retur reason
n
{2, 1, 1} 1 2+1+1
{2, 1, 1, 4, -1, -1} 1 2 = 1 + 1, 2 = 4 + -1 + -1
{6, 2, 4, 2, 2, 2, 1, 5, 0, 0} 1 6 = 2 + 4, 6 = 2 + 2 + 2, 6 = 1 + 5 + 0 +
0
{18, 9, 10, 6, 6, 6} 0 18 != 9 + 10
{-6, -3, -3, 8, -5, -4} 0 -6 != 8 + -5 + -4
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1 0 = 0 + 0, 0 = 0 + 0 + 0, 0 = 0 + 0 + 0 +
1, -2, -1} 0,
0 = 1 + 1 + 1 + -2 + -1
{3, 1, 2, 3, 0} 0 The length of the array is 5, but 5 does not
equal n*(n+1)/2 for any value of n.
3. An array is defined to be inertial if the following conditions hold:
a. it contains at least one odd value
b. the maximum value in the array is even
c. every odd value is greater than every even value that is not the maximum value.

So {11, 4, 20, 9, 2, 8} is inertial because


a. it contains at least one odd value
b. the maximum value in the array is 20 which is even
c. the two odd values (11 and 9) are greater than all the
even values that are not equal to 20 (the maximum), i.e., (4, 2, 8}.

However, {12, 11, 4, 9, 2, 3, 10} is not inertial because it fails condition (c), i.e., 10 (which is
even) is greater 9 (which is odd) but 10 is not the maximum value in the array.

Write a function called isIntertial that accepts an integer array and returns 1 if the array is
inertial; otherwise it returns 0.

If you are programming in Java or C#, the function signature is


int isInertial(int[ ] a)

If you are programming in C or C++, the function signature is


int isInertial(int a[ ], int len) where len is the number of elements in the array

Some other examples:


if the input array is return reason
fails condition (a) - the maximum value must be
{1} 0
even
fails condition (b) - the array must contain at
{2} 0
least one odd value.
fails condition (c) - 1 (which is odd) is not
greater than all even values other than the
{1, 2, 3, 4} 0
maximum (1 is less than 2 which is not the
maximum)
there is no even number other than the
{1, 1, 1, 1, 1, 1, 2} 1 maximum. Hence, there can be no other even
values that are greater than 1.
11, the only odd value is greater than all even
{2, 12, 4, 6, 8, 11} 1 values except 12 which is the maximum value
in the array.
same as previous, i.e., it is OK if maximum
{2, 12, 12, 4, 6, 8, 11} 1
value occurs more than once.
-8, which is even, is not the maximum value but
{-2, -4, -6, -8, -11} 0
is greater than -11 which is odd
{2, 3, 5, 7} 0 the maximum value is odd
{2, 4, 6, 8, 10} 0 there is no odd value in the array.

There are three questions on this exam. You have two hours to complete it. Please do your own work.

1.Define a square pair to be the tuple <x, y> where x and y are positive, non-zero integers, x<y and x + y is a perfect square. A perfect
square is an integer whose square root is also an integer, e.g. 4, 9, 16 are perfect squares but 3, 10 and 17 are not. Write a function
named countSquarePairs that takes an array and returns the number of square pairs that can be constructed from the elements in the
array. For example, if the array is {11, 5, 4, 20} the function would return 3 because the only square pairs that can be constructed from
those numbers are <5, 11>,
<5, 20> and <4, 5>. You may assume that there exists a function named isPerfectSquare that returns 1 if its argument is a
perfect square and 0 otherwise. E.G., isPerfectSquare(4) returns 1 and isPerfectSquare(8) returns 0.

If you are programming in Java or C#, the function signature is


int countSquarePairs(int[ ] a)

If you are programming in C++ or C, the function signature is


int countSquarePairs(int a[ ], int len) where len is the number of elements in the array.

You may assume that there are no duplicate values in the array, i.e, you don't have to deal with an array like {2, 7, 2, 2}.

Examples:
if a is return reason
{9, 0, 2, -5, 7} 2 The square pairs are <2, 7> and <7, 9>. Note that <-5, 9> and <0, 9>
are not square pairs, even though they sum to perfect squares,
because both members of a square pair have to be greater than 0.
Also <7,2> and <9,7> are not square pairs because the first number
has to be less than the second number.
{9} 0 The array must have at least 2 elements

2. A prime number is an integer that is divisible only by 1 and itself. A porcupine 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 porcupine number because:
a. it is prime
b. it ends in a 9
c. 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 findPorcupineNumber which takes an integer argument n and returns the first porcupine number that is
greater than n. So findPorcupineNumber(0) would return 139 (because 139 happens to be the first porcupine number) and so would
findPorcupineNumber(138). But findPorcupineNumber(139) would return 409 which is the second porcupine number.

The function signature is


int findPorcupineNumber(int n)

You may assume that a porcupine number greater than n exists.

You may assume that a function isPrime exists that returns 1 if its argument is prime, otherwise it returns 0. E.G., isPrime(7)
returns 1 and isPrime(8) returns 0.
Hint: Use modulo base 10 arithmetic to get last digit of a number.
3. Consider the following algorithm
Start with a positive number n
if n is even then divide by 2
if n is odd then multiply by 3 and add 1
continue this until n becomes 1

The Guthrie sequence of a positive number n is defined to be the numbers generated by the above algorithm.

For example, the Guthrie sequence of the number 7 is


7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1

It is easy to see that this sequence was generated from the number 7 by the above algorithm. Since 7 is odd multiply by 3 and add 1 to
get 22 which is the second number of the sequence. Since 22 is even, divide by 2 to get 11 which is the third number of the sequence.
11 is odd so multiply by 3 and add 1 to get 34 which is the fourth number of the sequence and so on.

Note: the first number of a Guthrie sequence is always the number that generated the sequence and the last number is always 1.

Write a function named isGuthrieSequence which returns 1 if the elements of the array form a Guthrie sequence. Otherwise, it returns
0.

If you are programming in Java or C#, the function signature is


int isGuthrieSequence(int[ ] a)

If you are programming in C++ or C, the function signature is


int isGuthrieSequence(int a[ ], int len) when len is the number of elements in the array.

Examples

if a is return reason
{8, 4, 2, 1} 1 This is the Guthrie sequence for 8
{8, 17, 4, 1} 0 This is not the Guthrie sequence for 8
{8, 4, 1} 0 Missing the 2
{8, 4, 2} 0 A Guthrie sequence must end with 1

There are three questions on this exam. You have two hours to complete it. Please do your own work.

1. The Stanton measure of an array is computed as follows. Count the number of 1s in the array. Let this count be n. The
Stanton measure is the number of times that n appears in the array. For example, the Stanton measure of {1, 4, 3, 2, 1, 2, 3,
2} is 3 because 1 occurs 2 times in the array and 2 occurs 3 times.

Write a function named stantonMeasure that returns the Stanton measure of its array argument.

If you are programming in Java or C#, the function prototype is


int stantonMeasure(int[ ] a)

If you are programming in C++ or C, the function prototype is


int stantonMeasure(int a[ ], int len) where len is the number of elements in the array.

Examples

if a is return reason
{1} 1 1 occurs 1 time, 1 occurs 1 time
{0} 1 1 occurs 0 times, 0 occurs 1 time
{3, 1, 1, 4} 0 1 occurs 2 times, 2 occurs 0 times
{1, 3, 1, 1, 3, 3, 2, 3, 3, 3, 4} 6 1 occurs 3 times, 3 occurs 6 times
{} 0 1 occurs 0 times, 0 occurs 0 times

2. The sum factor of an array is defined to be the number of times that the sum of the array appears as an element of the
array. So the sum factor of {1, -1, 1, -1, 1, -1, 1} is 4 because the sum of the elements of the array is 1 and 1 appears four
times in the array. And the sum factor of
{1, 2, 3, 4} is 0 because the sum of the elements of the array is 10 and 10 does not occur as an element of the array. The
sum factor of the empty array { } is defined to be 0.

Write a function named sumFactor that returns the sum factor of its array argument.

If you are programming in Java or C#, the function signature is


int sumFactor(int[ ] a)

If you are programming in C++ or C, the function signature is


int sumFactor(int a[ ], int len) where len is the number of elements in the array.

Examples:
if a is return reason
{3, 0, 2, -5, 0} 2 The sum of array is 0 and 0 occurs 2 times
{9, -3, -3, -1, -1} 0 The sum of the array is 1 and 1 does not occur in array.
{1} 1 The sum of the array is 1 and 1 occurs once in the array
{0, 0, 0} 3 The sum of the array is 0 and 0 occurs 3 times in the array
3. Consider the following algorithm
Start with a positive number n
if n is even then divide by 2
if n is odd then multiply by 3 and add 1
continue this until n becomes 1

The Guthrie index of a positive number n is defined to be how many iterations of the above algorithm it takes before n
becomes 1.

For example, the Guthrie index of the number 7 is 16 because the following sequence is 16 numbers long.
22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1

It is easy to see that this sequence was generated by the above algorithm. Since 7 is odd multiply by 3 and add 1 to get 22
which is the first number of the sequence. Since 22 is even, divide by 2 to get 11 which is the second number of the
sequence. 11 is odd so multiply by 3 and add 1 to get 34 which is the third number of the sequence and so on.

Write a function named guthrieIndex which computes the Guthrie index of its argument. Its signature is

int guthrieIndex(int n)

Examples

if n is return sequence
1 0 number is already 1
2 1 1
3 7 10, 5, 16, 8, 4, 2, 1
4 2 2, 1
42 8 21, 64, 32, 16, 8, 4, 2, 1

You may assume that the length of the sequence can be represented by a 32 bit signed integer.

There are 3 questions on this test. You have two hours to do it. Please do your own work.

1. It is a fact that there exist two numbers x and y such that x! + y! = 10!. Write a method
named solve10 that returns the values x and y in an array.

The notation n! is called n factorial and is equal to n * n-1 * n-2 * ... 2 * 1, e.g., 5! = 5*4*3*2*1
= 120.

If you are programming in Java or C#, the function prototype is


int[ ] solve10() where the length of the returned array is 2.

If you are programming in C++ or C, the function prototype is


int * solve10() where the length of the returned array is 2.

Please be sure that the method solve10 returns an array, a, with two elements
where a[0] == x, a[1] == y and x! + y! = 10!.

2. An array can hold the digits of a number. For example the digits of the number 32053 are
stored in the array {3, 2, 0, 5, 3}. Write a method call repsEqual that takes an array and an
integer and returns 1 if the array contains only the digits of the number in the same order that
they appear in the number. Otherwise it returns 0.

If you are programming in Java or C#, the function prototype is


int repsEqual(int[ ] a, int n)

If you are programming in C++ or C, the function prototype is


int repsEqual(int a[ ], int len, int n) where len is the number of elements in the array.

Examples (note: your program must work for all values of a and n, not just those given here!)
if a is and n is return reason
{3, 2, 0, 5, 3} 32053 1 the array contains only the digits
of the number, in the same order
as they are in the number.
{3, 2, 0, 5} 32053 0 the last digit of the number is
missing from the array.
{3, 2, 0, 5, 3, 4} 32053 0 an extra number (4) is in the
array.
{2, 3, 0, 5, 3} 32053 0 the array elements are not in the
same order as the digits of the
number
{9, 3, 1, 1, 2} 32053 0 elements in array are not equal
to digits of number.
{0, 3, 2, 0, 5, 3} 32053 1 you can ignore leading zeroes.
3. An array is called centered-15 if some consecutive sequence of elements of the array sum to
15 and this sequence is preceded and followed by the same number of elements. For example
{3, 2, 10, 4, 1, 6, 9} is centered-15 because the sequence 10, 4, 1 sums to 15 and the
sequence is preceded by two elements (3, 2) and followed by two elements(6,9).

Write a method called isCentered15 that returns 1 if its array argument is centered-15, otherwise
it returns 0.

If you are programming in Java or C#, the function prototype is


int isCentered15(int[ ] a)

If you are programming in C++ or C, the function prototype is


int isCentered5(int a[ ], int len) where len is the number of elements in the array.

Examples
if a is retur reason
n
{3, 2, 10, 4, 1, 6, 9} 1 the sequence 10, 4, 1 sums to 15 and is
preceded by 2 elements and followed by 2
elements. Note that there is another sequence
that sums to 15 (6,9}. It is okay for the array to
have more than one sequence that sums to 15
as long as at least one of them is centered.
{2, 10, 4, 1, 6, 9} 0 (10, 4, 1) is preceded by one element but
followed by two. (9,6) is preceded by five
elements but followed by none. Hence neither
qualify as centered.
{3, 2, 10, 4, 1, 6} (10, 4, 1) is preceded by two elements but
followed by one. Note that the values 3, 2, 4, 6
sum to 15 but they are not consecutive.
{1,1,8, 3, 1, 1} The entire array sums to 15, hence the
sequence is preceded by zero elements and
followed by zero elements.
{9, 15, 6} 1 the sequence (15) is preceded by one element
and followed by one element.
{1, 1, 2, 2, 1, 1} 0 no sequence sums to 15.
{1, 1, 15 -1,-1} 1 there are three different sequences that sum to
15, the entire array, (1, 15, -1) and (15). In this
case they all are centered but the requirement
is that just one of them has to be.

There are three questions on this test. You have two hours to finish it. Please do your own work.

1. A perfect number is one that is the sum of its factors, excluding itself. The 1st perfect number is 6 because 6 = 1 + 2 +
3. The 2nd perfect number is 28 which equals 1 + 2 + 4 + 7 + 14. The third is 496 = 1 + 2 + 4 + 8 + 16 + 31 + 62 + 124 +
248. In each case, the number is the sum of all its factors excluding itself.

Write a method named henry that takes two integer arguments, i and j and returns the sum of the ith and jth perfect
numbers. So for example, henry (1, 3) should return 502 because 6 is the 1st perfect number and 496 is the 3rd perfect
number and 6 + 496 = 502.
The function signature is
int henry (int i, int j)

You do not have to worry about integer overflow, i.e., you may assume that each sum that you have to compute can be
represented as a 31 bit integer. Hint: use modulo arithmetic to determine if one number is a factor of another.

2. Write a method named isDivisible that takes an integer array and a divisor and returns 1 if all its elements are divided
by the divisor with no remainder. Otherwise it returns 0.

If you are programming in Java or C#, the function signature is


int isDivisible(int [ ] a, int divisor)

If you are programming in C or C++, the function signature is


int isDivisible(int a[ ], int len, int divisor) where len is the number of elements in the array.

Examples

if a is and divisor is return because


{3, 3, 6, 36} 3 1 all elements of a are divisible by 3 with no
remainder.
{4} 2 1 all elements of a are even
{3, 4, 3, 6, 36} 3 0 because when a[1] is divided by 3, it leaves a
remainder of 1
{6, 12, 24, 36} 12 0 because when a[0] is divided by 12, it leaves a
remainder of 6.
{} anything 1 because no element fails the division test.
3. An array is defined to be n-unique if exactly one pair of its elements sum to n. For example, the array {2, 7, 3, 4} is 5-
unique because only a[0] and a[2] sum to 5. But the array {2, 3, 3, 7} is not 5-unique because a[0] + a[1] = 5 and a[0] +
a[2] = 5.

Write a function named isNUnique that returns 1 if its integer array argument is n-unique, otherwise it returns 0. So
isNUnique(new int[ ]{2, 7, 3, 4}, 5) should return 1 and
isNUnique(new int[] {2, 3, 3, 7}, 5) should return 0.

If you are programming in Java or C#, the function signature is


int isNUnique(int[ ] a, int n)

If you are programming in C or C++, the function signature is


int isNUnique(int a[ ], int len, int n) where len is the number of elements in the array.

Examples
If a is and n is return because
{7, 3, 3, 2, 4} 6 0 because a[1]+a[2] == 6 and a[3]+a[4] also == 6.
{7, 3, 3, 2, 4} 10 0 because a[0]+a[1] == 10 and a[0] + a[2] also == 10
{7, 3, 3, 2, 4} 11 1 because only a[0] + a[4] sums to 11
{7, 3, 3, 2, 4} 8 0 because no pair of elements sum to 8. (Note that
a[1]+a[2]+a[3] do sum to 8 but the requirement is that two
elements sum to 8.)
{7, 3, 3, 2, 4} 4 0 no pair of elements sum to 4. (Note that the a[4]==4, but
the requirement is that two elements have to sum to 4.)
{1} anything 0 array must have at least 2 elements

There are three questions on this exam. You have two hours to complete it.

1. Write a function named isSquare that returns 1 if its integer argument is a square of some integer, otherwise it returns 0. Your
function must not use any function or method (e.g. sqrt) that comes with a runtime library or class library!

The signature of the function is


int isSquare(int n)

Examples:
retu
if n is reason
rn
4 1 because 4 = 2*2
25 1 because 25 = 5*5
because there is no integer that when squared equals -4. (note, -2 squared is 4 not
-4 0
-4)
8 0 because the square root of 8 is not an integer.
0 1 because 0 = 0*0

2. A number with a base other than 10 can be written using its base as a subscript. For example, 10112 represents the binary number
1011 which can be converted to a base 10 number as follows:

(1 * 20) + (1 * 21) + (0 * 22) + (1 * 23) = 1 + 2 + 0 + 8 = 1110


Similarily, the base 3 number 1123 can be converted to base 10 as follows:

(2 * 30) + (1 * 31) + (1 * 32) = 2 + 3 + 9 = 1410

And the base 8 number 3258 can be converted to base 10 as follows:

(5 * 80) + (2 * 81) + (3 * 82) = 5 + 16 + 192 = 21310

Write a method named isLegalNumber that takes two arguments. The first argument is an array whose elements are the digits of the
number to test. The second argument is the base of the number represented by the first argument. The method returns 1 if the number
represented by the array is a legal number in the given base, otherwise it returns 0.

For example the number 3214 can be passed to the method as follows:
isLegalNumber(new int[] {3, 2, 1}, 4)
This call will return 1 because 3214 is a legal base 4 number.

However, since all digits of a base n number must be less than n, the following call will return 0 because 3716 is not a legal base 6
number (the digit 7 is not allowed)
isLegalNumber(new int[] {3, 7, 1}, 6)

If you are programming in Java or C#, the signature of the method is


int isLegalNumber(int[ ] a, int base)
If you are programming in C or C++, the signature of the method is
int isLegalNumber(int a[ ], int len, int base) where len is the size of the array.
3. Using the <array, base> representation for a number described in the second question write a method named convertToBase10 that
converts its <array, base> arguments to a base 10 number if the input is legal for the specified base. If it is not, it returns -1.

Some examples:

convertToBase10(new int[ ] {1, 0, 1, 1}, 2) returns 11


convertToBase10(new int[ ] {1, 1, 2}, 3) returns 14
convertToBase10(new int[ ] {3, 2, 5}, 8) returns 213
convertToBase10 (new int[ ] {3, 7, 1}, 6) returns 0 because 371 is not a legal base 6 number.

Your convertToBase10 method must call the isLegalNumber method that you wrote for question 2.

If you are programming in Java or C#, the function signature is:


int convertToBase10(int[ ] a, int base)

If you are programming in C or C++, the function signature is:


int convertToBase10(int a[ ], int len, int base) where len is the size of the array.
There are 3 questions on this test. You have 2 hours to finish it. Please use tabs or spaces to indent your program.
1. A simple pattern match on the elements of an array A can be defined using another array P. Each element n of P is negative or
positive (never zero) and defines the number of elements in a sequence in A. The first sequence in A starts at A[0] and its length is
defined by P[0]. The second sequence follows the first sequence and its length is defined by P[1] and so on. Furthermore, for n in P, if
n is positive then the sequence of n elements of A must all be positive. Otherwise the sequence of abs(n) elements must all be
negative. The sum of the absolute values of the elements of P must be the length of A. For example, consider the array
A = {1, 2, 3, -5, -5, 2, 3, 18}
If P = {3, -2, 3} then A matches P because
i. the first 3 elements of A (1, 2, 3) are positive (P[0] is 3 and is positive),
ii. the next 2 elements of A (-5, -5) are negative (P[1] is -2 and is negative)
iii. and the last 3 elements of A (2, 3, 18) are positive (P[2] is 3 and is positive)
Notice that the absolute values of the elements of P sum to 8 which is the length of A. The array A also matches the following
patterns:
{2, 1, -1, -1, 2, 1}, {1, 2, -1, -1, 1, 2}, {2, 1, -2, 3}, {1, 1, 1, -1, -1, 1, 1, 1}
In each case the sum of the absolute values is 8, which is the length of A and each sequence of numbers in A defined in a pattern is
negative or positive as required.
The array A = {1, 2, 3, -5, -5, 2, 3, 18} does not match the following patterns:
i. P = {4, -1, 3} (because the first 4 elements of A are not positive (A[3] is negative) as required by
P)
ii. P = {2, -3, 3} (because even though the first 2 elements of A are positive, the next 3 are
required to be negative but A[2] is positive which does not satisfy this requirement.)
iii. P = {8} (because this requires all elements of A to be positive and they are not.)

Please note: Zero is neither positive nor negative.

Write a function named matches which takes arrays A and P as arguments and returns 1 if A matches P. Otherwise it returns 0. You
may assume that P is a legal pattern, i.e., the absolute value of its elements sum to the length of A and it contains no zeros. So
do not write code to check if P is legal!
If you are programming in Java or C# the signature of the function is
int matches(int[ ] a, int[ ] p)
If you are programming in C++ or C, the signature of the function is
int matches(int a[ ], int len, int p[ ]) where len is the number of elements of a. Furthermore, the value of p[0] should be the
length of p. So, for example, if p={5, 2, -1, -2, 4}, p[0]=5 means that the array has 5 elements and that the last 4 define the pattern.
Hint: Your function should have one loop nested in another. The outer loop iterates through the elements of P. The inner loop iterates
through the next sequence of A. The upper bound of the inner loop is the absolute value of the current element of P. The lower bound
of the inner loop is 0. The loop variable of the inner loop is not used to index A!
2. Define a stacked number to be a number that is the sum of the first n positive integers for some n. The first 5 stacked numbers are
1=1
3=1+2
6=1+2+3
10 = 1 + 2 + 3+ 4
15 = 1 + 2 + 3 + 4 + 5

Note that from the above we can deduce that 7, 8, and 9 are not stacked numbers because they cannot be the sum of any sequence of
positive integers that start at 1.
Write a function named isStacked that returns 1 if its argument is stacked. Otherwise it returns 0. Its signature is:
int isStacked(int n);
So for example, isStacked(10) should return 1 and isStacked(7) should return 0.

3. Define an array to be sum-safe if none of its elements is equal to the sum of its elements. The array
a = {5, -5, 0} is not sum-safe because the sum of its elements is 0 and a[2] == 0. However, the array a =
{5, -2, 1} is sum-safe because the sum of its elements is 4 and none of its elements equal 4.

Write a function named isSumSafe that returns 1 if its argument is sum-safe, otherwise it returns 0.
If you are writing in Java or C#, the function signature is
int isSumSafe(int[ ]a)
If you are writing in C++ or C, the function signature is
int isSumSafe(int a[ ], int len) where len is the number of elements in a.
For example, isSumSafe(new int[ ] {5, -5, 0}) should return 0 and isSumSafe(new int[ ]{5, -2, 1}) should return 1.
Return 0 if the array is empty.

There are three questions on this exam. You have two hours to finish. Please do your own work.

1. Define a positive number to be isolated if none of the digits in its square are in its cube. For example 163 is n isolated number
because 69*69 = 26569 and 69*69*69 = 4330747 and the square does not contain any of the digits 0, 3, 4 and 7 which are the digits
used in the cube. On the other hand 162 is not an isolated number because 162*162=26244 and 162*162*162 = 4251528 and the digits
2 and 4 which appear in the square are also in the cube.

Write a function named isIsolated that returns 1 if its argument is an isolated number, it returns 0 if its not an isolated number and it
returns -1 if it cannot determine whether it is isolated or not (see the note below). The function signature is:

int isIsolated(long n)

Note that the type of the input parameter is long. The maximum positive number that can be represented as a long is 63 bits long. This
allows us to test numbers up to 2,097,151 because the cube of 2,097,151 can be represented as a long. However, the cube of 2,097,152
requires more than 63 bits to represent it and hence cannot be computed without extra effort. Therefore, your function should test if n is
larger than 2,097,151 and return -1 if it is. If n is less than 1 your function should also return -1.

Hint: n % 10 is the rightmost digit of n, n = n/10 shifts the digits of n one place to the right.

The first 10 isolated numbers are


N n*n n*n*n
2 4 8
3 9 27
8 64 512
9 81 729
14 196 2744
24 576 13824
28 784 21952
34 1156 39304
58 3364 195112
63 3969 250047

Questions 2 and 3 are on the next page.


2. An array is called vanilla if all its elements are made up of the same digit. For example {1, 1, 11, 1111, 1111111} is a vanilla array
because all its elements use only the digit 1. However, the array {11, 101, 1111, 11111} is not a vanilla array because its elements use the
digits 0 and 1. Write a method called isVanilla that returns 1 if its argument is a vanilla array. Otherwise it returns 0.

If you are writing in Java or C#, the function signature is


int isVanilla(int[ ] a)

If you are writing in C or C++, the function signature is


int isVanilla(int a[ ], int len) where len is the number of elements in the array a.

Example
if a is Return reason
{1} 1 all elements use only digit 1.
{11, 22, 13, 34, 125} 0 Elements used 5 different digits
{9, 999, 99999, -9999} 1 Only digit 9 is used by all elements.
Note that negative numbers are
okay.
{} 1 There is no counterexample to the
hypothesis that all elements use the
same digit.

3. Define an array to be trivalent if all its elements are one of three different values. For example, {22, 19, 10, 10, 19, 22, 22, 10} is
trivalent because all elements are either 10, 22, or 19. However, the array {1, 2, 2, 2, 2, 2, 2} is not trivalent because it contains only two
different values (1, 2). The array {2, 2, 3, 3, 3, 3, 2, 41, 65} is not trivalent because it contains four different values (2, 3, 41, 65).

Write a function named isTrivalent that returns 1 if its array argument is trivalent, otherwise it returns 0.

If you are writing in Java or C#, the function signature is


int isTrivalent (int[ ] a)

If you are writing in C or C++, the function signature is


int isTrivalent(int a[ ], int len) where len is the number of elements in the array a.

Hint: Remember that the elements of the array can be any value, so be careful how you initialize your local variables! For
example using -1 to initialize a variable won’t work because -1 might be one of the values in the array.

Examples

if a is return Reason
{-1, 0, 1, 0, 0, 0} 1 All elements have one of three values (0, -1, 1)
{} 0 There are no elements
{ 2147483647, -1, -1 1 Again only three different values. Note that the value of a[0]
-2147483648} is the maximum integer and the value of a[3] is the minimum
integer so you can’t use those to initialize local variables.

There are 3 questions on this exam. You have 2 hours to complete it. Please do your own work and use indentation.

1. Write a function named countRepresentations that returns the number of ways that an amount of money in rupees can be
represented as rupee notes. For this problem we only use rupee notes in denominations of 1, 2, 5, 10 and 20 rupee notes.

The signature of the function is:


int countRepresentations(int numRupees)

For example, countRepresentations(12) should return 15 because 12 rupees can be represented in the following 15 ways.
1. 12 one rupee notes
2. 1 two rupee note plus 10 one rupee notes
3. 2 two rupee notes plus 8 one rupee notes
4. 3 two rupee notes plus 6 one rupee notes
5. 4 two rupee notes plus 4 one rupee notes
6. 5 two rupee notes plus 2 one rupee notes
7. 6 two rupee notes
8. 1 five rupee note plus 7 one rupee notes
9. 1 five rupee note, 1 two rupee note and 5 one rupee notes
10. 1 five rupee note, 2 two rupee notes and 3 one rupee notes
11. 1 five rupee note, 3 two notes and 1 one rupee note
12. 2 five rupee notes and 2 one rupee notes
13. 2 five rupee notes and 1 two rupee note
14. 1 ten rupee note and 2 one rupee notes
15. 1 ten rupee note and 1 two rupee note

Hint: Use a nested loop that looks like this. Please fill in the blanks intelligently, i.e. minimize the number of times that the if
statement is executed.
for (int rupee20=0; rupee20<=__; rupee20++)
for (int rupee10=0; rupee10<=__; rupee10++)
for (int rupee5=0; rupee5<=__; rupee5++)
for (int rupee2=0; rupee2<=__; rupee2++)
for (int rupee1=0; rupee1<=__; rupee1++)
{
if (___)
count++
}

2. An integer array is defined to be sequentially-bounded if it is in ascending order and each value, n, in the array occurs less than n
times in the array. So {2, 3, 3, 99, 99, 99, 99, 99} is sequentially-bounded because it is in ascending order and the value 2 occurs less
than 2 times, the value 3 occurs less than 3 times and the value 99 occurs less than 99 times. On the other hand, the array {1, 2, 3} is
not sequentially-bounded because the value 1 does not occur < 1 times. The array {2, 3, 3, 3, 3} is not sequentially-bounded because
the maximum allowable occurrences of 3 is 2 but 3 occurs 4 times. The array {12, 12, 9} is not sequentially-bounded because it is not
in ascending order.

Write a function named isSequentiallyBounded that returns 1 if its array argument is sequentially-bounded, otherwise it returns 0.
• If you are programming in Java or C#, the function signature is int isSequentiallyBounded(int[ ] a)
• If you are programming in C or C++, the function signature is int isSequentiallyBounded(int a[ ], int len) where len is the length
of the array.

Examples
if a is return Reason
{0, 1} 0 the value 0 has to occur less than 0 times, but it doesn’t
{-1, 2} 0 if array contains a negative number, return 0.
{} 1 since there are no values, there are none that can fail the test.
{5, 5, 5, 5} 1 5 occurs less than 5 times
{5, 5, 5, 2, 5} 0 array is not in ascending order.

3. An array is defined to be minmax-disjoint if the following conditions hold:


a. The minimum and maximum values of the array are not equal.
b. The minimum and maximum values of the array are not adjacent to one another.
c. The minimum value occurs exactly once in the array.
d. The maximum value occurs exactly once in the array.

For example the array {5, 4, 1, 3, 2} is minmax-disjoint because


a. The maximum value is 5 and the minimum value is 1 and they are not equal.
b. 5 and 1 are not adjacent to one another
c. 5 occurs exactly once in the array
d. 2 occurs exactly once in the array

Write a function named isMinMaxDisjoint that returns 1 if its array argument is minmax-disjoint, otherwise it returns 0.
If you are programming in Java or C#, the function signature is
int isMinMaxDisjoint(int[ ] a)

If you are programming in C or C#, the function signature is


int isMinMaxDisjoint(int a[ ], int len) where len is the number of elements in the array.

Examples of arrays that are not minMaxDisjoint

if a is return Reason
{18, -1, 3, 4, 0} 0 The max and min values are
adjacent to one another.
{9, 0, 5, 9} 0 The max value occurs twice in the
array.
{0, 5, 18, 0, 9| 0 The min value occurs twice in the
array.
{7, 7, 7, 7} 0 The min and the max value must
be different.
{} 0 There is no min or max.
{1, 2} 0 The min and max elements are
next to one another.
{1} 0 The min and the max are the
same.

There are 3 questions on this exam. You have 2 hours to complete it. Please do your own work.

1. The number 124 has the property that it is the smallest number whose first three multiples contain the digit 2. Observe that
124*1 = 124, 124*2 = 248, 124*3 = 372 and that 124, 248 and 372 each contain the digit 2. It is possible to generalize this property to be the
smallest number whose first n multiples each contain the digit 2. Write a function named smallest(n) that returns the smallest number whose firs
n multiples contain the digit 2. Hint: use modulo base 10 arithmetic to examine digits.

Its signature is
int smallest(int n)

You may assume that such a number is computable on a 32 bit machine, i.e, you do not have to detect integer overflow in your answer.

Examples
If n is return because
because the first four multiples of 624 are 624, 1248, 1872, 2496 and they all contain the
4 624
digit 2. Furthermore 624 is the smallest number whose first four multiples contain the digit 2.
because the first five multiples of 624 are 624, 1248, 1872, 2496, 3120. Note that 624 is also
5 624
the smallest number whose first 4 multiples contain the digit 2.
6 642 because the first five multiples of 642 are 642, 1284, 1926, 2568, 3210, 3852
because the first five multiples of 4062 are 4062, 8124, 12186, 16248, 20310, 24372, 28434.
7 4062
Note that it is okay for one of the multiples to contain the digit 2 more than once (e.g., 24372).

2. Define a cluster in an integer array to be a maximum sequence of elements that are all the same value. For example, in the array {3, 3, 3, 4, 4,
3, 2, 2, 2, 2, 4} there are 5 clusters, {3, 3, 3}, {4, 4}, {3}, {2, 2, 2, 2} and {4}. A cluster-compression of an array replaces each cluster with the
number that is repeated in the cluster. So, the cluster compression of the previous array would be {3, 4, 3, 2, 4}. The first cluster {3, 3, 3} is
replaced by a single 3, and so on.

Write a function named clusterCompression with the following signature

If you are programming in Java or C#, the function signature is


int[ ] clusterCompression(int[ ] a)

If you are programming in C++ or C, the function signature is


int *clusterCompression(int a[ ], int len) where len is the length of the array.
The function returns the cluster compression of the array a. The length of the returned array must be equal to the number of clusters in the origin
array! This means that someplace in your answer you must dynamically allocate the returned array.

In Java or C# you can use


int[ ] result = new int[numClusters];

In C or C++ you can use


int *result = (int *)calloc(numClusters, sizeof(int));

Examples
a is then function returns
{0, 0, 0, 2, 0, 2, 0, 2, 0, 0} {0, 2, 0, 2, 0, 2, 0}
{18} {18}
{} {}
{-5, -5, -5, -5, -5} {-5}
{1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1} {1, 2, 1}
{8, 8, 6, 6, -2, -2, -2} {8, 6, -2}
3. Define an array to be a railroad-tie array if the following three conditions hold

a. The array contains at least one non-zero element


b. Every non-zero element has exactly one non-zero neighbor
c. Every zero element has two non-zero neighbors.

For example, {1, 2, 0, 3, -18, 0, 2, 2} is a railroad-tie array because

a[0] = 1 has exactly one non-zero neighbor (a[1])


a[1] = 2 has exactly one non-zero neighbor (a[0])
a[2] = 0 has two non-zero neighbors (a[1] and a[3])
a[3] = 3 has exactly one non-zero neighbor (a[4])
a[4] = -18 has exactly one non-zero neighbor (a[3])
a[5] = 0 has two non-zero neighbors (a[4] and a[6])
a[6] = 2 has exactly one non-zero neighbor (a[7])
a[7] = 2 has exactly one non-zero neighbor (a[6])

The following are not railroad-tie arrays

{1, 2, 3, 0, 2, 2}, because a[1]=2 has two non-zero neighbors.


{0, 1, 2, 0, 3, 4}, because a[0]=0 has only one non-zero neighbor (it has no left neighbor)
{1, 2, 0, 0, 3, 4}, because a[2]=0 has only one non-zero neighbor (a[1])
{1}, because a[0]=1 does not have any non-zero neighbors.
{}, because the array must have at least one non-zero element
{0}, because the array must have at lease one non-zero element.

Write a function named isRailroadTie which returns 1 if its array argument is a railroad-tie array; otherwise it returns 0.

If you are writing in Java or C#, the function signature is


int isRailroadTie(int[ ] a)

If you are writing in C or C++, the function signature is


int isRailroadTie(int a[ ], int len) where len is the number of elements in the array a

More examples:
if a is return
{1, 2} 1
{1, 2, 0, 1, 2, 0, 1, 2} 1
{3, 3, 0, 3, 3, 0, 3, 3, 0, 3, 3} 1
{0, 0, 0, 0} 0 (must have non-zero element)
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} 0 (a[1] has two non-zero neighbors)
{1, 3, 0, 3, 5, 0} 0 (a[5] has no right neighbor)

base representation of 94 because


This exam has three questions. You have two hours to complete it. Please
format your answers so that blocks are indented. This makes it easier for the
2 1011110 26 + 24 + 23 + 22 + 21 = 94
grader to read your answers. And do your own work!
3 10111 34 + 32 + 31 + 30 = 94 1. Define the fullness quotient of an integer n > 0 to be the number of
representations of n in bases 2 through 9 that have no zeroes anywhere after
4 1132 43 + 42 + 3*41 + 2*40 = 94 the most significant digit. For example, to see why the fullness quotient of
94 is 6 examine the following table which shows the representations of 94
5 334 3*52 + 3*51 + 4*40 = 94 in bases 2 through 9.

6 234 2*62 + 3*61 + 4*60 = 94

7 163 1*72 + 6*71 + 3*70 = 94 Notice that the representations of 94 in base 2 and 3 both have 0s
2 1 0
8 136 1*8 + 3*8 + 6*8 = 94

9 114 1*92 + 1*91 + 4*90 = 94


somewhere after the most significant digit, but the representations in bases 4, 5, 6, 7, 8, 9 do not. Since there are 6 such
representations, the fullness quotient of 94 is 6.
Write a method named fullnessQuotient that returns the fullness quotient of its argument. If the argument is less than 1 return -1. Its
signature is
int fullnessQuotient(int n)
Hint: use modulo and integer arithmetic to convert n to its various representations
Examples:

if n is return Because

Because all of its representations do not have a 0 anywhere after the most significant digit:
1 8
2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9

Because 5 of the representations (4, 5, 6, 7, 8) do not have a 0


9 5 anywhere after the most significant digit:
2: 1001, 3: 100, 4: 21, 5: 14, 6: 13, 7: 12, 8: 11, 9: 10

All its representations have a 0 somewhere after the most


significant digit:
360 0
2: 101101000, 3: 111100, 4: 11220, 5: 2420, 6: 1400,
7: 1023,8: 550, 9: 440

-4 -1 The argument must be > 0

2. Define an array to be packed if all its values are positive, each value n appears n
times and all equal values are in consecutive locations. So for example, {2, 2, 3, 3, 3}
is packed because 2 appears twice and 3 appears three times. But {2, 3, 2, 3, 3} is not
packed because the 2s are not in consecutive locations. And {2, 2, 2, 3, 3, 3} is not
packed because 2 appears three times.
Write a method named isPacked that returns 1 if its array argument is packed, otherwise it
returns 0. You may assume that the array is not null
If you are programming in Java or C#, the function signature is
int isPacked(int[ ] a)
If you are programming in C++ or C, the function signature is
int isPacked(int a[ ], int len) where len is the length of the array.
Examples

then
a is function reason
returns

because there are two 2s and one 1 and equal values


{2, 2, 1} 1
appear in consecutive locations.

{2, 2, 1, Because there are four 2s (doesn't matter that they


0
2, 2} are in groups of 2)

{4, 4, 4, because 4 occurs four times, 3 appears three times,


4, 1, 2, 2, 1 2 appears two times and 1 appears once and equal
3, 3, 3} values are in consecutive locations.

{7, 7, 7, 1 because 7 occurs seven times and 1 occurs once.


7, 7, 7, 7,
1}

{7, 7, 7,
7, 1, 7, 7, 0 because the 7s are not in consecutive locations.
7}

{7, 7, 7,
1 because 7 occurs seven times
7, 7, 7, 7}

because there is no value that appears the wrong


{} 1
number of times

{1, 2, 1} 0 because there are too many 1s

{2, 1, 1} 0 because there are too many 1s

{-3, -3,
0 because not all values are positive
-3}

{0, 2, 2} 0 because 0 occurs one time, not zero times.

because the 2s are not in consec


{2, 1, 2} 0
utive locations

Hint: Make sure that your solution handles all the above examples correctly!
3. An array is defined to be odd-heavy if it contains at least one odd element and every
element whose value is odd is greater than every even-valued element. So {11, 4, 9, 2, 8}
is odd-heavy because the two odd elements (11 and 9) are greater than all the even
elements. And {11, 4, 9, 2, 3, 10} is not odd-heavy because the even element 10 is greater
than the odd element 9.
Write a function called isOddHeavy that accepts an integer array and returns 1 if the
array is odd-heavy; otherwise it returns 0.
If you are programming in Java or C#, the function signature is int isOddHeavy(int[ ] a)
If you are programming in C or C++, the function signature is int isOddHeavy(int a[ ], int
len) where len is the number of elements in the array
Some other examples:

if the input
isOddHeavy should return
array is

{1} 1 (true vacuously)

{2} 0 (contains no odd elements)

{1, 1, 1, 1,
1
1, 1}

{2, 4, 6, 8, 1 (11, the only odd-valued element is


11} greater than all even-valued elements.

{-2, -4, -6, 0 (-8, an even-valued element is greater


-8, -11} than -11 an odd-valued element.)
This exam is two hours long and contains three questions. Please indent your code so it is easy for the grader to read it.

1. Write a method named getExponent(n, p) that returns the largest exponent x such that px evenly divides n. If p is <= 1 the method
should return -1.

For example, getExponent(162, 3) returns 4 because 162 = 21 * 34, therefore the value of x here is 4.

The method signature is


int getExponent(int n, int p)

Examples:

and p retu
if n is Because
is rn
27 3 3 33 divides 27 evenly but 34 does not.
28 3 0 30 divides 28 evenly but 31 does not.
71 divides 280 evenly but 72 does
280 7 1
not.
53 divides -250 evenly but 54 does
-250 5 3
not.
18 1 -1 if p <=1 the function returns -1.
43 divides 128 evenly but 44 does
128 4 3
not.

2. Define an array to be a 121 array if all its elements are either 1 or 2 and it begins with one or more 1s followed by a one or more 2s
and then ends with the same number of 1s that it begins with. Write a method named is121Array that returns 1 if its array argument is
a 121 array, otherwise, it returns 0.

If you are programming in Java or C#, the function signature is


int is121Array(int[ ] a)

If you are programming in C or C++, the function signature is


int is121Array(int a[ ], int len) where len is the number of elements in the array a.

Examples
then function
a is reason
returns
because the same number of 1s are at the beginning and
{1, 2, 1} 1 end of the array and there is at least one 2 in between
them.
because the same number of 1s are at the beginning and
{1, 1, 2, 2, 2, 1, 1} 1 end of the array and there is at least one 2 in between
them.
Because the number of 1s at the end does not equal the
{1, 1, 2, 2, 2, 1, 1, 1} 0
number of 1s at the beginning.
{1, 1, 2, 1, 2, 1, 1} 0 Because the middle does not contain only 2s.
{1, 1, 1, 2, 2, 2, 1, 1, 1, 3} 0 Because the array contains a number other than 1 and 2.
{1, 1, 1, 1, 1, 1} 0 Because the array does not contain any 2s
{2, 2, 2, 1, 1, 1, 2, 2, 2, 1, 1} 0 Because the first element of the array is not a 1.
{1, 1, 1, 2, 2, 2, 1, 1, 2, 2} 0 Because the last element of the array is not a 1.
{2, 2, 2} 0 Because there are no 1s in the array.
3. A binary representation of a number can be used to select elements from an array. For example,

n: 88 = 23 + 24 + 26 (1011000)
array: 8, 4, 9, 0, 3, 1, 2
indexes 0 1 2 3 4 5 6
selected * * *
result 0, 3, 2

so the result of filtering {8, 4, 9, 0, 3, 1, 2} using 88 would be {0, 3, 2}

In the above, the elements that are selected are those whose indices are used as exponents in the binary representation of 88. In other
words, a[3], a[4], and a[6] are selected for the result because 3, 4 and 6 are the powers of 2 that sum to 88.

Write a method named filterArray that takes an array and a non-negative integer and returns the result of filtering the array using the
binary representation of the integer. The returned array must big enough to contain the filtered elements and no bigger. So in the above
example, the returned array has length of 3, not 7 (which is the size of the original array.) Futhermore, if the input array is not big
enough to contain all the selected elements, then the method returns null. For example, if n=3 is used to filter the array a = {18},
the method should return null because 3=20+21 and hence requires that the array have at least 2 elements a[0] and a[1], but there is no
a[1].

If you are using Java or C#, the signature of the function is


int[ ] filterArray(int[ ] a, int n)

If you are using C or C++, the signature of the function is


int * filterArray(int a[ ], int len, int n) where len is the length of the array a

Hint: Proceed as follows

a. Compute the size of the returned array by counting the number of 1s in the binary representation of n (You can use modulo 2
arithmetic to determine the 1s in the binary represention of n)

b. Allocate an array of the required size

c. Fill the allocated array with elements selected from the input array

Examples
and n
if a is return because
is
because there are no 1s in the binary
{9, -9} 0 {}
representation of 0
{9, -9} 1 {9} because 1 = 20 and a[0] is 9
{9, -9} 2 {-9} because 2 = 21 and a[1] is -9
{9, -9} 3 {9, -9} because 3 = 20 + 21 and a[0]=9, a[1]=-9
{9, -9} 4 null because 4 = 22 and there is no a[2]
{9, -9, 5} 3 {9, -9} because 3 = 20 + 21 and a[0]=9, a[1]=-9
because 11 = 20 + 21 + 23 and a[0]=0,
{0, 9, 12, 18, -6} 11 {0, 9, 18}
a[1]=9, a[3]=18

There are three questions on this exam. You have 2 hours to complete it. Please indent your program so that it is easy for the grader to
read.

1. Write a function named largestAdjacentSum that iterates through an array computing the sum of adjacent elements and returning
the largest such sum. You may assume that the array has at least 2 elements.

If you are writing in Java or C#, the function signature is


int largestAdjacentSum(int[ ] a)
If you are writing in C or C++, the function signature is
int largestAdjacentSum(int a[ ], int len) where len is the number of elements in a

Examples:
if a is return
{1, 2, 3, 4} 7 because 3+4 is larger than either 1+2 or 2+3
{18, -12, 9, -10} 6 because 18-12 is larger than -12+9 or 9-10
{1,1,1,1,1,1,1,1,1} 2 because all adjacent pairs sum to 2
{1,1,1,1,1,2,1,1,1} 3 because 1+2 or 2+1 is the max sum of adjacent pairs

2. The number 198 has the property that 198 = 11 + 99 + 88, i.e., if each of its digits is concatenated twice and then summed, the result
will be the original number. It turns out that 198 is the only number with this property. However, the property can be generalized so
that each digit is concatenated n times and then summed. For example, 2997 = 222+999+999+777 and here each digit is concatenated
three times. Write a function named checkContenatedSum that tests if a number has this generalized property.
The signature of the function is
int checkConcatenatedSum(int n, int catlen) where n is the number and catlen is the number of times to concatenate each digit
before summing.

The function returns 1 if n is equal to the sum of each of its digits contenated catlen times. Otherwise, it returns 0. You may assume
that n and catlen are greater than zero

Hint: Use integer and modulo 10 arithmetic to sequence through the digits of the argument.

Examples:
if n is and catlen is return reason
198 2 1 because 198 == 11 + 99 + 88
198 3 0 because 198 != 111 + 999 + 888
2997 3 1 because 2997 == 222 + 999 + 999 + 777
2997 2 0 because 2997 != 22 + 99 + 99 + 77
13332 4 1 because 13332 = 1111 + 3333 + 3333 + 3333 + 2222
9 1 1 because 9 == 9

3. Define an m-n sequenced array to be an array that contains one or more occurrences of all the integers between m and n inclusive.
Furthermore, the array must be in ascending order and contain only those integers. For example, {2, 2, 3, 4, 4, 4, 5} is a 2-5 sequenced
array. The array {2, 2, 3, 5, 5, 5} is not a 2-5 sequenced array because it is missing a 4. The array {0, 2, 2, 3, 3} is not a 2-3 sequenced
array because the 0 is out of range. And {1,1, 3, 2, 2, 4} is not a 1-4 sequenced array because it is not in ascending order.

Write a method named isSequencedArray that returns 1 if its argument is a m-n sequenced array, otherwise it returns 0.

If you are writing in Java or C# the function signature is


int isSequencedArray(int[ ] a, int m, int n)

If you are writing in C or C++ the function signature is


int isSequencedArray(int a[ ], int len, int m, int n) where len is the number of elements in the array a.

You may assume that m<=n

Examples
and m and n
if a is return reason
is is
because the array contains all the numbers between 1 and 5 inclusive in
{1, 2, 3, 4, 5} 1 5 1
ascending order and no other numbers.
{1, 3, 4, 2, 5} 1 5 0 because the array is not in ascending order.
{-5, -5, -4, -4, -4, -3, -3, -2, -2, -2} -5 -2 1 because the array contains all the numbers between -5 and -2 inclusive in
ascending order and no other numbers. Note that duplicates are allowed.
{0, 1, 2, 3, 4, 5} 1 5 0 because 0 is not in between 1 and 5 inclusive
{1, 2, 3, 4} 1 5 0 because there is no 5
{1, 2, 5} 1 5 0 because there is no 3 or 4
because the array does not start with a 1. Furthermore, it is not in
{5, 4, 3, 2, 1} 1 5 0
ascending order.

There are three questions on this exam. You have 2 hours to complete it. Please indent your programs so that it is easy for the
grader to read.

1. Write a function named largestPrimeFactor that will return the largest prime factor of a number. If the number is <=1 it should
return 0. Recall that a prime number is a number > 1 that is divisible only by 1 and itself, e.g., 13 is prime but 14 is not.

The signature of the function is int largestPrimeFactor(int n)

Examples:

if n is return because
10 5 because the prime factors of 10 are 2 and 5 and 5 is the largest one.
6936 17 because the distinct prime factors of 6936 are 2, 3 and 17 and 17 is the largest
1 0 because n must be greater than 1

2. The fundamental theorem of arithmetic states that every natural number greater than 1 can be written as a unique product of prime
numbers. So, for instance, 6936=2*2*2*3*17*17. Write a method named encodeNumber what will encode a number n as an array that
contains the prime numbers that, when multipled together, will equal n. So encodeNumber(6936) would return the array {2, 2, 2, 3,
17, 17}. If the number is <= 1 the function should return null;

If you are programming in Java or C#, the function signature is


int[ ] encodeNumber(int n)

If you are programming in C or C++, the function signature is


int *encodeNumber(int n) and the last element of the returned array is 0.

Note that if you are programming in Java or C#, the returned array should be big enough to contain the prime factors and no bigger.
If you are programming in C or C++ you will need one additional element to contain the terminating zero.

Hint: proceed as follows:

1. Compute the total number of prime factors including duplicates.


2. Allocate an array to hold the prime factors. Do not hard-code the size of the returned array!!
3. Populate the allocated array with the prime factors. The elements of the array when multiplied together should equal the number.

Examples
if n is return reason
2 {2} because 2 is prime
6 {2, 3} because 6 = 2*3 and 2 and 3 are prime.
14 {2, 7} because 14=2*7 and 2 and 7 are prime numbers.
24 {2, 2, 2, 3} because 24 = 2*2*2*3 and 2 and 3 are prime
1200 {2, 2, 2, 2, 3, 5, 5} because 1200=2*2*2*2*3*5*5 and those are all prime
1 null because n must be greater than 1
-18 null because n must be greater than 1

3. Consider a simple pattern matching language that matches arrays of integers. A pattern is an array of integers. An array matches a
pattern if it contains sequences of the pattern elements in the same order as they appear in the pattern. So for example, the array {1, 1,
1, 2, 2, 1, 1, 3} matches the pattern {1, 2, 1, 3} as follows:

{1, 1, 1, 2, 2, 1, 1, 3} {1, 2, 1, 3} (first 1 of pattern matches three 1s in array)


{1, 1, 1, 2, 2, 1, 1, 3} {1, 2, 1, 3} (next element of pattern matches two 2s in array)
{1, 1, 1, 2, 2, 1, 1, 3} {1, 2, 1, 3} (next element of pattern matches two 1s in array)
{1, 1, 1, 2, 2, 1, 1, 3} {1, 2, 1, 3} (last element of pattern matches one 3 in array)

The pattern must be completely matched, i.e. the last element of the array must be matched by the last element of the pattern.

Here is an incomplete function that does this pattern matching. It returns 1 if the pattern matches the array, otherwise it returns 0.

static int matchPattern(int[] a, int len, int[] pattern, int patternLen) {


// len is the number of elements in the array a, patternLen is the number of elements in the pattern.
int i=0; // index into a
int k=0; // index into pattern
int matches = 0; // how many times current pattern character has been matched so far
for (i=0; i<len; i++) {
if (a[i] == pattern[k])
matches++; // current pattern character was matched
else if (matches == 0 || k == patternLen-1)
return 0; // if pattern[k] was never matched (matches==0) or at end of pattern (k==patternLen-1)
else // advance to next pattern character {
!!You write this code!!
} // end of else
} // end of for

// return 1 if at end of array a (i==len) and also at end of pattern (k==patternLen-1)


if (i==len && k==patternLen-1) return 1; else return 0;
}

Please finish this function by writing the code for the last else statement. Your answer just has to include this code, you do not have to
write the entire function.

Hint: You need at least 4 statements (one of them an if statement)

Examples
if a is and pattern is return reason
{1, 1, 1, 1, 1} {1} 1 because all elements of the array match the pattern element 1
{1} {1} 1 because all elements of the array match the pattern element 1
because the first two 1s of the array are matched by the first pattern
{1, 1, 2, 2, 2, 2} {1, 2} 1
element, last four 2s of array are matched by the last pattern element
{1, 2, 3} {1, 2} 0 because the 3 in the array is not in the pattern.
{1, 2} {1, 2, 3} 0 because the 3 in the pattern is not in the array
{1, 1, 2, 2, 2, 2, 3} {1, 3} 0 because at least one 3 must appear after the sequence of 1s.
{1, 1, 1, 1} {1, 2} 0 because the array ends without matching the pattern element 2.
{1, 1, 1, 1, 2, 2, 3, 3} {1, 2} 0 because the element 3 of the array is not matched
because the 10 element is not matched by the 4 pattern element. Be
{1, 1, 10, 4, 4, 3} {1, 4, 3} 0
sure your code handles this situation correctly!
There are three questions on this exam. You have 2 hours to complete it. Please indent your program so that it is easy for the grader to
read.

1. Define the n-based integer rounding of an integer k to be the nearest multiple of n to k. If two multiples of n are equidistant use
the greater one. For example

the 4-based rounding of 5 is 4 because 5 is closer to 4 than it is to 8,


the 5-based rounding of 5 is 5 because 5 is closer to 5 that it is to 10,
the 4-based rounding of 6 is 8 because 6 is equidistant from 4 and 8, so the greater one is used,
the 13-based rounding of 9 is 13, because 9 is closer to 13 than it is to 0,

Write a function named doIntegerBasedRounding that takes an integer array and rounds all its positive elements using n-based
integer rounding.

A negative element of the array is not modified and if n <=0, no elements of the array are modified. Finally you may assume that the
array has at least two elements.

Hint: In integer arithmetic, (6/4) * 4 = 4

If you are programming in Java or C#, the function signature is


void doIntegerBasedRounding(int[ ] a, int n) where n is used to do the rounding

If you are programming in C or C++, the function signature is


void doIntegerBasedRounding(int a[ ], int n, int len) where n is used to do the rounding and len is the number of elements in the array
a.

Examples
if a is and n is then a becomes reason
because the 2-based rounding of 1 is 2, the 2-based
rounding of 2 is 2, the 2-based rounding of 3 is 4, the
{1, 2, 3, 4, 5} 2 {2, 2, 4, 4, 6}
2-based rounding of 4 is 4, and the 2-based rounding
of 5 is 6.
because the 3-based rounding of 1 is 0, the 3-based
{1, 2, 3, 4, 5} 3 {0, 3, 3, 3, 6} roundings of 2, 3, 4 are all 3, and the 3-based
rounding of 5 is 6.
{1, 2, 3, 4, 5} -3 {1, 2, 3, 4, 5} because the array is not changed if n <= 0.
{-1, -2, -3, -4, -5} 3 {-1, -2, -3, -4, -5} because negative numbers are not rounded
because -18 is negative and hence is not modified,
{-18, 1, 2, 3, 4, 5} 4 {-18, 0, 4, 4, 4, 4} the 4-based rounding of 1 is 0, and the 4-based
roundings of 2, 3, 4, 5 are all 4.
{1, 2, 3, 4, 5} 5 {0, 0, 5, 5, 5}
{1, 2, 3, 4, 5} 100 {0, 0, 0, 0, 0}
2. A number n>0 is called cube-powerful if it is equal to the sum of the cubes of its digits.

Write a function named isCubePowerful that returns 1 if its argument is cube-powerful; otherwise it returns 0.

The function prototype is


int isCubePowerful(int n);

Hint: use modulo 10 arithmetic to get the digits of the number.

Examples:
retu
if n is because
rn
153 1 because 153 = 13 + 53 + 33
370 1 because 370 = 33 + 73 + 03
371 1 because 371 = 33 + 73 + 13
407 1 because 407 = 43 + 03 + 73
87 0 because 87 != 83 + 73
because n must be greater than
0 0
0.
because n must be greater than
-81 0
0.
3. A number can be encoded as an integer array as follows. The first element of the array is any number and if it is negative then the
encoded number is negative. Each digit of the number is the absolute value of the difference of two adjacent elements of the array. The
most significant digit of the number is the absolute value of the difference of the first two elements of the array. For example, the array
{2, -3, -2, 6, 9, 18} encodes the number 51839 because

 5 is abs(2 - (-3))
 1 is abs(-3 - (-2))
 8 is abs(-2 - 6)
 3 is abs(6-9)
 9 is abs(9-18)

The number is positive because the first element of the array is >= 0.

If you are programming in Java or C#, the function prototype is


int decodeArray(int[ ] a)

If you are programming in C or C++, the function prototype is


int decodeArray(int a[ ], int len) where len is the length of array a;

You may assume that the encoded array is correct, i.e., the absolute value of the difference of any two adjacent elements is between 0
and 9 inclusive and the array has at least two elements.

Examples

then
a is function reason
returns
{0, -3, 0, -4, 0} 3344 because abs(0-(-3)=3, abs(-3-0)=3, abs(0-(-4))=4, abs(-4-0)=4
because abs(-1-5)=6, abs(5-8)=3, abs(8-17)=9, abs(17-15)=2;
{-1, 5, 8, 17, 15} -6392 the number is negative because the first element of the array
is negative
because abs(1-5)=4, remaining digits are the same as previous
{1, 5, 8, 17, 15} 4392 example; the number is positive because the first element of
the array is >=0.
because abs(111-115)=4, abs(115-118)=3, abs(118-127)=9,
{111, 115, 118, 127, 125} 4392 abs(127-125)=2; the number is positive because the first
element of the array is >=0.
{1, 1} 0 because abs(1-1) = 0

There are three questions on this exam. You have 2 hours to complete it.

1. An array is zero-plentiful if it contains at least one 0 and every sequence of 0s is of length at least 4.

Write a method named isZeroPlentiful which returns the number of zero sequences if its array argument is zero-plentiful, otherwise it
returns 0.

If you are programming in Java or C#, the function signature is


int isZeroPlentiful(int[ ] a)
If you are programming in C or C++, the function signature is
int isZeroPlentiful(int a[ ], int len) where len is the number of elements in the array a.

Examples

then function
a is reason
returns
{0, 0, 0, 0, 0}1 1 because there is one sequence of 0s and its length >= 4.
because there are two sequences of 0s and both have lengths
{1, 2, 0, 0, 0, 0, 2, -18, 0, 0, 0, 0, 0, 12}1 2
>= 4.
because three are three sequences of zeros and all have length
{0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0}1 3
>=4
{1, 2, 3, 4}1 0 because there must be at least one 0.
{1, 0, 0, 0, 2, 0, 0, 0, 0} 0 because there is a sequence of zeros whose length is less < 4.
{0} 0 because there is a sequence of zeroes whose length is < 4.
{} 0 because there must be at least one 0.

2. A number is called digit-increasing if it is equal to n + nn + nnn + ... for some digit n between 1 and 9. For example 24 is digit-
increasing because it equals 2 + 22 (here n = 2)

Write a function called isDigitIncreasing that returns 1 if its argument is digit-increasing otherwise, it returns 0.

The signature of the method is


int isDigitIncreasing(int n)

Examples

then function
if n is reason
returns
7 1 because 7 = 7 (here n is 7)
36 1 because 36 = 3 + 33
984 1 because 984 = 8 + 88 + 888
because 7404 = 6 + 66 + 666 +
7404 1
6666
3. An integer number can be encoded as an array as follows. Each digit n of the number is represented by n zeros followed by a 1. So
the digit 5 is represented by 0, 0, 0, 0, 0, 1. The encodings of each digit of a number are combined to form the encoding of the number.
So the number 1234 is encoded as the array {0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1}. The first 0, 1 is contributed by the digit 1, the next 0,
0, 1 is contributed by the digit 2, and so on.

There is one other encoding rule: if the number is negative, the first element of the encoded array must be -1, so -201 is encoded as {-
1, 0, 0, 1, 1, 0, 1}. Note that the 0 digit is represented by no zeros, i.e. there are two consecutive ones!

Write a method named decodeArray that takes an encoded array and decodes it to return the number.

You may assume that the input array is a legal encoded array, i.e., that -1 will only appear as the first element, all elements are either 0,
1 or -1 and that the last element is 1.

If you are programming in Java or C#, the function prototype is


int decodeArray(int[ ] a)

If you are programming in C or C++, the function prototype is


int decodeArray(int a[ ], int len);

Examples
then
a is function reason
returns
because the digit 0 is represented by no zeros followed by
{1} 0
a one.
because the digit 1 is represented by one zero followed by
{0, 1} 1
a one.
because the encoding of a negative number begins with a
{-1, 0, 1} -1 -1 followed by the encoding of the absolute value of the
number.
because the encoding of the first 1 is 0, 1, the encoding of
{0, 1, 1, 1, 1, 1, 0, 1} 100001 each of the four 0s is just a 1 and the encoding of the last
1 is 0, 1.
{0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1} 999 because each 9 digit is encoded as 0,0,0,0,0,0,0,0,0,1.

This exam consists of three questions. You have two hours in which to complete it.

1. An onion array is an array that satisfies the following condition for all values of j and k:
if j>=0 and k>=0 and j+k=length of array and j!=k then a[j]+a[k] <= 10

Write a function named isOnionArray that returns 1 if its array argument is an onion array and returns 0 if it is not.

Your solution must not use a nested loop (i.e., a loop executed from inside another loop). Furthermore, once you determine that the
array is not an onion array your function must return 0; no wasted loops cycles please!

If you are programming in Java or C#, the function signature is


int isOnionArray(int[ ] a)

If you are programming in C or C++, the function signature is


int isOnionArray(int a[ ], int len) where len is the number of elements in the array a.

Examples
then function
a is reason
returns
{1, 2, 19, 4, 5} 1 because 1+5 <= 10, 2+4 <=10
{1, 2, 3, 4, 15} 0 because 1+15 > 10
{1, 3, 9, 8} 0 because 3+9 > 10
because there is no j, k where a[j]+a[k] > 10 and j+k=length of
{2} 1
array and j!=k
because there is no j, k where a[j]+a[k] > 10 and j+k=length of
{} 1
array and j!=k
{-2, 5, 0, 5, 12} 1 because -2+12 <= 10 and 5+5 <= 10

2. A number n is called prime happy if there is at least one prime less than n and the sum of all primes less than n is evenly divisible
by n.

Recall that a prime number is an integer > 1 which has only two integer factors, 1 and itself

The function prototype is int isPrimeHappy(int n);

Examples:
if n is return because
5 1 because 2 and 3 are the primes less than 5, their sum is 5 and 5 evenly divides 5.
25 1 because 2, 3, 5, 7, 11, 13, 17, 19, 23 are the primes less than 25, their sum is 100 and 25 evenly divides 100
32 1 because 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31 are the primes less than 32, their sum is 160 and 32 evenly divides 160
8 0 because 2, 3, 5, 7 are the primes less than 8, their sum is 17 and 8 does not evenly divide 17.
2 0 because there are no primes less than 2.
3. An integer number can be encoded as an array as follows. Each digit n of the number is represented by n zeros followed by a 1. So
the digit 5 is represented by 0, 0, 0, 0, 0, 1. The encodings of each digit of a number are combined to form the encoding of the number.
So the number 1234 is encoded as the array {0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1}. The first 0, 1 is contributed by the digit 1, the next 0,
0, 1 is contributed by the digit 2, and so on. There is one other encoding rule: if the number is negative, the first element of the
encoded array must be -1, so -201 is encoded as {-1, 0, 0, 1, 1, 0, 1}. Note that the 0 digit is represented by no zeros, i.e. there are two
consecutive ones!

Write a method named encodeArray that takes an integer as an argument and returns the encoded array.

If you are programming in Java or C#, the function prototype is


int[] encodeArray(int n)

If you are programming in C or C++, the function prototype is


int * encodeArray(int n);

Hints

Use modulo 10 arithmetic to get digits of number


Make one pass through the digits of the number to compute the size of the encoded array.
Make a second pass through the digits of the number to set elements of the encoded array to 1.

Examples
n is then function returns reason
because the digit 0 is represented by no
0 {1} zeros and the representation of each digit
ends in one.
because the digit 1 is represented by one
1 {0, 1} zero and the representation of each digit
ends in one.
because the encoding of a negative
number begins with a -1 followed by the
-1 {-1, 0, 1}
encoding of the absolute value of the
number.
because the encoding of the first 1 is 0, 1,
100001 {0, 1, 1, 1, 1, 1, 0, 1} the encoding of each of the four 0s is just
a 1 and the encoding of the last 1 is 0, 1.
because each 9 digit is encoded as
999 0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1
0,0,0,0,0,0,0,0,0,1.

1. An array is called systematically increasing if it consists of increasing sequences of the numbers from 1 to n.

The first six (there are over 65,000 of them) systematically increasing arrays are:

{1}
{1, 1, 2}
{1, 1, 2, 1, 2, 3}
{1, 1, 2, 1, 2, 3, 1, 2, 3, 4}
{1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5}
{1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 6}

Write a function named isSystematicallyIncreasing which returns 1 if its array argument is systematically increasing. Otherwise it
returns 0.

If you are programming in Java or C#, the function signature is


int isSystematicallyIncreasing(int[ ] a)
If you are programming in C or C++, the function signature is
int isSystematicallyIncreasing(int a[ ], int len) where len is the number of elements in the array a.

Examples
then function
a is reason
returns
{1} 1 because 1 is a sequence from 1 to 1 and is the only sequence.
{1, 2, 1, 2, 3} 0 because it is missing the sequence from 1 to 1.
{1, 1, 3} 0 because {1, 3} is not a sequence from 1 to n for any n.
{1, 2, 1, 2, 1, 2} 0 because it contains more than one sequence from 1 to 2.
because it is "backwards", i.e., the sequences from 1 to n are not ordered
{1, 2, 3, 1, 2, 1} 0
by increasing value of n
{1, 1, 2, 3} 0 because the sequence {1, 2} is missing (it should precede {1, 2, 3})

2. A positive, non-zero number n is a factorial prime if it is equal to factorial(n) + 1 for some n and it is prime. Recall that factorial(n)
is equal to 1 * 2 * ... * n-1 * n. If you understand recursion, the recursive definition is
factorial(1) = 1;
factorial(n) = n*factorial(n-1).
For example, factorial(5) = 1*2*3*4*5 = 120.

Recall that a prime number is a natural number which has exactly two distinct natural number divisors: 1 and itself.

Write a method named isFactorialPrime which returns 1 if its argument is a factorial prime number, otherwise it returns 0.

The signature of the method is


int isFactorialPrime(int n)

Examples
then function
if n is reason
returns
2 1 because 2 is prime and is equal to factorial(1) + 1
3 1 because 3 is prime and is equal to factorial(2) + 1
7 1 because 7 prime and is equal to factorial(3) + 1
8 0 because 8 is not prime
because 11 does not equal factorial(n) + 1 for any n (factorial(3)=6,
11 0
factorial(4)=24)
721 0 because 721 is not prime (its factors are 7 and 103)

3. Write a function named largestDifferenceOfEvens which returns the largest difference between even valued elements of its array
argument. For example largestDifferenceOfEvens(new int[ ]{-2, 3, 4, 9}) returns 6 = (4 - (-2)). If there are fewer than 2 even numbers
in the array, largestDifferenceOfEvens should return -1.

If you are programming in Java or C#, the function signature is


int largestDifferenceOfEvens(int[ ] a)

If you are programming in C or C++, the function signature is


int largestDifferenceOfEvens(int a[ ], int len) where len is the number of elements in the array a.

Examples
then function
a is reason
returns
{1, 3, 5, 9} -1 because there are no even numbers
because there is only one even number
{1, 18, 5, 7, 33} -1
(18)
{[2, 2, 2, 2]} 0 because 2-2 == 0
{1, 2, 1, 2, 1, 4, 1, 6, 4} 4 because 6 - 2 == 4

1. A hodder number is one that is prime and is equal to 2j-1 for some j. For example, 31 is a hodder number
because 31 is prime and is equal to 25-1 (in this case j = 5). The first 4 hodder numbers are 3, 7, 31, 127

Write a function with signature int isHodder(int n) that returns 1 if n is a hodder number, otherwise it returns 0.

Recall that a prime number is a whole number greater than 1 that has only two whole number factors, itself and
1.

2. One word is an anagram of another word if it is a rearrangement of all the letters of the second word. For
example, the character arrays {'s', 'i', 't'} and {'i', 't', 's'} represent words that are anagrams of one another
because "its" is a rearrangement of all the letters of "sit" and vice versa. Write a function that accepts two
character arrays and returns 1 if they are anagrams of one another, otherwise it returns 0. For simplicity, if the
two input character arrays are equal, you may consider them to be anagrams.

If you are programming in Java or C#, the function signature is:


int areAnagrams(char [ ] a1, char [ ] a2)

If you are programming in C or C++, the function signature is


int areAnagrams(a1 char[ ], a2 char[ ], int len) where len is the length of a1 and a2.

Hint: Please note that "pool" is not an anagram of "poll" even though they use the same letters. Please be sure
that your function returns 0 if given these two words! You can use another array to keep track of each letter that
is found so that you don't count the same letter twice (e.g., the attempt to find the second "o" of "pool" in "poll"
should fail.)

Hint: do not modify either a1 or a2, i.e., your function should have no side effects! If your algorithm requires
modification of either of these arrays, you must work with a copy of the array and modify the copy!

Examples

if input arrays are return


{'s', 'i', 't'} and {'i', 't', 's'} 1
{'s', 'i', 't'} and {'i', 'd', 's'} 0
{'b', 'i', 'g'} and {'b', 'i', 't'} 0
{'b', 'o', 'g'} and {'b', 'o', 'o'} 0
{} and {} 1
{'b', 'i', 'g'} and {'b', 'i', 'g'} 1

3. The Fibonacci sequence of numbers is 1, 1, 2, 3, 5, 8, 13, 21, 34, ... The first and second numbers are 1 and
after that ni = ni-2 + ni-1, e.g., 34 = 13 + 21. A number in the sequence is called a Fibonacci number. Write a
method with signature int closestFibonacci(int n) which returns the largest Fibonacci number that is less than
or equal to its argument. For example, closestFibonacci(13) returns 8 because 8 is the largest Fibonacci number
less than 13 and closestFibonacci(33) returns 21 because 21 is the largest Fibonacci number that is <= 33.
closestFibonacci(34) should return 34. If the argument is less than 1 return 0. Your solution must not use
recursion because unless you cache the Fibonacci numbers as you find them, the recursive solution recomputes
the same Fibonacci number many times.

1. A number n is vesuvian if it is the sum of two different pairs of squares. For example, 50 is vesuvian because 50 == 25
+ 25 and 1 + 49. The numbers 65 (1+64, 16+49) and 85 (4+81, 36+49) are also vesuvian. 789 of the first 10,000 integers
are vesuvian.

Write a function named isVesuvian that returns 1 if its parameter is a vesuvian number, otherwise it returns 0. Hint: be
sure to verify that your function detects that 50 is a vesuvian number!

2. Define an array to be one-balanced if begins with zero or more 1s followed by zero or more non-1s and concludes with
zero or more 1s. Write a function named isOneBalanced that returns 1 if its array argument is one-balanced, otherwise it
returns 0.

If you are programming in Java or C#, the function signature is


int isOneBalanced(int[ ] a)

If you are programming in C or C++, the function signature is


int isOneBalanced(int a[ ], int len) where len is the number of elements in the array a.

Examples

then
functio
if a is reason
n
returns
because it begins with three 1s, followed by four non-1s and
{1, 1, 1, 2, 3, -18, 45, 1} 1
ends with one 1 and 3+1 == 4
because the 0 starts another sequence of non-1s. There can be
{1, 1, 1, 2, 3, -18, 45, 1, 0} 0
only one sequence of non-1s.
because there are two sequences of non-1s
{1, 1, 2, 3, 1, -18, 26, 1} 0
({2, 3} and {-18, 26}
because 0 (# of beginning 1s) + 0 (# of ending 1s) =
{} 1
0 (# of non-1s)
because 0 (# of beginning 1s) + 2 (# of ending 1s) =
{3, 4, 1, 1} 1
2 (# of non-1s)
because 2 (# of beginning 1s) + 0 (# of ending 1s) =
{1, 1, 3, 4} 1
2 (# of non-1s)
because 0 (# of beginning 1s) + 0 (# of ending 1s) !=
{3, 3, 3, 3, 3, 3} 0
6 (# of non-1s)
because 6 (# of beginning 1s) + 0 (# of ending 1s) !=
{1, 1, 1, 1, 1, 1} 0
0 (# of non-1s)

3. The Fibonacci sequence of numbers is 1, 1, 2, 3, 5, 8, 13, 21, 34, ... The first and second numbers are 1 and after that n i
= ni-2 + ni-1, e.g., 34 = 13 + 21. Write a method with signature
int isFibonacci(int n) which returns 1 if its argument is a number in the Fibonacci sequence, otherwise it returns 0. For
example, isFibonacci(13) returns a 1 and isFibonacci(27) returns a 0. Your solution must not use recursion because unless
you cache the Fibonacci numbers as you find them, the recursive solution recomputes the same Fibonacci number many
times.

1. A number n is triangular if n == 1 + 2 +...+j for some j. Write a function


int isTriangular(int n)
that returns 1 if n is a triangular number, otherwise it returns 0. The first 4 triangular numbers are 1 (j=1), 3
(j=2), 6, (j=3), 10 (j=4).

2. Define an array to be a Mercurial array if a 3 does not occur between any two 1s. Write a function named
isMercurial that returns 1 if its array argument is a Mercurial array, otherwise it returns 0.

If you are programming in Java or C#, the function signature is


int isMercurial(int[ ] a)

If you are programming in C or C++, the function signature is


int isMercurial(int a[ ], int len) where len is the number of elements in the array a.

Hint: if you encounter a 3 that is preceded by a 1, then there can be no more 1s in the array after the 3.

Examples
then function
a is reason
returns
because 3 occurs after a 1 (a[0]) and
{1, 2, 10, 3, 15, 1, 2, 2} 0
before another 1 (a[5])
{5, 2, 10, 3, 15, 1, 2, 2} 1 because the 3 is not between two 1s.
{1, 2, 10, 3, 15, 16, 2, 2} 1 because the 3 is not between two 1s.
because a[5] is a 3 and is between a[3]
{3, 2, 18, 1, 0, 3, -11, 1, 3} 0
and a[7] which are both 1s.
because there are no instances of a 3
{2, 3, 1, 1, 18} 1
that is between two 1s
because there are no instances of a 3
{8, 2, 1, 1, 18, 3, 5} 1
that is between two 1s
because there are no instances of a 3
{3, 3, 3, 3, 3, 3} 1
that is between two 1s
because there are no instances of a 3
{1} 1
that is between two 1s
because there are no instances of a 3
{} 1
that is between two 1s

3. An array is defined to be a 235 array if the number of elements divisible by 2 plus the number of elements
divisible by 3 plus the number of elements divisible by 5 plus the number of elements not divisible by 2, 3, or 5
is equal to the number of elements of the array. Write a method named is123Array that returns 1 if its array
argument is a 235 array, otherwise it returns 0.

If you are writing in Java or C#, the function signature is


int is235Array(int[ ] a)
If you are writing in C or C++, the function signature is
int is235Array(int a[ ], int len) where len is the length of a

Hint: remember that a number can be divisible by more than one number

Examples

In the following: <a, b, c, d> means that the array has a elements that are divisible by 2, b elements that are
divisible by 3, c elements that are divisible by 5 and d elements that are not divisible by 2, 3, or 5.

if a is return reason
because one element is divisible by 2 (a[0]), one is
divisible by 3 (a[1]), one is divisible by 5 (a[2]) and two
{2, 3, 5, 7, 11} 1 are not divisible by 2, 3, or 5 (a[3] and a[4]). So we have
<1, 1, 1, 2> and 1+1+1+2 == the number of elements in
the array.
because two elements are divisible by 2 (a[0] and a[2]),
two are divisible by 3 (a[1] and a[2]), none are divisible
{2, 3, 6, 7, 11} 0 by 5 and two are not divisible by 2, 3, or 5 (a[3] and
a[4]). So we have <2, 2, 0, 2> and 2 + 2 + 0 + 2 == 6 !=
the number of elements in the array.
because <5, 3, 2, 1> and 5 + 3 + 2 + 1 == 11 != the
{2, 3, 4, 5, 6, 7, 8, 9, 10} 0
number of elements in the array.
because <5, 0, 0, 0> and 5 + 0 + 0 + 0 == 5 == the
{2, 4, 8, 16, 32} 1
number of elements in the array.
because <0, 3, 0, 6> and 0 + 3 + 0 + 6 == 9 == the
{3, 9, 27, 7, 1, 1, 1, 1, 1} 1
number of elements in the array.
because <0, 0, 0, 4> and 0 + 0 + 0 + 4 == 4 == the
{7, 11, 77, 49} 1
number of elements in the array.
because <1, 0, 0, 0> and 1 + 0 + 0 + 0 == 1 == the
{2} 1
number of elements in the array.
because <0, 0, 0, 0> and 0 + 0 + 0 + 0 == 0 == the
{} 1
number of elements in the array.
because <4, 1, 0, 6> and 4 + 1 + 0 + 6 == 11 == the
{7, 2, 7, 2, 7, 2, 7, 2, 3, 7, 7} 1
number of elements in the array.
1. Write a method named computeHMS that computes the number of hours, minutes and seconds in a given number of seconds.

If you are programming in Java or C#, the method signature is


int[] computeHMS(int seconds);

If you are programming in C or C++, the method signature is


int * computeHMS(int seconds);

The returned array has 3 elements; arr[0] is the hours, arr[1] is the minutes and arr[2] is the seconds contained within the seconds
argument.

Recall that there are 3600 seconds in an hour and 60 seconds in a minute. You may assume that the numbers of seconds is non-
negative.

Examples
If seconds then function
reason
is returns
because 3735 = 1*3600 + 2*60 +15. In other words, 3,735 is the number of seconds in 1 hour 2
3735 {1, 2, 15}
minutes and 15 seconds
380 {0, 6, 20} because 380 = 0*3600 + 6*60 + 20
3650 {1, 0, 50} because 3650 = 1*3600 + 0*60 + 50
55 {0, 0, 55} because 55 = 0*3600 + 0*60 + 55
0 {0, 0, 0} because 0 = 0*3600 + 0*60 + 0

2. Define an array to be a Martian array if the number of 1s is greater than the number of 2s and no two adjacent elements are equal.
Write a function named isMartian that returns 1 if its argument is a Martian array; otherwise it returns 0.

If you are programming in Java or C#, the function signature is


int isMartian(int[ ] a)

If you are programming in C or C++, the function signature is


int isMartian(int a[ ], int len) where len is the number of elements in the array a.

There are two additional requirements.

1. You should return 0 as soon as it is known that the array is not a Martian array; continuing to analyze the array would be a waste of
CPU cycles.

2. There should be exactly one loop in your solution.


Examples
then function
a is reason
returns
There is one 1 and zero 2s, hence the number of 1s is
{1, 3} 1 greater than the number of 2s. Also, no adjacent elements
have the same value (1 does not equal 3)
There are five 1s and four 2s, hence the number of 1s is
{1, 2, 1, 2, 1, 2, 1, 2,
1 greater than the number of 2s. Also, no two adjacent
1}
elements have the same value.
There is one 1 and one 2, hence the number of 1s is not
{1, 3, 2} 0
greater than the number of 2s.
{1, 3, 2, 2, 1, 5, 1, 5} 0 There are two 2s adjacent to each other.
The two -18s are adjacent to one another. Note that the
number of 1s is not greater than than the number of 2s but
{1, 2, -18, -18, 1, 2} 0
your method should return 0 before determining that! (See
the additional requirements above.)
There are zero 1s and zero 2s hence the number of 1s is
{} 0
not greater than the number of 2s.
There is one 1 and zero 2s hence the number of 1s is
greater than the number of 2s. Also since there is only one
{1} 1
element, there cannot be adjacent elements with the same
value.
There are zero 1s and one 2 hence the number of 1s is not
{2} 0
greater than the number of 2s.

Hint: Make sure that your solution does not exceed the boundaries of the array!

3. An array is defined to be paired-N if it contains two distinct elements that sum to N for some specified value of N and the indexes
of those elements also sum to N. Write a function named isPairedN that returns 1 if its array parameter is a paired-N array, otherwise
it returns 0. The value of N is passed as the second parameter.
If you are writing in Java or C#, the function signature is
int isPairedN(int[ ] a, int n)

If you are writing in C or C++, the function signature is


int isPairedN(int a[ ], int n, int len) where len is the length of a

There are two additional requirements.

1. Once you know the array is paired-N, you should return 1. No wasted loop iterations please.

2. Do not enter the loop unless you have to. You should test the length of the array and the value of n to determine whether the array
could possibly be a paired-N array. If the tests indicate no, return 0 before entering the loop.
Examples
if a is and n is return reason
because a[2] + a[3] == 5 and 2+3==5. In other
words, the sum of the values is equal to the sum of
{1, 4, 1, 4, 5, 6} 5 1
the corresponding indexes and both are equal to n
(5 in this case).
{1, 4, 1, 4, 5, 6} 6 1 because a[2] + a[4] == 6 and 2+4==6
{0, 1, 2, 3, 4, 5, 6, 7, 8} 6 1 because a[1]+a[5]==6 and 1+5==6
because although a[0] + a[1] == 5, 0+1 != 5; and
{1, 4, 1} 5 0
although a[1]+a[2]==5, 1+2 != 5
because there are several ways to get the values to
{8, 8, 8, 8, 7, 7, 7} 15 0 sum to 15 but there is no way to get the
corresponding indexes to sum to 15.
{8, -8, 8, 8, 7, 7, -7} -15 0 because although a[1]+a[6]==-15, 1+6!=-15
{3} 3 0 because the array has only one element
{} 0 0 because the array has no elements

This exam tests very basic programming skills and hence will be graded strictly. However, simple syntax errors will be forgiven. The
following examples gives you an idea of how the exam will be graded.

Sample problem: Write a method int allEven(int a[ ], int len) that returns 1 if all elements of the array a are even, otherwise it returns
0. Assume that the array has at least one element.

Solution 1:
int allEven (int a[ ], int len)
{
int result = 1;
for (int i=0; i<len && result==1; i++)
{
if (a[i] % 2 == 1)
result = 0; // exit loop, found a non-even element
}

return result;
}
Grading result: Correct; full marks. Will also accept breaking or returning from loop.

Solution 2:
static int allEven (int a[ ], int len)
{
int result = 1;
for (int i=0; i<len; i++)
{
if (a[i] % 2 == 1)
result = 0; // found non-even element
}

return result;
}
Grading result: Correct, but inefficient; marks will be deducted because program continues to loop even though it is known that the
result is 0.

Solution 3
static int allEven (int a[ ], int len)
{
int result = 1;
for (int i=0; i<len; i++)
{
if (a[i] % 2 == 1)
result = 0;
else
result = 1;
}

return result;
}
Grading result: Incorrect; no marks. Program returns status of the last element of the array.
1. Define an array to be n-primeable if for a given n, all elements of the array when incremented by n are prime. Recall that a prime
number is a number that has no factors except 1 and itself. Write a method named isNPrimeable that has an array parameter and an
integer parameter that defines n; the method returns 1 if its array parameter is n-primeable; otherwise it returns 0.

If you are programming in Java or C#, the function signature is


int isNPrimeable(int[ ] a, int n)

If you are programming in C or C++, the function signature is


int isNPrimeable(int a[ ], int len, int n) where len is the number of elements in the array a.

Examples

then function
If a is and n is reason
returns
{5, 15, 27} 2 1 5+2=7 is prime, and 15+2=17 is prime, and 27+2=29 is prime
{5, 15, 27} 3 0 5+3=8 is not prime
{5, 15, 26} 2 0 26+2=28 is not prime
{1, 1, 1, 1, 1, 1, 1} 4 1 1+4=5 is prime. This obviously holds for all elements in the array
Since there are no elements in the array, there cannot exist one that is
{} 2 1
non-prime when 2 is added to it.

2. Define an array to be a 121 array if all elements are either 1 or 2 and the array begins with one or more 1s followed by a one or
more 2s and then ends with the same number of 1s that it begins with. Write a method named is121Array that returns 1 if its array
argument is a 121 array, otherwise, it returns 0.

If you are programming in Java or C#, the function signature is


int is121Array(int[ ] a)

If you are programming in C or C++, the function signature is


int is121Array(int a[ ], int len) where len is the number of elements in the array a.

Examples

then function
a is reason
returns
because the same number of 1s are at the beginning and end of the
{1, 2, 1} 1
array and there is at least one 2 in between them.
because the same number of 1s are at the beginning and end of the
{1, 1, 2, 2, 2, 1, 1} 1
array and there is at least one 2 in between them.
Because the number of 1s at the end does not equal the number of 1s
{1, 1, 2, 2, 2, 1, 1, 1} 0
at the beginning.
Because the number of 1s at the end does not equal the number of 1s
{1, 1, 1, 2, 2, 2, 1, 1} 0
at the beginning.
{1, 1, 1, 2, 2, 2, 1, 1, 1, 3} 0 Because the array contains a number other than 1 and 2.
{1, 1, 1, 1, 1, 1} 0 Because the array does not contain any 2s
{2, 2, 2, 1, 1, 1, 2, 2, 2, 1, 1, 1} 0 Because the first element of the array is not a 1.
{1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2} 0 Because the last element of the array is not a 1.
{2, 2, 2} 0 Because there are no 1s in the array.
Hint: Walk through your solution with all the above test cases!

3. Write a method named pairwiseSum that has an array with an even number of elements as a parameter and returns an array that
contains the pairwise sums of the elements of its parameter array.

If you are writing in Java or C#, the function signature is


int[ ] pairwiseSum(int[ ] a)

If you are writing in C or C++, the function signature is


int * pairwiseSum(int a[ ], int len) where len is the length of a

The method returns null if

1. The array has no elements


2. The array has an odd number of elements

Otherwise, the method returns an array with arrayLength/2 elements. Each element of the returned array is the sum of successive pairs
of elements of the original array. See examples for more details.

Examples

if a is return reason
because 2+1=3, 18+-5=13. Note that there are exactly 2
{2, 1, 18, -5} {3, 13} elements in the returned array. You will lose full marks
for this question if you return {3, 13, 0, 0}!
because 2+1=3, 18+-5=13, -5+-15=-20, 0+0=0, 1+-1=0.
Note that there are exactly 5 elements in the returned
{2, 1, 18, -5, -5, -15, 0, 0, 1, -1} {3, 13, -20, 0, 0}
array. You will lose full marks for this question if you
return {3, 13, -20, 0, 0, 0, 0, 0, 0, 0}!
{2, 1, 18} null because there are an odd number of elements in the array.
{} null because there are no elements in the array

1. Write a function named isSquare that returns 1 if its integer argument is a square of some integer, otherwise it returns 0. Your
function must not use any function or method (e.g. sqrt) that comes with a runtime library or class library! You will need to
write a loop to solve this problem. Furthermore, your method should return as soon as the status of its parameter is known. So
once it is known that the input parameter is a square of some integer, your method should return 1 and once it is known that the input
is not a square, the method should return 0. There should be no wasted loop cycles, your method should be efficent!

The signature of the function is


int isSquare(int n)

Examples:

retu
if n is reason
rn
4 1 because 4 = 2*2
25 1 because 25 = 5*5
because there is no integer that when squared equals -4. (note, -2 squared is 4 not
-4 0
-4)
8 0 because the square root of 8 is not an integer.
0 1 because 0 = 0*0
2. An array is called complete if it contains an even element, a perfect square and two different elements that sum to 8. For example,
{3, 2, 9, 5} is complete because 2 is even, 9 is a perfect square and a[0] + a[3] = 8.

Write a function named isComplete that accepts an integer array and returns 1 if it is a complete array, otherwise it returns 0. Your
method must be efficient. It must return as soon as it is known that the array is complete. Hint: reuse the method you wrote for
question 1.

If you are programming in Java or C#, the function signature is

int isComplete(int[ ] a)

If you are programming in C or C++, the function signature is

int isComplete(int a[ ], int len) where len is the number of elements in the array

Other examples

if the input array is return reason


{36, -28} 1 36 is even, 36 is a perfect square, 36-28 = 8
{36, 28} 0 There are no two elements that sum to 8
It does not have two different elements that sum to 8 (you
{4} 0
can't use a[0]+a[0])
{3, 2, 1, 1, 5, 6} 0 there is no perfect square.
{3, 7, 23, 13, 107, -99, 97, 81} 0 there is no even number.

3. Write a function that takes two arguments, an array of integers and a positive, non-zero number n. It sums n elements of the array
starting at the beginning of the array. If n is greater than the number of elements in the array, the function loops back to the beginning
of the array and continues summing until it has summed n elements. You may assume that the array contains at least one element and
that n is greater than 0.

If you are programming in Java or C#, the function signature is

int loopSum(int[ ] a, int n)

If you are programming in C or C++, the function signature is

int loopSum(int a[ ], int len, int n) where len is the number of elements in the array

Examples

and n
If a is then function returns
is
{1, 2, 3} 2 3 (which is a[0] + a[1])
-1 (which is a[0] + a[1] + a[2] + a[0] + a[1] + a[2] +
{-1, 2, -1} 7
a[0])
{1, 4, 5, 6} 4 16 (which is a[0] + a[1] + a[2] + a[3])
30
{3} 10
(a[0]+a[0]+a[0]+a[0]+a[0]+a[0]+a[0]+a[0]+a[0]+a[0])

1 Write a function named allValuesTheSame that returns 1 if all elements of its argument array have the same
value. Otherwise, it returns 0.
If you are programming in Java or C#, the function signature is
int allValuesTheSame(int[ ] a)

If you are programming in C or C++, the function signature is


int allValuesTheSame(int a[ ], int len) where len is the number of elements in a

Examples:

if a is return
{1, 1, 1, 1} 1
{83, 83, 83} 1
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} 1
{1, -2343456, 1, -2343456} 0 (because there are two different values, 1 and -2343456)
{0, 0, 0, 0, -1} 0 (because there are two different values, 0 and -1)
{432123456} 1

{-432123456} 1
{} 0

2 Write a function named hasNValues which takes an array and an integer n as arguments. It returns true if all
the elements of the array are one of n different values.

If you are writing in Java or C#, the function signature is


int hasNValues(int[ ] a, int n)

If you are writing in C or C++, the function signature is


int hasNValues(int a[ ], int n, int len) where len is the length of a

Note that an array argument is passed by reference so that any change you make to the array in your function
will be visible when the function returns. Therefore, you must not modify the array in your function! In other
words, your function should have no side effects.

Examples

if a is if n is return
{1, 2, 2, 1} 2 1 (because there are 2 different element values, 1 and 2)
{1, 1, 1, 8, 1, 1, 1, 3, 3} 3 1 (because there are 3 different element values, 1, 3, 8)
{1, 2, 3, 4, 5, 6, 7, 8 ,9, 10} 10 1 (because there are 10 different element values)
{1, 2, 2, 1} 3 0 (because there are 2 different element values, not 3 as required)
{1, 1, 1, 8, 1, 1, 1, 3, 3} 2 0 (because there are 3 different element values, not 2 as required)
{1, 2, 3, 4, 5, 6, 7, 8 ,9, 10} 20 0 (because there are 10 different element values, not 20 as required)
Hint: There are many ways to solve this problem. One way is to allocate an array of n integers and add each
unique element found in the array parameter to it. If you add n elements to the array, return 1, otherwise return
0.

3 Write a function named sameNumberOfFactors that takes two integer arguments and returns 1 if they have
the same number of factors. If either argument is negative, return -1. Otherwise return 0.

int sameNumberOfFactors(int n1, int n2)

Examples:

if n1 is and n2 is return
-6 21 -1 (because one of the arguments is negative)
6 21 1 (because 6 has four factors (1, 2, 3, 6) and so does 21 (1, 3, 7, 21)
8 12 0 (because 8 has four factors(1, 2, 4, 8) and 12 has six factors (1, 2, 3, 4, 6, 12)
23 97 1 (because 23 has two factors (1, 23) and so does 97 (1, 97))
0 1 0 (because 0 has no factors, but 1 has one (1))
0 0 1 (always true if n1 == n2)

1. Write a function named eval that returns the value of the polynomial anxn + an-1xn-1 + ... + a1x1 + a0.

If you are programming in Java or C#, the function signature is


double eval(double x, int[ ] a)

If you are programming in C or C++, the function signature is


double eval(double x, int a[ ], int len) where len is the number of elements in the array

Examples:

if x is if the input array is this represents eval should return


1.0 {0, 1, 2, 3, 4} 4x4 + 3x3 + 2x2 + x + 0 10.0
3.0 {3, 2, 1} x2 + 2x + 3 18.0
2.0 {3, -2, -1} -x2 - 2x + 3 -5.0
2
-3.0 {3, 2, 1} x + 2x + 3 6.0
2.0 {3, 2} 2x + 3 7.0
2.0 {4, 0, 9} 9x2 + 4 40.0
2.0 {10} 10 10.0
10.0 {0, 1} x 10.0

Copy and paste your answer here and click the "Submit answer" button
4. A non-empty array a of length n is called an array of all possiblities if it contains all numbers between 0
and a.length-1 inclusive. Write a method named isAllPossibilities that accepts an integer array and returns 1 if
the array is an array of all possiblities, otherwise it returns 0.

If you are programming in Java or C#, the function signature is


int isAllPossibilities(int[ ] a)

If you are programming in C or C++, the function signature is


int isAllPossibilities(int a[ ], int len) where len is the number of elements in the array

Examples

If the input array is return


{1, 2, 0, 3} 1
{3, 2, 1, 0} 1
{1, 2, 4, 3} 0 (because 0 not included and 4 is too big)
{0, 2, 3} 0 (because 1 is not included)
{0} 1
{} 0

Copy and paste your answer here and click the "Submit answer" button

5. An array is called layered if its elements are in ascending order and each element appears two or more
times. For example, {1, 1, 2, 2, 2, 3, 3} is layered but {1, 2, 2, 2, 3, 3} and {3, 3, 1, 1, 1, 2, 2} are not. Write a
method named isLayered that accepts an integer array and returns 1 if the array is layered, otherwise it returns
0.

If you are programming in Java or C#, the function signature is


int isLayered(int[ ] a)

If you are programming in C or C++, the function signature is


int isLayered(int a[ ], int len) where len is the number of elements in the array

Examples:

If the input array is return


{1, 1, 2, 2, 2, 3, 3} 1
{3, 3, 3, 3, 3, 3, 3} 1
{1, 2, 2, 2, 3, 3} 0 (because there is only one occurence of the value 1)
{2, 2, 2, 3, 3, 1, 1} 0 (because values are not in ascending order)
{2} 0
{} 0
Copy and paste your answer here and click the "Submit answer" button

6. A mileage counter is used to measure mileage in an automobile. A mileage counter looks something like this

0 599 8

The above mileage counter says that the car has travelled 5,998 miles. Each mile travelled by the automobile
increments the mileage counter. Here is how the above mileage counter changes over a 3 mile drive.

After the first mile

0 599 9

After the second mile

0 600 0

After the third mile

0 600 1

A mileage counter can be represented as an array. The mileage counter

0 599 8

can be represented as the array


int a[ ] = new int[ ] {8, 9, 9, 5, 0}
Note that the mileage counter is "backwards" in the array, a[0] represents ones, a[1] represents tens, a[2]
represents hundreds, etc.

Write a function named updateMileage that takes an array representation of a mileage counter (which can be
arbitrarily long) and adds a given number of miles to the array. Since arrays are passed by reference you can
update the array in the function, you do not have to return the updated array.

You do not have to do any error checking. You may assume that the array contains non-negative digits and that
the mileage is non-negative

If you are programming in Java or C#, the function signature is


void updateMileage counter(int[ ] a, int miles)

If you are programming in C or C++, the function signature is


void updateMileage counter(int a[ ], int miles, int len) where len is the number of elements in the array
Examples:
if the input array is and the mileage is the array becomes
{8, 9, 9, 5, 0} 1 {9, 9, 9, 5, 0}
{8, 9, 9, 5, 0} 2 {0, 0, 0, 6, 0}
{9, 9, 9, 9, 9, 9, 9, 9, 9, 9} 1 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
{9, 9, 9, 9, 9, 9, 9, 9, 9, 9} 13 {2, 1, 0, 0, 0, 0, 0, 0, 0, 0}

Note that the mileage counter wraps around if it reaches all 9s and there is still some mileage to add.

Hint: Write a helper function that adds 1 to the mileage counter and call the helper function once for each mile

Copy and paste your answer here and click the "Submit answer" button

7. 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.

If you are programming in Java or C#, the function signature is


int isHollow(int[ ] a)

If you are programming in C or C++, the function signature is


int isHollow(int a[ ], int len) where len is the number of elements in the array

Examples:

is
if the input array is reason
hollow?
{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
The number of preceding non-zeros(2) is not equal to the number of
{1,2,0,0,0,3,4,5} no
following non-zeros(3)
The number of preceding non-zeros(3) is not equal to the number of
{3,8,3,0,0,0,3,3} no
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
The number of preceding non-zeros is 0 which equals the number of
{0,0,0} yes
following non-zeros. And there are three zeros "in the middle".

Hint: Write three loops. The first counts the number of preceding non-zeros. The second counts the number of
zeros in the middle. The third counts the number of following non-zeros. Then analyze the results.

Copy and paste your answer here and click the "Submit answer" button
8. A positive number n is consecutive-factored if and only if it has factors, i and j where i > 1, j > 1 and j = i + 1.
Write a function named isConsecutiveFactored that returns 1 if its argument is consecutive-factored, otherwise
it returns 0.

the function signature is


int isConsectiveFactored(int n)

Examples:
If n is return because
24 1 24 = 2*3*4 and 3 = 2 + 1
105 0 105 = 3*5*7 and 5 != 3+1 and 7 != 5+1
90 1 factors of 90 include 2 and 3 and 3 = 2 + 1
23 0 has only 1 factor that is not equal to 1
15 0 15 = 3*5 and 5 != 3 + 1
2 0 2 = 1*2, 2 = 1 + 1 but factor 1 is not greater than 1
0 0 n has to be positive
-12 0 n has to be positive

Copy and paste your answer here and click the "Submit answer" button

9. A twin prime is a prime number that differs from another prime number by 2. Write a function named
isTwinPrime with an integer parameter that returns 1 if the parameter is a twin prime, otherwise it returns 0.
Recall that a prime number is a number with no factors other than 1 and itself.

the function signature is


int isTwinPrime(int n)

Examples:

number is twin prime?


5 yes, 5 is prime, 5+2 is prime
7 yes, 7 is prime, 7-2 is prime
53 no, 53 is prime, but neither 53-2 nor 53+2 is prime
9 no, 9 is not prime

10. Write a function named largestAdjacentSum that iterates through an array computing the sum of adjacent
elements and returning the largest such sum. You may assume that the array has at least 2 elements.
If you are writing in Java or C#, the function signature is
int largestAdjacentSum(int[ ] a)

If you are writing in C or C++, the function signature is


int largestAdjacentSum(int a[ ], int len) where len is the number of elements in a

Examples:

if a is return
{1, 2, 3, 4} 7 because 3+4 is larger than either 1+2 or 2+3
{18, -12, 9, -10} 6 because 18-12 is larger than -12+9 or 9-10
{1,1,1,1,1,1,1,1,1} 2 because all adjacent pairs sum to 2
{1,1,1,1,1,2,1,1,1} 3 because 1+2 or 2+1 is the max sum of adjacent pairs

11. An array is called zero-balanced if its elements sum to 0 and for each positive element n, there exists
another element that is the negative of n. Write a function named isZeroBalanced that returns 1 if its argument
is a zero-balanced array. Otherwise it returns 0.

If you are writing in Java or C#, the function signature is


int isZeroBalanced(int[ ] a)

If you are writing in C or C++, the function signature is


int isZeroBalanced(int a[ ], int len) where len is the number of elements in a

Examples:

if a is return
{1, 2, -2, -1} 1 because elements sum to 0 and each positive element has a corresponding negative element.
{-3, 1, 2, -2, -1, 3} 1 because elements sum to 0 and each positive element has a corresponding negative element.
{3, 4, -2, -3, -2} 0 because even though this sums to 0, there is no element whose value is -4
{0, 0, 0, 0, 0, 0} 1 this is true vacuously; 0 is not a positive number
{3, -3, -3} 0 because it doesn't sum to 0. (Be sure your function handles this array correctly)
{3} 0 because this doesn't sum to 0
{} 0 because it doesn't sum to 0

12. Write a function named largestAdjacentSum that iterates through an array computing the sum of adjacent
elements and returning the largest such sum. You may assume that the array has at least 2 elements.

If you are writing in Java or C#, the function signature is


int largestAdjacentSum(int[ ] a)

If you are writing in C or C++, the function signature is


int largestAdjacentSum(int a[ ], int len) where len is the number of elements in a
Examples:

if a is return
{1, 2, 3, 4} 7 because 3+4 is larger than either 1+2 or 2+3
{18, -12, 9, -10} 6 because 18-12 is larger than -12+9 or 9-10
{1,1,1,1,1,1,1,1,1} 2 because all adjacent pairs sum to 2
{1,1,1,1,1,2,1,1,1} 3 because 1+2 or 2+1 is the max sum of adjacent pairs

13. An array is called zero-balanced if its elements sum to 0 and for each positive element n, there exists
another element that is the negative of n. Write a function named isZeroBalanced that returns 1 if its argument
is a zero-balanced array. Otherwise it returns 0.

If you are writing in Java or C#, the function signature is


int isZeroBalanced(int[ ] a)

If you are writing in C or C++, the function signature is


int isZeroBalanced(int a[ ], int len) where len is the number of elements in a

Examples:

if a is return
{1, 2, -2, -1} 1 because elements sum to 0 and each positive element has a corresponding negative element.
{-3, 1, 2, -2, -1, 3} 1 because elements sum to 0 and each positive element has a corresponding negative element.
{3, 4, -2, -3, -2} 0 because even though this sums to 0, there is no element whose value is -4
{0, 0, 0, 0, 0, 0} 1 this is true vacuously; 0 is not a positive number
{3, -3, -3} 0 because it doesn't sum to 0. (Be sure your function handles this array correctly)
{3} 0 because this doesn't sum to 0
{} 0 because it doesn't sum to 0

This test has three questions on it. You have two hours to complete it. Please do your own work.

1. A BEQ number is one whose cube contains exactly four 6’s. For example, 806 is a BEQ number because 806*806*806 =
523,606,616 which has four 6’s. But 36 is not a BEQ number because its cube is 46,656 which has only three 6’s. And neither is 1,118
because its cube is 1,676,676,672 which contains five 6’s. Write a function named findSmallestBEQ that returns the smallest BEQ
number. The function signature is

int findSmallestBEQnumber( )

Hint use modulo base 10 arithmetic and integer division to get the digits of an integer.
2. An array, a, is called zero-limited if the following two conditions hold:
i. a[3*n+1] is 0 for n>=0 where 3*n+1 is less than the number of elements in the array.
ii. if i != 3*n+1 for some n, then a[i] does not equal 0

For example, {1, 0, 5, -1, 0, 2, 3, 0, 8} is zero-limited because


a[3*0+1] = 0, a[3*1+1] = 0 and a[3*2+1] = 0 and all other elements are non-zero.

Write a function named isZeroLimited that returns 1 if its array argument is zero-limited, else it returns 0.

If you are programming in Java or C#, the function signature is


int isZeroLimited(int[ ] a)

If you are programming in C or C++, the function signature is


int isZeroLimited(int a[ ], int len) where len is the number of elements in a.

Examples
if a is return reason
{0, 0, 0, 0, 0} 0 Only a[1] and a[4] can be 0
{1, 0} 1 a[1] is 0, all other elements are non-zero
{0, 1} 0 a[1] must be 0 (and a[0] cannot be 0)
{5} 1 Note, because the length of the array is 1, there can be
no zero values, since the first one would occur at a[1]
{1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0} 1 Elements a[1], a[4], a[7] and a[10] are 0 and all others
are non zero
{} 1 Since there are no elements, none can fail the condition.

The best answers will make only one pass through the array, i.e, will have only one loop.

The third question is on the next page.


3. An array is called cube-perfect if all its elements are cubes of some integer. For example, {-1, 1, -8, -27, 8} is cube-perfect because
-1 = -1 * -1 * -1
1=1*1*1
-8 = -2 * -2 * -2
-27 = -3 * -3 * -3
8=2*2*2
But {27, 3} is not cube-perfect because 3 is not the cube of any integer.

Write a function named isCubePerfect that returns 1 if its argument is cube-perfect, otherwise it returns 0.

If you are programming in Java or C#, the function signature is


int isCubePerfect(int[ ] a)

If you are programming in C or C++, the function signature is


int isCubePerfect(int a[ ], int len) where len is the number of elements in a.

Examples
if a is return because
{1, 1, 1, 1} 1 all elements are cubes of 1
{64} 1 64 = 4*4*4
{63} 0 63 is not the cube of any integer
{-1, 0, 1} 1 -1 = -1 * -1 * -1, 0 = 0 * 0 * 0, 1=1 * 1 * 1
{} 1 no elements fail the cube test
{3, 7, 21, 36} 0 3 is not the cube of any integer

There are three questions on this test. You have two hours to complete it. Please do your own work.

1. Write a function named countOnes that returns the number of ones in the binary representation of 
its argument. For example, countOnes(9) returns 2 because the binary representation of 9 is 1001. 

Some other examples:
countOnes(5) returns 2 because binary 101 equals 5
countOnes(15) returns 4 because binary 1111 equals 15.

You may assume that the argument is greater than 0.

The function prototype is
   int countOnes(int n);

Hint use modulo and integer arithmetic to count the number of ones.

2. A Daphne array is an array that contains either all odd numbers or all even numbers. For example,
{2, 4, 2} (only even numbers) and {1, 3, 17, ­5} (only odd numbers) are Daphne arrays but {3, 2, 5} is 
not because it contains both odd and even numbers. Write a function named isDaphne that returns 1 
if its array argument is a Daphne array. Otherwise it returns 0.

If you are programming in Java or C#, the function prototype is
   int isDaphne (int[ ] a);

If you are programming in C or C++, the function prototype is
   int isDaphne (int a[ ], int len) where len is the number of elements in the array.

3. An array is defined to be odd­valent if it meets the following two conditions:
   a. It contains a value that occurs more than once
   b. It contains an odd number

For example {9, 3, 4, 9, 1} is odd­valent because 9 appears more than once and 3 is odd. Other odd­
valent arrays are {3, 3, 3, 3} and {8, 8, 8, 4, 4, 7, 2}

The following arrays are not odd­valent:
{1, 2, 3, 4, 5} ­ no value appears more than once.
{2, 2, 2, 4, 4} ­ there are duplicate values but there is no odd value.

Write a function name isOddValent that returns 1 if its array argument is odd­valent, otherwise it 
returns 0.

If you are programming in Java or C#, the function prototype is
   int isOddValent (int[ ] a);

If you are programming in C or C++, the function prototype is
   int isOddValent (int a[ ], int len) where len is the number of elements in the array.

There are 3 questions on this test. You have 2 hours to finish it. Please do your own work.

1. A  normal  number is defined to be one that has no odd factors, except for 1 and possibly itself.


Write a method named isNormal that returns 1 if its integer argument is normal, otherwise it returns 0.

Examples: 1, 2, 3, 4, 5, 7, 8 are normal numbers. 6 and 9 are not normal numbers since 3 is an odd
factor. 10 is not a normal number since 5 is an odd factor. The function signature is 

int isNormal(int n)

2. A non­empty array of length n is called an array of  all possibilities, if it contains all numbers
between 0 and n ­ 1 inclusive. Write a method named isAllPossibilities that accepts an integer array
and returns 1 if the array is an array of all possibilities, otherwise it returns 0. 

Examples {1, 2, 0, 3} is an array of all possibilities, {3, 2, 1, 0} is an array of all possibilities, {1, 2, 4, 3}
is not an array of all possibilities, (because 0 not included and 4 is too big), {0, 2, 3} is not an array of
all possibilities, (because 1 is not included), {0} is an array of all possibilities, {} is not an array of all
possibilities (because array is empty).

If you are programming in Java or C#, the function signature is  
     int isAllPossibilities(int[ ] a)
If you are programming in C or C++, the function signature is  
    int isAllPossibilities(int a[ ], int len) where len is the number of elements in the array.

3. An array is defined to be a Filter array if it meets the following conditions
   a. If it contains 9 then it also contains 11.
   b. If it contains 7 then it does not contain 13.

So {1, 2, 3, 9, 6, 11} and {3, 4, 6, 7, 14, 16}, {1, 2, 3, 4, 10, 11, 13} and {3, 6, 5, 5, 13, 6, 13} are Filter 
arrays. The following arrays are not Filter arrays: {9, 6, 18} (contains 9 but no 11), {4, 7, 13} (contains
both 7 and 13)

Write a function named isFilter that returns 1 if its array argument is a Filter array, otherwise it returns 
0.

If you are programming in Java or C#, the function signature is
   int isFilter(int[ ] a)
If you are programming in C or C++, the function signature is
   int isFilter(int a[ ], int len) where len is the number of elements in the array.

There are three questions on this test. You have two hours to complete it. Please do your own work.

1. Write a function named isDigitSum that returns 1 if sum of all digits of the first argument is less 
than the second argument and 0 otherwise. For example isDigitSum(32121,10 ) would return 1 
because 3+2+1+2+1 = 9 < 10. 

More examples:
isDigitSum(32121,9) returns 0, isDigitSum(13, 6) returns 1, isDigitSum(3, 3) returns 0

The function should return ­1 if either argument is negative, so isDigitSum(­543, 3) returns ­1.

The function signature is 
int isDigitSum(int n, int m)

2. A twin prime is a prime number that differs from another prime number by 2. A Fine array is 
defined to be an array in which every prime in the array has its twin in the array. So {4, 7, 9, 6, 5} is a 
Fine array because 7 and 5 occurs. Note that {4, 9, 6, 33} is a Fine array since there are no primes. 
On the other hand, {3, 8, 15} is not a Fine array since 3 appear in the array but its twin 5 is not in the 
array.

Write a function named isFineArray that returns 1 if its array argument is a Fine array, otherwise it 
returns 0.

If you are programming in Java or C#, the function signature is
   int isFineArray (int [ ] a)

If you are programming in C or C++, the function signature is
   int isFineArray (int a[ ], int len) where len is the number of elements in the array.

You may assume that there exists a function isPrime that returns 1 if it argument is a prime, otherwise
it returns 0. You do not have to write this function.

3. A balanced array is defined to be an array where for every value n in the array, ­n also is in the 
array. For example {­2, 3, 2, ­3} is a balanced array. So is {­2, 2, 2, 2}. But {­5, 2, ­2} is not because 5 
is not in the array.

Write a function named isBalanced that returns 1 if its array argument is a balanced array. Otherwise
it returns 0.

If you are programming in Java or C#, the function signature is
int isBalanced (int [ ] a);
If you are programming in C or C++, the function signature is
int isBalanced(int a[ ], int len) where len is the number of elements in the array.

There are three questions on this test. You have two hours to complete it. Please do your own work. 
You are not allowed to use any methods or functions provided by the system unless explicitly stated 
in the question. In particular, you are not allowed to convert int to a String or vice­versa.

1. An Evens number is an integer whose digits are all even. For example 2426 is an Evens number 
but 3224 is not.

Write a function named isEvens that returns 1 if its integer argument is an Evens number otherwise it 
returns 0.

The function signature is
   int isEvens (int n)

2. An array is defined to be a Magic array if the sum of the primes in the array is equal to the first 
element of the array. If there are no primes in the array, the first element must be 0. So {21, 3, 7, 9, 
11 4, 6} is a Magic array because 3, 7, 11 are the primes in the array and they sum to 21 which is the 
first element of the array. {13, 4, 4, 4, 4} is also a Magic array because the sum of the primes is 13 
which is also the first element. Other Magic arrays are {10, 5, 5}, {0, 6, 8, 20} and {3}. {8, 5, ­5, 5, 3} is
not a Magic array because the sum of the primes is 5+5+3 = 13. Note that ­5 is not a prime because 
prime numbers are positive. 

Write a function named isMagicArray that returns 1 if its integer array argument is a Magic array. 
Otherwise it returns 0.

If you are writing in Java or C#, the function signature is
   int isMagicArray (int[ ] a)

If you are writing in C or C++, the function signature is
   int isMagicArray (int a[ ], int len) where len is the number of elements in the array.
You may assume that a function named isPrime exists that returns 1 if its int argument is a prime, 
otherwise it returns 0. You do not have to write this function! You are allowed to use it.

3. An array is defined to be complete if the conditions (a), (d) and (e) below hold.
  a. The array contains even numbers
  b. Let min be the smallest even number in the array.
  c. Let max be the largest even number in the array.
  d. min does not equal max
  e. All numbers between min and max are in the array

For example {­5, 6, 2, 3, 2, 4, 5, 11, 8, 7} is complete because 
  a. The array contains even numbers
  b. 2 is the smallest even number
  c. 8 is the largest even number
  d. 2 does not equal 8
  e. the numbers 3, 4, 5, 6, 7 are in the array.

Examples of arrays that are not complete are:
{5, 7, 9, 13} condition (a) does not hold, there are no even numbers.
{2, 2} condition (d) does not hold
{2, 6, 3, 4} condition (e) does not hold (5 is missing)

Write a function named isComplete that returns 1 if its array argument is a complete array. Otherwise 
it returns 0.

If you are writing in Java or C#, the function signature is
   int isComplete (int[ ] a)

If you are writing in C or C++, the function signature is
   int isComplete (int a[ ], int len) where len is the number of elements in the array.

There are three questions on this test. You have two hours to complete it. Please do your own work. 
You are not allowed to use any methods or functions provided by the system unless explicitly stated 
in the question. In particular, you are not allowed to convert int to a String or vice­versa.

1. A primeproduct is a positive integer that is the product of exactly two primes greater than 1. For 
example, 22 is primeproduct since 22 = 2 times 11 and both 2 and 11 are primes greater than 1. 
Write a function named isPrimeProduct  with an integer parameter that returns 1 if the parameter is a 
primeproduct, otherwise it returns 0. Recall that a prime number is a positive integer with no factors 
other than 1 and itself. 

You may assume that there exists a function named isPrime(int m) that returns 1 if its m is a prime 
number. You do not need to write isPrime. You are allowed to use this function.

The function signature 
   int isPrimeProduct(int n)
2. An array is called balanced if its even numbered elements (a[0], a[2], etc.) are even and its odd 
numbered elements (a[1], a[3], etc.) are odd.  

Write a function named isBalanced that accepts an array of integers and returns 1 if the array is 
balanced, otherwise it returns 0. 

Examples:  {2, 3, 6, 7} is balanced since a[0] and a[2] are even, a[1] and a[3] are odd. {6, 7, 2, 3, 12} 
is balanced since a[0], a[2] and a[4] are even, a[1] and a[3] are odd. 
{7, 15, 2, 3} is not balanced since a[0] is odd. 
{16, 6, 2, 3} is not balanced since a[1] is even.

If you are programming in Java or C#, the function signature is  
int isBalanced(int[ ] a)

If you are programming in C or C++, the function signature is  
int isBalanced(int a[ ], int len) 
where len is the number of elements in the array.   

3. An array with an odd number of elements is said to be centered if all elements   (except the middle
one) are strictly greater than the value of the middle element. Note that only arrays with an odd
number of  elements  have  a   middle  element.  Write  a   function  named  isCentered  that  accepts  an
integer array and returns 1 if it is a centered array, otherwise it returns 0.  

Examples: {5, 3, 3, 4, 5} is not a centered array (the middle element 3 is not strictly less than all other
elements), {3, 2, 1, 4, 5} is centered (the middle element 1 is strictly less than all other elements), {3,
2, 1, 4, 1} is not centered (the middle element 1 is not strictly less than all other elements), {3, 2, 1, 1,
4, 6} is not centered (no middle element since array has even number of elements), {} is not centered
(no middle element), {1} is centered (satisfies the condition vacuously). 

If you are programming in Java or C#, the function signature is  
int isCentered(int[ ] a)

If you are programming in C or C++, the function signature is  
int isCentered(int a[ ], int len) 
where len is the number of elements in the array.   

There are three questions on this test. You have two hours to complete it. Please do your own work. 
You are not allowed to use any methods or functions provided by the system unless explicitly stated 
in the question. In particular, you are not allowed to convert int to a String or vice­versa.

1. Given a positive integer k, another positive integer n is said to have k­small factors if n can be 
written as a product u*v where u and v are both less than k. For instance, 20 has 10­small factors 
since both 4 and 5 are less than 10 and 4*5 = 20. (For the same reason, it is also true to say that 20 
has 6­small factors, 7­small factors,   8­small factors, etc). However, 22 does not have 10­small 
factors since the only way to factor 22 is as 22 = 2 * 11,   and 11 is not less than 10.
Write a function hasKSmallFactors with signatuare

boolean hasKSmallFactors(int k, int n) 

which returns true if n has k­small factors. The function should return false if either k or n is not 
positive.  

Examples:  
hasKSmallFactors(7, 30) is true (since 5*6 = 30 and 5 < 7, 6 < 7). 
hasKSmallFactors(6, 14) is false (since the only way to factor 14 is 2*7 = 14 and 7 not less than 6)
hasKSmallFactors(6, 30) is false (since 5*6 = 30, 6 not less than 6; 3 * 10 = 30, 10 not less than 6; 2 *
15 = 30, 15 not less than 6) 

2. Write a function fill with signature
 
int[ ] fill(int[ ] arr, int k, int n) 

which does the following: It returns an integer array arr2 of length n whose first k elements are the 
same as the first k elements of arr, and whose remaining elements consist of repeating blocks of the 
first k elements.  You can assume array arr has at least k elements. The function should return null if 
either k or n is not positive.  

Examples:  
fill({1,2,3,5, 9, 12,­2,­1}, 3, 10) returns {1,2,3,1,2,3,1,2,3,1}. 
fill({4, 2, ­3, 12}, 1, 5)  returns {4, 4, 4, 4, 4}. 
fill({2, 6, 9, 0, ­3}, 0, 4) returns null.

3. An array is said to be hollow if it contains 3 or more zeroes in the middle that are preceded and 
followed by the same number of non­zero elements. Write a function named isHollow that  accepts an
integer array and returns 1 if it is a hollow array, otherwise it returns 0

Examples:  isHollow({1,2,4,0,0,0,3,4,5}) returns 1.  isHollow ({1,2,0,0,0,3,4,5}) returns 0. isHollow 
({1,2,4,9, 0,0,0,3,4, 5}) returns 0.  isHollow ({1,2, 0,0, 3,4}) returns 0.

If you are programming in Java or C#, the function signature is
int isHollow(int[ ] a).

If you are C or C++ programmer
int isHollow(int[ ] a,  int len)             
where len is the number of elements in the array.   

There are 3 questions on this test. You have 2 hours to finish it. Please do your own work. All 
you need to write is three functions. Please do not use any string functions. No sorting 
allowed. No additional arrays allowed. Try to write a simple, elegant and correct code.

1. Write a function named minDistance that returns the smallest distance between two factors of a 
number. For example, consider 13013 = 1*7*11*13. Its factors are 1, 7, 11, 13 and 13013. 
minDistance(13013) would return 2 because the smallest distance between any two factors is 2 (13 ­
11 = 2). As another example, minDistance (8) would return 1 because the factors of 8 are 1, 2, 4, 8 
and the smallest distance between any two factors is 1 (2 – 1 = 1).

The function signature is
   int minDistance(int n)

2. A wave array is defined to an array which does not contain two even numbers or two odd 
numbers in adjacent locations. So {7, 2, 9, 10, 5}, {4, 11, 12, 1, 6}, {1, 0, 5} and {2} are all wave 
arrays. But {2, 6, 3, 4} is not a wave array because the even numbers 2 and 6 are adjacent to each 
other.

Write a function named isWave that returns 1 if its array argument is a Wave array, otherwise it 
returns 0.

If you are programming in Java or C#, the function signature is
   int isWave (int [ ] a)

If you are programming in C or C++, the function signature is
   int isWave (int a[ ], int len) where len is the number of elements in the array.

3. An array is defined to be a Bean array if it meets the following conditions
   a. If it contains a 9 then it also contains a 13.
   b. If it contains a 7 then it does not contain a 16.

So {1, 2, 3, 9, 6, 13}  and {3, 4, 6, 7, 13, 15}, {1, 2, 3, 4, 10, 11, 12} and {3, 6, 9, 5, 7, 13, 6, 17} are 
Bean arrays. The following arrays are not Bean arrays:
   a. { 9, 6, 18} (contains a 9 but no 13)
   b. {4, 7, 16} (contains both a 7 and a 16)

Write a function named isBean that returns 1 if its array argument is a Bean array, otherwise it returns
0.

If you are programming in Java or C#, the function signature is
   int isBean (int[ ] a)

If you are programming in C or C++, the function signature is
   int isBean (int a[ ], int len) where len is the number of elements in the array.

There are three questions on this test. You have two hours to complete it. Please do your own work.
You are not allowed to use any methods or functions provided by the system unless explicitly stated
in the question. In particular, you are not allowed to convert int to a String or vice­versa.

1. Write a function named countDigit that returns the number of times that a given digit appears in a 
positive number. For example countDigit(32121, 1) would return 2 because there are two 1s in 32121.
Other examples:
countDigit(33331, 3) returns 4
countDigit(33331, 6) returns 0
countDigit(3, 3) returns 1

The function should return ­1 if either argument is negative, so
countDigit(­543, 3) returns ­1.

The function signature is 
int countDigit(int n, int digit)

Hint: Use modulo base 10 and integer arithmetic to isolate the digits of the number.

2. A Bunker array is defined to be an array in which at least one odd number is immediately followed 
by a prime number. So {4, 9, 6, 7, 3} is a Bunker array because the odd number 7 is immediately 
followed by the prime number 3. But {4, 9, 6, 15, 21} is not a Bunker array because none of the odd 
numbers are immediately followed by a prime number.

Write a function named isBunkerArray that returns 1 if its array argument is a Bunker array, 
otherwise it returns 0.

If you are programming in Java or C#, the function signature is
   int isBunkerArray(int [ ] a)

If you are programming in C or C++, the function signature is
   int isBunkerArray(int a[ ], int len) where len is the number of elements in the array.

You may assume that there exists a function isPrime that returns 1 if it argument is a prime, otherwise
it returns 0. You do not have to write this function.

3. A Meera array is defined to be an array such that for all values n in the array, the value 2*n is not in
the array. So {3, 5, ­2} is a Meera array because 3*2, 5*2 and ­2*2 are not in the array. But {8, 3, 4} is 
not a Meera array because for n=4, 2*n=8 is in the array.

Write a function named isMeera that returns 1 if its array argument is a Meera array. Otherwise it 
returns 0.

If you are programming in Java or C#, the function signature is
   int isMeera(int [ ] a)

If you are programming in C or C++, the function signature is
   int isMeera(int a[ ], int len) where len is the number of elements in the array.

There are three questions on this test. You have two hours to complete it. Please do your own work.
You are not allowed to use any methods or functions provided by the system unless explicitly stated
in the question. In particular, you are not allowed to convert int to a String or vice­versa.
1. A Meera number is a number such that the number of nontrivial factors is a factor of the number. 
For example, 6 is a Meera number because 6 has two nontrivial factors : 2 and 3. (A nontrivial factor 
is a factor other than 1 and the number). Thus 6 has two nontrivial factors. Now, 2 is a factor of 6. 
Thus the number of nontrivial factors is a factor of 6. Hence 6 is a Meera number. Another Meera 
number is 30 because 30 has 2, 3, 5, 6, 10, 15 as nontrivial factors. Thus 30 has 6 nontrivial factors. 
Note that 6 is a factor of 30. So 30 is a Meera Number. However 21 is not a Meera number. The 
nontrivial factors of 21 are 3 and 7. Thus the number of nontrivial factors is 2. Note that 2 is not a 
factor of 21. Therefore, 21 is not a Meera number.

Write a function named isMeera that returns 1 if its integer argument is a Meera number, otherwise it 
returns 0.

The signature of the function is
   int isMeera(int n)

2. A Bunker array is an array that contains the value 1 if and only if it contains a prime number. The 
array {7, 6, 10, 1} is a Bunker array because it contains a prime number (7) and also contains a 1.  
The array {7, 6, 10} is not a Bunker array because it contains a prime number (7) but does not 
contain a 1. The array {6, 10, 1} is not a Bunker array because it contains a 1 but does not contain a 
prime number.

It is okay if a Bunker array contains more than one value 1 and more than one prime, so the array {3, 
7, 1, 8, 1} is a Bunker array (3 and 7 are the primes).

Write a function named isBunker that returns 1 if its array argument is a Bunker array and returns 0 
otherwise.

You may assume the existence of a function named isPrime that returns 1 if its argument is a prime 
and returns 0 otherwise. You do not have to write isPrime, you can just call it.

If you are programming in Java or C#, the function signature is
   int isBunker(int [ ] a)

If you are programming in C or C++, the function signature is
   int isBunker(int a[ ], int len) where len is the number of elements in the array.

3. A Nice array is defined to be an array where for every value n in the array, there is also an 
element n­1 or n+1 in the array. 

For example, {2, 10, 9, 3} is a Nice array because
2 = 3­1
10 = 9+1
3 = 2 + 1
9 = 10 ­1
Other Nice arrays include {2, 2, 3, 3, 3}, {1, 1, 1, 2, 1, 1} and {0, ­1, 1}.

The array {3, 4, 5, 7} is not a Nice array because of the value 7 which requires that the array contains
either the value 6 (7­1) or 8 (7+1) but neither of these values are in the array.

Write a function named isNice that returns 1 if its array argument is a Nice array. Otherwise it returns 
a 0.

If you are programming in Java or C#, the function signature is
   int isNice(int[ ] a)

If you are programming in C or C++, the function signature is
   int isNice(int a[ ], int len) where len is the number of elements in the array.

There are 3 questions on this test. You have two hours to finish it. Please do your own work. 
All you need to write is two functions. Please do not use any string methods. No sorting 
allowed. No additional data structures including arrays allowed. Try to write a simple, elegant 
and correct code.

1. An integer is defined to be “continuous factored” if it can be expressed as the product of two or 
more continuous integers greater than 1.  
Examples of “continuous factored” integers are:
6 = 2 * 3. 
60 = 3 * 4 * 5
120 = 4 * 5 * 6
90 = 9*10
Examples of integers that are NOT “continuous factored” are: 99 = 9*11, 121=11*11, 2=2, 13=13

Write a function named isContinuousFactored(int  n) that returns 1 if n is continuous factored and 0
otherwise.

2. Consider the prime number 11. Note that 13 is also a prime and 13 – 11 = 2. So, 11 and 13 are 
known as twin primes. Similarly, 29 and 31 are twin primes. So is 71 and 73. However, there are 
many primes for which there is no twin. Examples are 23, 67. A twin array is defined to an array 
which every prime that has a twin appear with a twin. Some examples are 
{3, 5, 8, 10, 27},       // 3 and 5 are twins and both are present
{11, 9, 12, 13, 23},       // 11 and 13 are twins and both are present, 23 has no twin
{5, 3, 14, 7, 18, 67}.      // 3 and 5 are twins, 5 and 7 are twins, 67 has no twin

The following are NOT twin arrays:

{13, 14, 15, 3, 5}   // 13 has a twin prime and it is missing in the array
{1, 17, 8, 25, 67}     // 17 has a twin prime and it is missing in the array

Write a function named isTwin(int[ ] arr) that returns 1 if its array argument is a Twin array, otherwise 
it returns 0. 

3. Let us define two arrays as “set equal” if every element in one is also in the other and vice­versa. 
For example, any two of the following are equal to one another: {1, 9, 12}, {12, 1, 9}, {9, 1, 12, 1}, {1, 
9, 12, 9, 12, 1, 9}. Note that {1, 7, 8} is not set equal to {1, 7, 1} or {1, 7, 6}.   

Write a function named isSetEqual(int[ ] a, int[ ] b) that returns 1 if its array arguments are set equal, 
otherwise it returns 0. 

There are 3 questions on this test. You have two hours to finish it. Please do your own work. 
All you need to write is two functions. Please do not use any string methods. No sorting 
allowed. No additional data structures including arrays allowed. Try to write a simple, elegant 
and correct code.

1. An integer is defined to be a Smart number if it is an element in the infinite sequence 1, 2, 4, 7, 11,
16 … Note that 2­1=1, 4­2=2, 7­4=3, 11­7=4, 16­11=5 so for k>1, the kth element of the sequence is 
equal to the k­1th element + k­1. For example, for k=6, 16 is the kth element and is equal to 11 (the k­
1th element) + 5 ( k­1).

Write function named isSmart that returns 1 if its argument is a Smart number, otherwise it returns 0. 
So isSmart(11) returns 1, isSmart(22) returns 1 and isSmart(8) returns 0 .

The function signature is
   int isSmart(int n)

2. An array is defined to be a Nice array if the sum of the primes in the array is equal to the first 
element of the array. If there are no primes in the array, the first element must be 0. So {21, 3, 7, 9, 
11 4, 6} is a Nice array because 3, 7, 11 are the primes in the array and they sum to 21 which is the 
first element of the array. {13, 4, 4,4, 4} is also a Nice array because the sum of the primes is 13 
which is also the first element. Other Nice arrays are {10, 5, 5}, {0, 6, 8, 20} and {3}. {8, 5, ­5, 5, 3} is 
not a Nice array because the sum of the primes is 5+5+3 = 13 but the first element of the array is 8. 
Note that ­5 is not a prime because prime numbers are positive. 

Write a function named isNiceArray that returns 1 if its integer array argument is a Nice array. 
Otherwise it returns 0.

The function signature is
   int isNiceArray (int[ ] a)

You may assume that a function named isPrime exists that returns 1 if its int argument is a prime, 
otherwise it returns 0. You do **not** have to write this function! You just have to call it.

3. An array is defined to be complete if all its elements are greater than 0 and all even numbers that 
are less than the maximum even number are in the array. 

For example {2, 3, 2, 4, 11, 6, 10, 9, 8} is complete because 
  a. all its elements are greater than 0
  b. the maximum even integer is 10
  c. all even numbers that are less than 10 (2, 4, 6, 8) are in the array.

But {2, 3, 3, 6} is not complete because the even number 4 is missing. {2, ­3, 4, 3, 6} is not complete 
because it contains a negative number.

Write a function named isComplete that returns 1 if its array argument is a complete array. Otherwise 
it returns 0.

The function signature is
   int isComplete (int[ ] a)

There are 3 questions on this test. You have two hours to finish it. Please do your own work. All you need to write
is three functions. Please do not use any string methods. No sorting allowed. No additional data structures 
including arrays allowed. Try to write a simple, elegant and correct code.

Two integers are defined to be factor equal, if they have the same number of factors. For example, integers 10 and 33 
are factor equal because, 10 has four factors: 1, 2, 5, 10 and 33 also has four factors: 1, 3, 11, 33. On the other hand, 9 
and 10 are not factor equal since 9 has only three factors: 1, 3, 9 and 10 has four factors: 1, 2, 5, 10.

Write a function named factorEqual(int n, int m) that returns 1 if n and m are factor equal and 0 otherwise.

The signature of the function is
   int factorEqual(int n, int m)

2. Define a Meera array to be an array a if it satisfies two conditions:
(a) a[i] is less than i for i = 0 to a.length­1. 
(b) sum of all elements of a is 0.

For example, {­4, 0, 1, 0, 2, 1} is a Meera array because
­4 = a[0] < 0
0 = a[1] < 1
1 = a[2] < 2
0 = a[3] < 3
2 = a[4] < 4
1 = a[5] < 5

and ­4 + 0 + 1 + 0 + 2 + 1 = 0

{­8, 0, 0, 8, 0} is not a Meera array because a[3] is 8 which is not less than 3. Thus condition (a) fails. {­8, 0, 0, 2, 0} is not 
a Meera array because ­8 + 2 = ­6 not equal to zero. Thus condition (b) fails.

Write a function named isMeera that returns 1 if its array argument is a Meera array. Otherwise it returns 0.

If you are programming in Java or C#, the function signature is
   int isMeera (int[ ] a)

If you are programming in C or C++, the function signature is
   int isMeera (int a[ ], int len) where len is the number of elements in the array.

3. Define a Triple array to be an array where every value occurs exactly three times. 

For example, {3, 1, 2, 1, 3, 1, 3, 2, 2} is a Triple array.

The following arrays are not Triple arrays
{2, 5, 2, 5, 5, 2, 5} (5 occurs four times instead of three times)
{3, 1, 1, 1} (3 occurs once instead of three times)

Write a function named isTriple that returns 1 if its array argument is a Triple array. Otherwise it returns 0.

If you are programming in Java or C#, the function signature is
   int isTriple (int[ ] a)

If you are programming in C or C++, the function signature is
   int isTriple (int a[ ], int len) where len is the number of elements in the array.

There are 3 questions on this test. You have two hours to finish it. Please do your own work. All you need to write
is three functions. Please do not use any string methods. No sorting allowed. No additional data structures 
including arrays allowed. Try to write a simple, elegant and correct code.

1. A Fibonacci number is a number in the sequence 1, 1, 2, 3, 5, 8, 13, 21,…. Note that first two Fibonacci numbers are 
1 and any Fibonacci number other than the first two is the sum of the previous two Fibonacci numbers. For example, 2 = 1
+ 1, 3 = 2 + 1, 5 = 3 + 2 and so on.

Write a function named isFibonacci that returns 1 if its integer argument is a Fibonacci number, otherwise it returns 0.

The signature of the function is
   int isFibonacci (int n)

2. A Meera array is an array that contains the value 0 if and only if it contains a prime number. The array {7, 6, 0, 10, 1} is
a Meera array because it contains a prime number (7) and also contains a 0. The array {6, 10, 1} is a Meera array 
because it contains no prime number and also contains no 0. 

The array {7, 6, 10} is not a Meera array because it contains a prime number (7) but does not contain a 0. The array {6, 
10, 0} is not a Meera array because it contains a 0 but does not contain a prime number.

It is okay if a Meera array contains more than one value 0 and more than one prime, so the array {3, 7, 0, 8, 0, 5} is a 
Meera array (3, 5 and 7 are the primes and there are two zeros.).

Write a function named isMeera that returns 1 if its array argument is a Meera array and returns 0 otherwise.

You may assume the existence of a function named isPrime that returns 1 if its argument is a prime and returns 0 
otherwise. You do not have to write isPrime, you can just call it.

If you are programming in Java or C#, the function signature is
   int isMeera(int [ ] a)

If you are are programming in C or C++, the function signature is
   int isMeera(int a[ ], int len) where len is the number of elements in the array.

3. A Bean array is defined to be an array where for every value n in the array, there is also an element n­1 or n+1 in the 
array. 

For example, {2, 10, 9, 3} is a Bean array because
2 = 3­1
10 = 9+1
3 = 2 + 1
9 = 10 ­1

Other Bean arrays include {2, 2, 3, 3, 3}, {1, 1, 1, 2, 1, 1} and {0, ­1, 1}.

The array {3, 4, 5, 7} is not a Bean array because of the value 7 which requires that the array contains either the value 6 
(7­1) or 8 (7+1) but neither of these values are in the array.

Write a function named isBean that returns 1 if its array argument is a Bean array. Otherwise it returns a 0.

If you are programming in Java or C#, the function signature is
   int isBean(int[ ] a)

If you are programming in C or C++, the function signature is
   int isBean(int a[ ], int len) where len is the number of elements in the array.

There are 3 questions on this test. You have two hours to finish it. Please do your own work. All you need to write
is three functions. Please do not write main method. If you write, you are just wasting your time! Please do not 
use any string methods. No sorting allowed. No additional data structures including arrays allowed. Try to write a
simple, elegant and correct code.

1. A fancy number is a number in the sequence 1, 1, 5, 17, 61, … .Note that first two fancy numbers are 1 and any fancy 
number other than the first two is sum of the three times previous one and two times the one before that. See below: 
1,
1,
3*1 +2*1 = 5
3*5 +2*1 = 17
3*17 + 2*5 = 61
Write a function named isFancy that returns 1 if its integer argument is a Fancy number, otherwise it returns 0.
The signature of the function is
   int isFancy(int n)

2. A Meera array is an array that contains the value 1 if and only if it contains 9. The array {7, 9, 0, 10, 1} is a Meera array
because it contains 1 and 9. The array {6, 10, 8} is a Meera array because it contains no 1 and also contains no 9. 
The array {7, 6, 1} is not a Meera array because it contains 1 but does not contain a 9. The array {9, 10, 0} is not a Meera
array because it contains a 9 but does not contain 1.
It is okay if a Meera array contains more than one value 1 and more than one 9, so the array {1, 1, 0, 8, 0, 9, 9, 1} is a 
Meera array.
Write a function named isMeera that returns 1 if its array argument is a Meera array and returns 0 otherwise.
If you are programming in Java or C#, the function signature is
   int isMeera(int [ ] a)
If you are are programming in C or C++, the function signature is
   int isMeera(int a[ ], int len) where len is the number of elements in the array.

3. A Bean array is defined to be an integer array where for every value n in the array, there is also an element 2n, 2n+1 
or n/2 in the array. 
For example, {4, 9, 8} is a Bean array because
For 4, 8 is present; for 9, 4 is present; for 8, 4 is present.
Other Bean arrays include {2, 2, 5, 11, 23}, {7, 7, 3, 6} and {0}.
The array {3, 8, 4} is not a Bean array because of the value 3 which requires that the array contains either the value 6, 7 
or 1 and none of these values are in the array.
Write a function named isBean that returns 1 if its array argument is a Bean array. Otherwise it returns a 0.

If you are programming in Java or C#, the function signature is
   int isBean(int[ ] a)

If you are programming in C or C++, the function signature is
   int isBean(int a[ ], int len) where len is the number of elements in the array.

There are 3 questions on this test. You have two hours to finish it. Please do your own work. 
All you need to write is three functions. Please do not write main method. If you write, you are 
just wasting your time! Please do not use any string methods. No sorting allowed. No 
additional data structures including arrays allowed. Try to write a simple, elegant and correct 
code.

1. An integer is defined to be a Bunker number if it is an element in the infinite sequence 1, 2, 4, 7, 
11, 16, 22,  … Note that 2­1=1, 4­2=2, 7­4=3, 11­7=4, 16­11=5 so for k>1, the kth element of the 
sequence is equal to the k­1th element + k­1. E.G., for k=6, 16 is the kth element and is equal to 11 
(the k­1th element) + 5 (k­1).
Write function named isBunker  that returns 1 if its argument is a Bunker number, otherwise it returns 
0. So isBunker(11) returns 1, isBunker(22) returns 1 and isBunker(8) returns 0 .
The function signature is
   int isBunker (int  n)

2. Define a Dual array to be an array where every value occurs exactly twice. 
For example, {1, 2, 1, 3, 3, 2} is a dual array.
The following arrays are not Dual arrays
{2, 5, 2, 5, 5} (5 occurs three times instead of two times)
{3, 1, 1, 2, 2} (3 occurs once instead of two times)
Write a function named isDual that returns 1 if its array argument is a Dual array. Otherwise it returns 
0.
If you are programming in Java or C#, the function signature is
   int isDual (int[ ] a)
If you are programming in C or C++, the function signature is
   int isDual (int a[ ], int len) where len is the number of elements in the array.

3. An array is defined to be a Filter array if it meets the following conditions
   a. If it contains 9 then it also contains 11.
   b. If it contains 7 then it does not contain 13.
So {1, 2, 3, 9, 6, 11} and {3, 4, 6, 7, 14, 16}, {1, 2, 3, 4, 10, 11, 13} and {3, 6, 5, 5, 13, 6, 13} are Filter 
arrays. The following arrays are not Filter arrays: {9, 6, 18} (contains 9 but no 11), {4, 7, 13} (contains
both 7 and 13)
Write a function named isFilter  that returns 1 if its array argument is a Filter array, otherwise it returns
0.
If you are programming in Java or C#, the function signature is
   int isFilter(int[ ] a)
If you are programming in C or C++, the function signature is
   int isFilter(int a[ ], int len) where len is the number of elements in the array.

There are 3 questions on this test. You have two hours to finish it. Please do your own work. 
All you need to write is three functions. Please do not write main method. If you write, you are 
just wasting your time! Please do not use any string methods. No sorting allowed. No 
additional data structures including arrays allowed. Try to write a simple, elegant and correct 
code.

Question 1. An array is called balanced if its even numbered elements (a[0], a[2], etc.)
are even and its odd numbered elements (a[1], a[3], etc.) are odd. Write a function
named isBalanced that accepts an array of integers and returns 1 if the array is
balanced, otherwise it returns 0. Examples: {2, 3, 6, 7} is balanced since a[0] and a[2]
are even, a[1] and a[3] are odd. {6, 7, 2, 3, 12} is balanced since a[0], a[2] and a[4] are
even, a[1] and a[3] are odd. {7, 15, 2, 3} is not balanced since a[0] is odd. {16, 6, 2, 3}
is not balanced since a[1] is even.
If you are programming in Java or C#, the function signature is
int isBalanced(int[ ] a)
If you are programming in C or C++, the function signature is
int isBalanced(int a[ ], int len)
where len is the number of elements in the array.

Question 2. An array is defined to be odd-heavy if it contains at least one odd element


and every odd element is greater than every even element. So {11, 4, 9, 2, 8} is odd-
heavy because the two odd elements (11 and 9) are greater than all the even elements.
And {11, 4, 9, 2, 3, 10} is not odd-heavy because the even element 10 is greater than
the odd element 9. Write a function called isOddHeavy that accepts an integer array and
returns 1 if the array is odd-heavy; otherwise it returns 0. Some other examples: {1} is
odd-heavy, {2} is not odd-heavy, {1, 1, 1, 1} is odd-heavy, {2, 4, 6, 8, 11} is odd-heavy,
{-2, -4, -6, -8, -11} is not odd-heavy.
If you are programming in Java or C#, the function signature is
int isOddHeavy(int[ ] a)
If you are programming in C or C++, the function signature is
int isOddHeavy(int a[ ], int len)
where len is the number of elements in the array.
Question 3. A normal number is defined to be one that has no odd factors, except for 1
and possibly itself. Write a method named isNormal that returns 1 if its integer argument
is normal, otherwise it returns 0. The function signature is
int isNormal(int n)
Examples: 1, 2, 3, 4, 5, 7, 8 are normal numbers. 6 and 9 are not normal numbers since
3 is an odd factor. 10 is not a normal number since 5 is an odd factor.

There are 3 questions on this test. You have two hours to finish it. Please do your own work. 
All you need to write is three functions. Please do not write main method. If you write, you are 
just wasting your time! Please do not use any string methods. No sorting allowed. No 
additional data structures including arrays allowed. Try to write a simple, elegant and correct 
code.

Question 1. An array with an odd number of elements is said to be centered if all elements (except
the middle one) are strictly greater than the value of the middle element. Note that only arrays with an
odd number of elements have a middle element. Write a function named isCentered that accepts an
integer array and returns 1 if it is a centered array, otherwise it returns 0. Examples: {1, 2, 3, 4, 5} is not
a centered array (the middle element 3 is not strictly less than all other elements), {3, 2, 1, 4, 5} is
centered (the middle element 1 is strictly less than all other elements), {3, 2, 1, 4, 1} is not centered (the
middle element 1 is not strictly less than all other elements), {3, 2, 1, 1, 4, 6} is not centered (no middle
element since array has even number of elements), {} is not centered (no middle element), {1} is
centered (satisfies the condition vacuously).
If you are programming in Java or C#, the function signature is
int isCentered(int[ ] a)
If you are programming in C or C++, the function signature is
int isCentered(int a[ ], int len)
where len is the number of elements in the array.
Question 2. An array is said to be dual if it has an even number of elements and each pair of
consecutive even and odd elements sum to the same value. Write a function named isDual that accepts an
array of integers and returns 1 if the array is dual, otherwise it returns 0. Examples: {1, 2, 3, 0} is a dual
array (because 1+2 = 3+0 = 3), {1, 2, 2, 1, 3, 0} is a dual array (because 1+2 = 2+1 = 3+0 = 3), {1, 1,
2, 2}</td> is not a dual array (because 1+1 is not equal to 2+2), {1, 2, 1}</td> <td> is not a dual
array (because array does not have an even number of elements), {} is a dual array.
If you are programming in Java or C#, the function signature is
int isDual(int[ ] a)
If you are programming in C or C++, the function signature is
int isDual(int a[ ], int len)
where len is the number of elements in the array.
Question 3. A non-empty array of length n is called an array of all possibilities, if it contains all
numbers between 0 and n - 1 inclusive. Write a method named isAllPossibilities that accepts an integer
array and returns 1 if the array is an array of all possibilities, otherwise it returns 0. Examples {1, 2, 0, 3}
is an array of all possibilities, {3, 2, 1, 0} is an array of all possibilities, {1, 2, 4, 3} is not an array of all
possibilities, (because 0 not included and 4 is too big), {0, 2, 3} is not an array of all possibilities, (because
1 is not included), {0} is an array of all possibilities, {} is not an array of all possibilities (because array is
empty).
If you are programming in Java or C#, the function signature is
int isAllPossibilities(int[ ] a)
If you are programming in C or C++, the function signature is
int isAllPossibilities(int a[ ], int len)
where len is the number of elements in the array.

There are 3 questions on this test. You have two hours to finish it. Please do your own work. 
All you need to write is three functions. Please do not use any string methods. No sorting 
allowed. No additional data structures including arrays allowed. Try to write a simple, elegant 
and correct code.

1. Write a function named factorTwoCount that returns the number of times that 2 divides the 
argument.

For example, factorTwoCount(48) returns 4 because 
48/2 = 24
24/2 = 12
12/2 = 6
6/2 = 3
2 does not divide 3 evenly.

Another example: factorTwoCount(27) returns 0 because 2 does not divide 27.

The function signature is
   int factorTwoCount(int n);

2. A Daphne array is defined to be an array that contains at least one odd number and  begins and 
ends with the same number of even numbers.

So {4, 8, 6, 3, 2, 9, 8,11, 8, 13, 12, 12, 6} is a Daphne array because it begins with three even 
numbers and ends with three even numbers and it contains at least one odd number

The array {2, 4, 6, 8, 6} is not a Daphne array because it does not contain an odd number.

The array {2, 8, 7, 10, ­4, 6} is not a Daphne array because it begins with two even numbers but ends
with three even numbers.

Write a function named isDaphne that returns 1 if its array argument is a Daphne array. Otherwise, it 
returns 0.

If you are writing in Java or C#, the function signature is
   int isDaphne (int[ ] a)

If you are writing in C or C++, the function signature is
   int isDaphne (int a[ ], int len) where len is the number of elements in the array.

3. Write a function called goodSpread that returns 1 if no value in its array argument occurs more 
than 3 times in the array.

For example, goodSpread(new int[] {2, 1, 2, 5, 2, 1, 5, 9} returns 1 because no value occurs more 
than three times.

But goodSpread(new int[ ] {3, 1, 3 ,1, 3, 5, 5, 3} ) returns 0 because the value 3 occurs four times.

If you are writing in Java or C#, the function signature is
   int goodSpread (int[ ] a)

If you are writing in C or C++, the function signature is
   int goodSpread  (int a[ ], int len) where len is the number of elements in the array.

There are 3 questions on this test. You have two hours to finish it. Please do your own work. All you need to write
is three functions. Please do not use any string methods. No sorting allowed. No additional data structures 
including arrays allowed. Try to write a simple, elegant and correct code.

1. Write a function named factorTwoCount that returns the number of times that 2 divides the argument.

For example, factorTwoCount(48) returns 4 because 
48/2 = 24
24/2 = 12
12/2 = 6
6/2 = 3
2 does not divide 3 evenly.

Another example: factorTwoCount(27) returns 0 because 2 does not divide 27.

The function signature is
   int factorTwoCount(int n);

2. A Daphne array is defined to be an array that contains at least one odd number and  begins and ends with the same 
number of even numbers.

So {4, 8, 6, 3, 2, 9, 8,11, 8, 13, 12, 12, 6} is a Daphne array because it begins with three even numbers and ends with 
three even numbers and it contains at least one odd number

The array {2, 4, 6, 8, 6} is not a Daphne array because it does not contain an odd number.

The array {2, 8, 7, 10, ­4, 6} is not a Daphne array because it begins with two even numbers but ends with three even 
numbers.

Write a function named isDaphne that returns 1 if its array argument is a Daphne array. Otherwise, it returns 0.

If you are writing in Java or C#, the function signature is
   int isDaphne (int[ ] a)

If you are writing in C or C++, the function signature is
   int isDaphne (int a[ ], int len) where len is the number of elements in the array.

3. Write a function called goodSpread that returns 1 if no value in its array argument occurs more than 3 times in the 
array.

For example, goodSpread(new int[] {2, 1, 2, 5, 2, 1, 5, 9} returns 1 because no value occurs more than three times.

But goodSpread(new int[ ] {3, 1, 3 ,1, 3, 5, 5, 3} ) returns 0 because the value 3 occurs four times.

If you are writing in Java or C#, the function signature is
   int goodSpread (int[ ] a)

If you are writing in C or C++, the function signature is
   int goodSpread  (int a[ ], int len) where len is the number of elements in the array.

There are three questions on this test. You have two hours to complete it. Please do your own work.

1. Write a function named sumDigits that sums the digits of its integer argument. For example 
sumDigits(3114) returns 9, sumDigits(­6543) returns 18 and sumDigits(0) returns 0. 
The signature of the function is
   int sumDigits (int n)

2. Define a Meera array to be an array where a[n] is less than n for n = 0 to a.length­1. 

For example, {­4, 0, 1, 0, 2} is a Meera array because
a[0] < 0
a[1] < 1
a[2] < 2
a[3] < 3
a[4] < 4

{­1, 0, 0, 8, 0} is not a Meera array because a[3] is 8 which is not less than 3.

Write a function named isMeera  that returns 1 if its array argument is a Meera array. Otherwise it 
returns 0.

If you are programming in Java or C#, the function signature is
   int isMeera (int[ ] a)

If you are programming in C or C++, the function signature is
   int isMeera (int a[ ], int len) where len is the number of elements in the array.

3. Define a Dual array to be an array where every value occurs exactly twice. 

For example, {1, 2, 1, 3, 3, 2} is a dual array.

The following arrays are not Dual arrays
{2, 5, 2, 5, 5} (5 occurs three times instead of two times)
{3, 1, 1, 2, 2} (3 occurs once instead of two times)

Write a function named isDual that returns 1 if its array argument is a Dual array. Otherwise it returns 
0.

If you are programming in Java or C#, the function signature is
   int isDual (int[ ] a)

If you are programming in C or C++, the function signature is
   int isDual (int a[ ], int len) where len is the number of elements in the array.

Hint: you need a nested loop.

There are three questions on this test. You have two hours to complete it. Please do your own work.
1. An integer is defined to be a Guthrie number if it is an element in the infinite sequence 1, 2, 4, 7, 11, 16 … Note that 2­
1=1, 4­2=2, 7­4=3, 11­7=4, 16­11=5 so for k>1, the kth element of the sequence is equal to the k­1th element + k­1. E.G., 
for k=6, 16 is the kth element and is equal to 11 (the k­1th element) + k­1.

Write function named isGuthrie that returns 1 if its argument is a Guthrie number, otherwise it returns 0. So isGuthrie(11) 
returns 1, is Guthrie(22) returns 1 and isGuthrie(8) returns 0 .

The function signature is
   int isGuthrie (int n)

2. An array is defined to be a Bean array if the sum of the primes in the array is equal to the first element of the array. If 
there are no primes in the array, the first element must be 0. So {21, 3, 7, 9, 11 4, 6} is a Bean array because 3, 7, 11 are 
the primes in the array and they sum to 21 which is the first element of the array. {13, 4, 4,4, 4} is also a Bean array 
because the sum of the primes is 13 which is also the first element. Other Bean arrays are {10, 5, 5}, {0, 6, 8, 20} and {3}. 
{8, 5, ­5, 5, 3} is not a Bean array because the sum of the primes is 5+5+3 = 13 but the first element of the array is 8. 
Note that ­5 is not a prime because prime numbers are positive. 

Write a function named isBeanArray that returns 1 if its integer array argument is a Bean array. Otherwise it returns 0.

If you are writing in Java or C#, the function signature is
   int isBeanArray (int[ ] a)

If you are writing in C or C++, the function signature is
   int isBeanArray (int a[ ], int len) where len is the number of elements in the array.

You may assume that a function named isPrime exists that returns 1 if its int argument is a prime, otherwise it returns 0. 
You do **not** have to write this function! You just have to call it.

3. An array is defined to be complete if all its elements are greater than 0 and all even numbers that are less than the 
maximum even number are in the array. 

For example {2, 3, 2, 4, 11, 6, 10, 9, 8} is complete because 
  a. all its elements are greater than 0
  b. the maximum even integer is 10
  c. all even numbers that are less than 10 (2, 4, 6, 8) are in the array.

But {2, 3, 3, 6} is not complete because the even number 4 is missing. {2, ­3, 4, 3, 6} is not complete because it contains 
a negative number.

Write a function named isComplete that returns 1 if its array argument is a complete array. Otherwise it returns 0.

If you are writing in Java or C#, the function signature is
   int isComplete (int[ ] a)

If you are writing in C or C++, the function signature is
   int isComplete (int a[ ], int len) where len is the number of elements in the array.

There are three questions on this test. You have 2 hours to complete it. Please do your own work.

1. An integer is defined to be a Bunker number if it is an element in the infinite sequence 1, 2, 4, 7, 
11, 16, 22,  … Note that 2­1=1, 4­2=2, 7­4=3, 11­7=4, 16­11=5 so for k>1, the kth element of the 
sequence is equal to the k­1th element + k­1. E.G., for k=6, 16 is the kth element and is equal to 11 
(the k­1th element) + 5 (k­1).

Write function named isBunker  that returns 1 if its argument is a Bunker number, otherwise it returns 
0. So isBunker(11) returns 1, isBunker(22) returns 1 and isBunker(8) returns 0 .

The function signature is
   int isBunker (int  n)

2. Define a Dual array to be an array where every value occurs exactly twice. 

For example, {1, 2, 1, 3, 3, 2} is a dual array.

The following arrays are not Dual arrays
{2, 5, 2, 5, 5} (5 occurs three times instead of two times)
{3, 1, 1, 2, 2} (3 occurs once instead of two times)

Write a function named isDual that returns 1 if its array argument is a Dual array. Otherwise it returns 
0.

If you are programming in Java or C#, the function signature is
   int isDual (int[ ] a)

If you are programming in C or C++, the function signature is
   int isDual (int a[ ], int len) where len is the number of elements in the array.

3. An array is defined to be a Filter array if it meets the following conditions
   a. If it contains 9 then it also contains 11.
   b. If it contains 7 then it does not contain 13.

So {1, 2, 3, 9, 6, 11} and {3, 4, 6, 7, 14, 16}, {1, 2, 3, 4, 10, 11, 13} and {3, 6, 5, 5, 13, 6, 13} are Filter 
arrays. The following arrays are not Filter arrays: {9, 6, 18} (contains 9 but no 11), {4, 7, 13} (contains
both 7 and 13)

Write a function named isFilter  that returns 1 if its array argument is a Filter array, otherwise it returns
0.

If you are programming in Java or C#, the function signature is
   int isFilter(int[ ] a)
If you are programming in C or C++, the function signature is
   int isFilter(int a[ ], int len) where len is the number of elements in the array.

There are 3 questions on this test. You have two hours to finish it. Please do your own work. All you need to write
is three functions. Please do not use any string methods. No sorting allowed. No additional data structures 
including arrays allowed. Try to write a simple, elegant and correct code.

1. A Fibonacci number is a number in the sequence 1, 1, 2, 3, 5, 8, 13, 21,…. Note that first two Fibonacci numbers are 
1 and any Fibonacci number other than the first two is the sum of the previous two Fibonacci numbers. For example, 2 = 1
+ 1, 3 = 2 + 1, 5 = 3 + 2 and so on.

Write a function named isFibonacci that returns 1 if its integer argument is a Fibonacci number, otherwise it returns 0.

The signature of the function is
   int isFibonacci (int n)

2. A Meera array is an array that contains the value 0 if and only if it contains a prime number. The array {7, 6, 0, 10, 1} is
a Meera array because it contains a prime number (7) and also contains a 0. The array {6, 10, 1} is a Meera array 
because it contains no prime number and also contains no 0. 

The array {7, 6, 10} is not a Meera array because it contains a prime number (7) but does not contain a 0. The array {6, 
10, 0} is not a Meera array because it contains a 0 but does not contain a prime number.

It is okay if a Meera array contains more than one value 0 and more than one prime, so the array {3, 7, 0, 8, 0, 5} is a 
Meera array (3, 5 and 7 are the primes and there are two zeros.).

Write a function named isMeera that returns 1 if its array argument is a Meera array and returns 0 otherwise.

You may assume the existence of a function named isPrime that returns 1 if its argument is a prime and returns 0 
otherwise. You do not have to write isPrime, you can just call it.

If you are programming in Java or C#, the function signature is
   int isMeera(int [ ] a)

If you are are programming in C or C++, the function signature is
   int isMeera(int a[ ], int len) where len is the number of elements in the array.

3. A Bean array is defined to be an array where for every value n in the array, there is also an element n­1 or n+1 in the 
array. 

For example, {2, 10, 9, 3} is a Bean array because
2 = 3­1
10 = 9+1
3 = 2 + 1
9 = 10 ­1

Other Bean arrays include {2, 2, 3, 3, 3}, {1, 1, 1, 2, 1, 1} and {0, ­1, 1}.

The array {3, 4, 5, 7} is not a Bean array because of the value 7 which requires that the array contains either the value 6 
(7­1) or 8 (7+1) but neither of these values are in the array.

Write a function named isBean that returns 1 if its array argument is a Bean array. Otherwise it returns a 0.

If you are programming in Java or C#, the function signature is
   int isBean(int[ ] a)

If you are programming in C or C++, the function signature is
   int isBean(int a[ ], int len) where len is the number of elements in the array.
There are 3 questions on this test. You have 2 hours to finish it. Please do your own work. All 
you need to write is three functions. Please do not use any string functions. No sorting 
allowed. No additional arrays allowed. Try to write a simple, elegant and correct code.

1. Write a function named minDistance that returns the smallest distance between two factors of a 
number. For example, consider 13013 = 1*7*11*13. Its factors are 1, 7, 11, 13 and 13013. 
minDistance(13013) would return 2 because the smallest distance between any two factors is 2 (13 ­
11 = 2). As another example, minDistance (8) would return 1 because the factors of 8 are 1, 2, 4, 8 
and the smallest distance between any two factors is 1 (2 – 1 = 1).

The function signature is
   int minDistance(int n)

2. A wave array is defined to an array which does not contain two even numbers or two odd 
numbers in adjacent locations. So {7, 2, 9, 10, 5}, {4, 11, 12, 1, 6}, {1, 0, 5} and {2} are all wave 
arrays. But {2, 6, 3, 4} is not a wave array because the even numbers 2 and 6 are adjacent to each 
other.

Write a function named isWave that returns 1 if its array argument is a Wave array, otherwise it 
returns 0.

If you are programming in Java or C#, the function signature is
   int isWave (int [ ] a)

If you are programming in C or C++, the function signature is
   int isWave (int a[ ], int len) where len is the number of elements in the array.

3. An array is defined to be a Bean array if it meets the following conditions
   a. If it contains a 9 then it also contains a 13.
   b. If it contains a 7 then it does not contain a 16.

So {1, 2, 3, 9, 6, 13}  and {3, 4, 6, 7, 13, 15}, {1, 2, 3, 4, 10, 11, 12} and {3, 6, 9, 5, 7, 13, 6, 17} are 
Bean arrays. The following arrays are not Bean arrays:
   a. { 9, 6, 18} (contains a 9 but no 13)
   b. {4, 7, 16} (contains both a 7 and a 16)

Write a function named isBean that returns 1 if its array argument is a Bean array, otherwise it returns
0.

If you are programming in Java or C#, the function signature is
   int isBean (int[ ] a)

If you are programming in C or C++, the function signature is
   int isBean (int a[ ], int len) where len is the number of elements in the array.
There are three questions on this test. You have two hours to complete it. Please do your own work. You are not allowed to
use any methods or functions provided by the system unless explicitly stated in the question. In particular, you are not allowed
to convert int to a String or vice­versa.

1. A Meera number is a number such that the number of nontrivial factors is a factor of the number. For example, 6 is a Meera number
because 6 has two nontrivial factors : 2 and 3. (A nontrivial factor is a factor other than 1 and the number). Thus 6 has two nontrivial 
factors. Now, 2 is a factor of 6. Thus the number of nontrivial factors is a factor of 6. Hence 6 is a Meera number. Another Meera 
number is 30 because 30 has 2, 3, 5, 6, 10, 15 as nontrivial factors. Thus 30 has 6 nontrivial factors. Note that 6 is a factor of 30. So 30 
is a Meera Number. However 21 is not a Meera number. The nontrivial factors of 21 are 3 and 7. Thus the number of nontrivial factors 
is 2. Note that 2 is not a factor of 21. Therefore, 21 is not a Meera number.

Write a function named isMeera that returns 1 if its integer argument is a Meera number, otherwise it returns 0.

The signature of the function is

   int isMeera(int n)

2. A Bunker array is an array that contains the value 1 if and only if it contains a prime number. The array {7, 6, 10, 1} is 
a Bunker array because it contains a prime number (7) and also contains a 1.  The array {7, 6, 10} is not a Bunker array because it 
contains a prime number (7) but does not contain a 1. The array {6, 10, 1} is not a Bunker array because it contains a 1 but does not 
contain a prime number.

It is okay if a Bunker array contains more than one value 1 and more than one prime, so the array {3, 7, 1, 8, 1} is a Bunker array (3 
and 7 are the primes).

Write a function named isBunker that returns 1 if its array argument is a Bunker array and returns 0 otherwise.

You may assume the existence of a function named isPrime that returns 1 if its argument is a prime and returns 0 otherwise. You do 
not have to write isPrime, you can just call it.

If you are programming in Java or C#, the function signature is

   int isBunker(int [ ] a)

If you are programming in C or C++, the function signature is

   int isBunker(int a[ ], int len) where len is the number of elements in the array.

3. A Nice array is defined to be an array where for every value n in the array, there is also an element n­1 or n+1 in the array.

For example, {2, 10, 9, 3} is a Nice array because

2 = 3­1

10 = 9+1

3 = 2 + 1

9 = 10 ­1

Other Nice arrays include {2, 2, 3, 3, 3}, {1, 1, 1, 2, 1, 1} and {0, ­1, 1}.

The array {3, 4, 5, 7} is not a Nice array because of the value 7 which requires that the array contains either the value 6 (7­1) or 8 
(7+1) but neither of these values are in the array.
Write a function named isNice that returns 1 if its array argument is a Nice array. Otherwise it returns a 0.

If you are programming in Java or C#, the function signature is

   int isNice(int[ ] a)

If you are programming in C or C++, the function signature is

   int isNice(int a[ ], int len) where len is the number of elements in the array.

There are three questions on this test. You have two hours to complete it. Please do your 
own work.

1. A Pascal number is a number that is the sum of the integers from 1 to j for some j. For
example 6 is a Pascal number because 6 = 1 + 2 + 3. Here j is 3. Another Pascal number 
is 15 because 15 = 1 + 2 + 3 + 4 + 5. An example of a number that is not a Pascal number
is 7 because it falls between the Pascal numbers 6 and 10.

Write a function named isPascal that returns 1 if its integer argument is a Pascal number, 
otherwise it returns 0.

The signature of the function is

   int isPascal (int n)

2. A Meera array is an array that contains the value 1 if and only if it contains a prime 
number. The array {7, 6, 10, 1} is a Meera array because it contains a prime number (7) 
and also contains a 1.  The array {7, 6, 10} is not a Meera array because it contains a 
prime number (7) but does not contain a 1. The array {6, 10, 1} is not a Meera array 
because it contains a 1 but does not contain a prime number.

It is okay if a Meera array contains more than one value 1 and more than one prime, so 
the array {3, 7, 1, 8, 1} is a Meera array (3 and 7 are the primes).

Write a function named isMeera that returns 1 if its array argument is a Meera array and 
returns 0 otherwise.

You may assume the existence of a function named isPrime that returns 1 if its argument 
is a prime and returns 0 otherwise. You do not have to write isPrime, you can just call it.

If you are programming in Java or C#, the function signature is

   int isMeera (int [ ] a)

If you are are programming in C or C++, the function signature is
   int isMeera (int a[ ], int len) where len is the number of elements in the array.

3. A Suff array is defined to be an array where for every value n in the array, there is 
also an element n­1 or n+1 in the array.

For example, {2, 10, 9, 3} is a Suff array because

2 = 3­1

10 = 9+1

3 = 2 + 1

9 = 10 ­1

Other Suff arrays include {2, 2, 3, 3, 3}, {1, 1, 1, 2, 1, 1} and {0, ­1, 1}.

The array {3, 4, 5, 7} is not a Suff array because of the value 7 which requires that the 
array contains either the value 6 (7­1) or 8 (7+1) but neither of these values are in the 
array.

Write a function named isSuff that returns 1 if its array argument is a Suff array. 
Otherwise it returns a 0.

If you are programming in Java or C#, the function signature is

   int isSuff (int[ ] a)

If you are programming in C or C++, the function signature is

   int isSuff (int a[ ], int len) where len is the number of elements in the array.

March 7, 2015

There are 3 questions on this test. You have two hours to finish it. Please do your own work. 
All you need to write is three functions. Please do not use any string methods. No sorting 
allowed. No additional data structures including arrays allowed. Try to write a simple, elegant 
and correct code.

QUESTION 1. An array is called balanced if its even numbered elements (a[0], a[2], etc.) are even and its odd
numbered elements (a[1], a[3], etc.) are odd. Write a function named isBalanced that accepts an array of
integers and returns 1 if the array is balanced, otherwise it returns 0. Examples: {2, 3, 6, 7} is balanced since
a[0] and a[2] are even, a[1] and a[3] are odd. {6, 7, 2, 3, 12} is balanced since a[0], a[2] and a[4] are even, a[1]
and a[3] are odd. {7, 15, 2, 3} is not balanced since a[0] is odd. {16, 6, 2, 3} is not balanced since a[1] is even.

If you are programming in Java or C#, the function signature is

int isBalanced(int[ ] a)

If you are programming in C or C++, the function signature is

int isBalanced(int a[ ], int len)

where len is the number of elements in the array.

QUESTION 2. An array is defined to be odd-heavy if it contains at least one odd element and every odd
element is greater than every even element. So {11, 4, 9, 2, 8} is odd-heavy because the two odd elements (11
and 9) are greater than all the even elements. And {11, 4, 9, 2, 3, 10} is not odd-heavy because the even element
10 is greater than the odd element 9. Write a function called isOddHeavy that accepts an integer array and
returns 1 if the array is odd-heavy; otherwise it returns 0. Some other examples: {1} is odd-heavy, {2} is not
odd-heavy, {1, 1, 1, 1} is odd-heavy, {2, 4, 6, 8, 11} is odd-heavy, {-2, -4, -6, -8, -11} is not odd-heavy.

If you are programming in Java or C#, the function signature is

int isOddHeavy(int[ ] a)

If you are programming in C or C++, the function signature is

int isOddHeavy(int a[ ], int len)

where len is the number of elements in the array.

QUESTION 3. A normal number is defined to be one that has no odd factors, except for 1 and possibly itself.
Write a method named isNormal that returns 1 if its integer argument is normal, otherwise it returns 0. The
function signature is

int isNormal(int n)

Examples: 1, 2, 3, 4, 5, 7, 8 are normal numbers. 6 and 9 are not normal numbers since 3 is an odd factor. 10 is
not a normal number since 5 is an odd factor.

March 21, 2015

There are three questions on this test. You have two hours to complete it. Please do your own work.
You are not allowed to use any methods or functions provided by the system unless explicitly stated
in the question. In particular, you are not allowed to convert int to a String or vice­versa. You are not
allowed to use any additional data structures.

1. A Meera number is a number such that the number of nontrivial factors is a factor of the number. 
For example, 6 is a Meera number because 6 has two nontrivial factors : 2 and 3. (A nontrivial factor 
is a factor other than 1 and the number). Thus 6 has two nontrivial factors. Now, 2 is a factor of 6. 
Thus the number of nontrivial factors is a factor of 6. Hence 6 is a Meera number. Another Meera 
number is 30 because 30 has 2, 3, 5, 6, 10, 15 as nontrivial factors. Thus 30 has 6 nontrivial factors. 
Note that 6 is a factor of 30. So 30 is a Meera Number. However 21 is not a Meera number. The 
nontrivial factors of 21 are 3 and 7. Thus the number of nontrivial factors is 2. Note that 2 is not a 
factor of 21. Therefore, 21 is not a Meera number.

Write a function named isMeera that returns 1 if its integer argument is a Meera number, otherwise it 
returns 0.

The signature of the function is
   int isMeera(int n)

2. A Bunker array is an array that contains the value 1 if and only if it contains a prime number. The 
array {7, 6, 10, 1} is a Bunker array because it contains a prime number (7) and also contains a 1.  
The array {7, 6, 10} is not a Bunker array because it contains a prime number (7) but does not 
contain a 1. The array {6, 10, 1} is not a Bunker array because it contains a 1 but does not contain a 
prime number.

It is okay if a Bunker array contains more than one value 1 and more than one prime, so the array {3, 
7, 1, 8, 1} is a Bunker array (3 and 7 are the primes).

Write a function named isBunker that returns 1 if its array argument is a Bunker array and returns 0 
otherwise.

You may assume the existence of a function named isPrime that returns 1 if its argument is a prime 
and returns 0 otherwise. You do not have to write isPrime, you can just call it.

If you are programming in Java or C#, the function signature is
   int isBunker(int [ ] a)

If you are programming in C or C++, the function signature is
   int isBunker(int a[ ], int len) where len is the number of elements in the array.

3. A Nice array is defined to be an array where for every value n in the array, there is also an 
element n­1 or n+1 in the array. 

For example, {2, 10, 9, 3} is a Nice array because
2 = 3­1
10 = 9+1
3 = 2 + 1
9 = 10 ­1

Other Nice arrays include {2, 2, 3, 3, 3}, {1, 1, 1, 2, 1, 1} and {0, ­1, 1}.
The array {3, 4, 5, 7} is not a Nice array because of the value 7 which requires that the array contains
either the value 6 (7­1) or 8 (7+1) but neither of these values are in the array.

Write a function named isNice that returns 1 if its array argument is a Nice array. Otherwise it returns 
a 0.

If you are programming in Java or C#, the function signature is
   int isNice(int[ ] a)

If you are programming in C or C++, the function signature is
   int isNice(int a[ ], int len) where len is the number of elements in the array.

April 4, 2015

There are 3 questions on this test. You have 2 hours to finish it. Please do your own work. All 
you need to write is three functions. Please do not use any string functions. No sorting 
allowed. No additional arrays allowed. Try to write a simple, elegant and correct code.

1. Write a function named minDistance that returns the smallest distance between two factors of a 
number. For example, consider 13013 = 1*7*11*13. Its factors are 1, 7, 11, 13 and 13013. 
minDistance(13013) would return 2 because the smallest distance between any two factors is 2 (13 ­
11 = 2). As another example, minDistance (8) would return 1 because the factors of 8 are 1, 2, 4, 8 
and the smallest distance between any two factors is 1 (2 – 1 = 1).

The function signature is
   int minDistance(int n)

2. A wave array is defined to an array which does not contain two even numbers or two odd 
numbers in adjacent locations. So {7, 2, 9, 10, 5}, {4, 11, 12, 1, 6}, {1, 0, 5} and {2} are all wave 
arrays. But {2, 6, 3, 4} is not a wave array because the even numbers 2 and 6 are adjacent to each 
other.

Write a function named isWave that returns 1 if its array argument is a Wave array, otherwise it 
returns 0.

If you are programming in Java or C#, the function signature is
   int isWave (int [ ] a)

If you are programming in C or C++, the function signature is
   int isWave (int a[ ], int len) where len is the number of elements in the array.

3. An array is defined to be a Bean array if it meets the following conditions
   a. If it contains a 9 then it also contains a 13.
   b. If it contains a 7 then it does not contain a 16.

So {1, 2, 3, 9, 6, 13}  and {3, 4, 6, 7, 13, 15}, {1, 2, 3, 4, 10, 11, 12} and {3, 6, 9, 5, 7, 13, 6, 17} are 
Bean arrays. The following arrays are not Bean arrays:
   a. { 9, 6, 18} (contains a 9 but no 13)
   b. {4, 7, 16} (contains both a 7 and a 16)

Write a function named isBean that returns 1 if its array argument is a Bean array, otherwise it returns
0.

If you are programming in Java or C#, the function signature is
   int isBean (int[ ] a)

If you are programming in C or C++, the function signature is
   int isBean (int a[ ], int len) where len is the number of elements in the array.

2015 April 18th test

There are three questions on this test. Please do your own work. All you need to write is
three functions. Please do not use any String functions. No sorting allowed. No 
additional arrays or data structures allowed. Try to write a simple, elegant and correct 
code. If you are not a Java or C# programmer, you can assume one more argument int len, to 
pass the length of the array.

Question 1. Write a function fill with signature

int[] fill(int[] arr, int k, int n)

which does the following: It returns an integer array arr2 of length n whose first k elements are the same as the
first k elements of arr, and whose remaining elements consist of repeating blocks of the first k elements. You
can assume array arr has at least k elements. The function should return null if either k or n is not positive.

Examples: fill({1,2,3,5, 9, 12,-2,-1}, 3, 10) returns {1,2,3,1,2,3,1,2,3,1}. Fill({4, 2, -3, 12}, 1, 5) returns {4, 4,
4, 4, 4}. fill({2, 6, 9, 0, -3}, 0, 4) returns null.

Question 2. Write a function sumIsPower with signatuare


boolean sumIsPower(int[] arr)
which outputs true if the sum of the elements in the input array arr is a power of 2, false otherwise. Recall that
the powers of 2 are 1, 2, 4, 8, 16, and so on. In general a number is a power of 2 if and only if it is of the form 2 n
for some nonnegative integer n. You may assume (without verifying in your code) that all elements in the array
are positive integers. If the input array arr is null, the return value should be false.
Examples: sumIsPower({8,8,8,8}) is true since 8 + 8 + 8 + 8 = 32 = 2 5. sumIsPower({8,8,8}) is false, since 8
+ 8 +8 = 24, not a power of 2.
Question 3. 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. Write a function named isHollow that accepts an integer
array and returns 1 if it is a hollow array, otherwise it returns 0. The function signature is
int isHollow(int[ ] a).
Examples: isHollow({1,2,4,0,0,0,3,4,5}) returns true. isHollow ({1,2,0,0,0,3,4,5}) returns false. : isHollow
({1,2,4,9, 0,0,0,3,4, 5}) returns false. isHollow ({1,2, 0,0, 3,4}) returns false.

May 2nd 2015

There are 3 questions on this test. You have two hours to finish it. Please do 
your own work. All you need to write is three functions. Please do not use any
string methods. No sorting allowed. No additional data structures including 
arrays allowed. Try to write a simple, elegant and correct code.

1. A Fibonacci number is a number in the sequence 1, 1, 2, 3, 5, 8, 13, 21,…. Note that 
first two Fibonacci numbers are 1 and any Fibonacci number other than the first two is the 
sum ofthe previous two Fibonacci numbers. For example, 2 = 1 + 1, 3 = 2 + 1, 5 = 3 + 2 
and so on.

Write a function named isFibonacci that returns 1 if its integer argument is 
a Fibonacci number, otherwise it returns 0.

The signature of the function is

   int isFibonacci (int n)

2. A Meera array is an array that contains the value 0 if and only if it contains a prime 
number. The array {7, 6, 0, 10, 1} is a Meera array because it contains a prime number (7)
and also contains a 0. The array {6, 10, 1} is a Meera array because it contains no prime 
number and also contains no 0.
The array {7, 6, 10} is not a Meera array because it contains a prime number (7) but does 
not contain a 0. The array {6, 10, 0} is not a Meera array because it contains a 0 but does 
not contain a prime number.

It is okay if a Meera array contains more than one value 0 and more than one prime, so 
the array {3, 7, 0, 8, 0, 5} is a Meera array (3, 5 and 7 are the primes and there are two 
zeros.).

Write a function named isMeera that returns 1 if its array argument is a Meera array and 
returns 0 otherwise.

You may assume the existence of a function named isPrime that returns 1 if its argument 
is a prime and returns 0 otherwise. You do not have to write isPrime, you can just call it.

If you are programming in Java or C#, the function signature is

   int isMeera(int [ ] a)

If you are are programming in C or C++, the function signature is

   int isMeera(int a[ ], int len) where len is the number of elements in the array.

3. A Bean array is defined to be an array where for every value n in the array, there is 
also an element n­1 or n+1 in the array.

For example, {2, 10, 9, 3} is a Bean array because

2 = 3­1

10 = 9+1

3 = 2 + 1
9 = 10 ­1

Other Bean arrays include {2, 2, 3, 3, 3}, {1, 1, 1, 2, 1, 1} and {0, ­1, 1}.

The array {3, 4, 5, 7} is not a Bean array because of the value 7 which requires that the 
array contains either the value 6 (7­1) or 8 (7+1) but neither of these values are in the 
array.

Write a function named isBean that returns 1 if its array argument is a Bean array. 
Otherwise it returns a 0.

If you are programming in Java or C#, the function signature is

   int isBean(int[ ] a)

If you are programming in C or C++, the function signature is

   int isBean(int a[ ], int len) where len is the number of elements in the array.

You might also like