Multiple Q

Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

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.

4) 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)

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

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

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

8) A twin 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.

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

10) 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)

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

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

13) 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)

14) 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.
15) 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.

16) 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)

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

18) 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.
[Lien] Concern: done

19) 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)

[Lien] ok
20) 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.

[Lien] done: isBean9_13_7_16


21) 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.
==Continue

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

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

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

25) 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)

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

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

28) 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.
=Continue: Afternoon

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

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

31) 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)

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

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

34) 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)

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

36) 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.
37) 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)

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

39) 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