Exam Problems Renamed
Exam Problems Renamed
Exam Problems Renamed
7. 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:
retur
if n is because
n
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
0 0 because n must be greater than 0.
-81 0 because n must be greater than 0.
8. 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
{-1, 5, 8, 17, 15} -6392
because abs(-1-5)=6, abs(5-8)=3, abs(8-17)=9, abs(17-
15)=2;
the number is negative because the first element of the array
is
negative
{1, 5, 8, 17, 15} 4392
because abs(1-5)=4, remaining digits are the same as
previous
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.
9. 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
a is function reason
returns
{0, 0, 0, 0, 0}1 1 because there is one sequence of 0s and its
length >= 4.
because there are two
{1, 2, 0, 0, 0, 0, 2, -18, 0, 0, 0, 0, 0, 12}1 2 sequences of 0s and
both have lengths >= 4.
because three are three
{0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0}1 3 sequences of zeros
and all have length >=4
because there must be at
{1, 2, 3, 4}1 0
least one 0.
because there is a sequence
{1, 0, 0, 0, 2, 0, 0, 0, 0} 0 of zeros whose
length is less < 4.
because there is a sequence
{0} 0 of zeroes
whose length is < 4.
11. 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
{1} 0 because the digit 0 is represented by
no zeros followed by a one.
{0, 1} 1 because the digit 1 is represented by
one zero followed by a one.
because the encoding of
a negative
number begins with a -1
{-1, 0, 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.
because each 9 digit is
{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 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.
12. 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
a is function 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
{2} 1 because there is no j, k where a[j]+a[k] > 10 and
j+k=length of
array and j!=k
{} 1 because there is no j, k where a[j]+a[k] > 10 and
j+k=length of
array and j!=k
{-2, 5, 0, 5, 12} 1 because -2+12 <= 10 and 5+5 <= 10
13. 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 retur
because
is n
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.
14. 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
15. 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}
then function
n is reason
returns
0 {1} because the digit 0
is represented by no
zeros and the
representation of
each digit
ends in one.
1 {0, 1}
because the digit 1 is represented by one
zero and the representation of each digit
ends in one.
-1 {-1, 0, 1}
because the encoding of a negative
number begins with a -1 followed by the
encoding of the absolute value of the
number.
100001 {0, 1, 1, 1, 1, 1, 0, 1}
because the encoding of the first 1 is 0,
1, the encoding of each of the four 0s is
just a 1 and the encoding of the last 1 is
0, 1.
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
because each 9 digit is encoded as
0,0,0,0,0,0,0,0,0,1.
{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
a is function 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.
because {1, 3} is not a sequence
{1, 1, 3} 0 from 1 to n for any
n.
because it contains more than one
{1, 2, 1, 2, 1, 2} 0 sequence from 1
to 2.
because it is "backwards", i.e., the
sequences from 1
{1, 2, 3, 1, 2, 1} 0
to n are not ordered by increasing
value of n
{1, 1, 2, 3} 0 because the sequence {1, 2} is missing (it
should
precede {1, 2, 3})
16. 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
if n is function 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
27. 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
then
If seconds
function reason
is
returns
because 3735 = 1*3600 + 2*60 +15. In
other words, 3,735 is the number of
3735 {1, 2, 15}
seconds in 1 hour 2 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
{1, 3} 1
There is one 1 and zero 2s, hence the number of 1s is
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.
1}
Also, no two adjacent
elements have the same value.