Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

USA MSc in computer science entrance exam, Exams of Computer Science

university of maharase enterance exam

Typology: Exams

2020/2021
On special offer
30 Points
Discount

Limited-time offer


Uploaded on 05/11/2021

muluneh-abrham
muluneh-abrham 🇺🇸

4

(1)

4 documents

1 / 53

Toggle sidebar
Discount

On special offer

Related documents


Partial preview of the text

Download USA MSc in computer science entrance exam and more Exams Computer Science in PDF only on Docsity! Sample 1 questions answer for the MUM entrance exam After applying in the Maharishi University of Management (MUM) for Master of Science in Computer Science, we get student id number provided by the University and we have to appear to the entrance exam, and in the entrance exam you will have three questions to be solved within 2 hours. I have collected some of the question and try to provide you the answers. Some of the sample questions that i am able to collect for the MUM entrance exam are as follows: 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 is and end is return reason 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 return reason {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, -2, - 1} 1 0 = 0 + 0, 0 = 0 + 0 + 0, 0 = 0 + 0 + 0 + 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 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. 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 Sample 2 question collections of MUM university MS computer science 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: if n is return reason 4 1 because 4 = 2*2 25 1 because 25 = 5*5 -4 0 because there is no integer that when squared equals -4. (note, -2 squared is 4 not - 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 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 -2147483648} 1 Again only three different values. Note that the value of a[0] is the maximum integer and the value of a[3] is the minimum integer so you can’t use those to initialize local variables. 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 first 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 4 624 because the first four multiples of 624 are 624, 1248, 1872, 2496 and they all contain the digit 2. Furthermore 624 is the smallest number whose first four multiples contain the digit 2. 5 624 because the first five multiples of 624 are 624, 1248, 1872, 2496, 3120. Note that 624 is also 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 7 4062 because the first five multiples of 4062 are 4062, 8124, 12186, 16248, 20310, 24372, 28434. 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 original 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]) the most significant digit: 2: 1001, 3: 100, 4: 21, 5: 14, 6: 13, 7: 12, 8: 11, 9: 10 360 0 All its representations have a 0 somewhere after the most significant digit: 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 a is then function returns reason {2, 2, 1} 1 because there are two 2s and one 1 and equal values appear in consecutive locations. {2, 2, 1, 2, 2} 0 Because there are four 2s (doesn’t matter that they are in groups of 2) {4, 4, 4, 4, 1, 2, 2, 3, 3, 3} 1 because 4 occurs four times, 3 appears three times, 2 appears two times and 1 appears once and equal values are in consecutive locations. {7, 7, 7, 7, 7, 7, 7, 1} 1 because 7 occurs seven times and 1 occurs once. {7, 7, 7, 7, 1, 7, 7, 7} 0 because the 7s are not in consecutive locations. {7, 7, 7, 7, 7, 7, 7} 1 because 7 occurs seven times {} 1 because there is no value that appears the wrong 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, -3} 0 because not all values are positive {0, 2, 2} 0 because 0 occurs one time, not zero times. {2, 1, 2} 0 because the 2s are not in consec 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 array is isOddHeavy should return {1} 1 (true vacuously) {2} 0 (contains no odd elements) {1, 1, 1, 1, 1, 1} 1 {2, 4, 6, 8, 11} 1 (11, the only odd-valued element is greater than all even-valued elements. {-2, -4, -6, -8, - 11} 0 (-8, an even-valued element is greater 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: if n is and p is return Because 27 3 3 33 divides 27 evenly but 34 does not. 28 3 0 30 divides 28 evenly but 31 does not. 280 7 1 71 divides 280 evenly but 72 does not. -250 5 3 53 divides -250 evenly but 54 does not. 18 1 -1 if p <=1 the function returns -1. 128 4 3 43 divides 128 evenly but 44 does 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 a is then function returns reason {1, 2, 1} 1 because the same number of 1s are at the beginning and end of the array and there is at least one 2 in between them. {1, 1, 2, 2, 2, 1, 1} 1 because the same number of 1s are at the beginning and end of the array and there is at least one 2 in between them. {1, 1, 2, 2, 2, 1, 1, 1} 0 Because the number of 1s at the end does not equal the 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. 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 if a is and m is and n is return reason {1, 2, 3, 4, 5} 1 5 1 because the array contains all the numbers between 1 and 5 inclusive in 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 {5, 4, 3, 2, 1} 1 5 0 because the array does not start with a 1. Furthermore, it is not in 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) 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 {1, 2, 3, 4, 5} 2 {2, 2, 4, 4, 6} 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 2-based rounding of 4 is 4, and the 2-based rounding of 5 is 6. {1, 2, 3, 4, 5} 3 {0, 3, 3, 3, 6} because the 3-based rounding of 1 is 0, the 3- based 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 {-18, 1, 2, 3, 4, 5} 4 {-18, 0, 4, 4, 4, 4} because -18 is negative and hence is not modified, 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: if n is return because 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. 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 a is then function returns reason {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. {111, 115, 118, 127, 125} 4392 because abs(111-115)=4, abs(115-118)=3, abs(118- 127)=9, 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 Sample 3 MUM entrance exam solutions 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 a is then function returns reason {0, 0, 0, 0, 0}1 1 because there is one sequence of 0s and its length >= 4. {1, 2, 0, 0, 0, 0, 2, -18, 0, 0, 0, 0, 0, 12}1 2 because there are two sequences of 0s and both have lengths >= 4. {0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0}1 3 because three are three sequences of zeros and all have length >=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 a is then function returns reason {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 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 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. 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 a is then function returns reason {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. {1, 2, 3, 1, 2, 1} 0 because it is “backwards”, i.e., the sequences from 1 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}) 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; {‘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 if a is then function returns reason {1, 1, 1, 2, 3, -18, 45, 1} 1 because it begins with three 1s, followed by four non-1s and ends with one 1 and 3+1 == 4 {1, 1, 1, 2, 3, -18, 45, 1, 0} 0 because the 0 starts another sequence of non-1s. There can be only one sequence of non-1s. {1, 1, 2, 3, 1, -18, 26, 1} 0 because there are two sequences of non-1s ({2, 3} and {-18, 26} {} 1 because 0 (# of beginning 1s) + 0 (# of ending 1s) = 0 (# of non-1s) {3, 4, 1, 1} 1 because 0 (# of beginning 1s) + 2 (# of ending 1s) = 2 (# of non-1s) {1, 1, 3, 4} 1 because 2 (# of beginning 1s) + 0 (# of ending 1s) = 2 (# of non-1s) {3, 3, 3, 3, 3, 3} 0 because 0 (# of beginning 1s) + 0 (# of ending 1s) != 6 (# of non-1s) {1, 1, 1, 1, 1, 1} 0 because 6 (# of beginning 1s) + 0 (# of ending 1s) != 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 ni = 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 a is then function returns reason {1, 2, 10, 3, 15, 1, 2, 2} 0 because 3 occurs after a 1 (a[0]) and 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. {3, 2, 18, 1, 0, 3, -11, 1, 3} 0 because a[5] is a 3 and is between a[3] and a[7] which are both 1s. {2, 3, 1, 1, 18} 1 because there are no instances of a 3 that is between two 1s {8, 2, 1, 1, 18, 3, 5} 1 because there are no instances of a 3 that is between two 1s {3, 3, 3, 3, 3, 3} 1 because there are no instances of a 3 that is between two 1s {1} 1 because there are no instances of a 3 that is between two 1s {} 1 because there are no instances of a 3 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 {1, 3, 2} 0 There is one 1 and one 2, hence the number of 1s is not greater than the number of 2s. {1, 3, 2, 2, 1, 5, 1, 5} 0 There are two 2s adjacent to each other. {1, 2, -18, -18, 1, 2} 0 The two -18s are adjacent to one another. Note that the number of 1s is not greater than than the number of 2s but your method should return 0 before determining that! (See the additional requirements above.) {} 0 There are zero 1s and zero 2s hence the number of 1s is not greater than the number of 2s. {1} 1 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 element, there cannot be adjacent elements with the same value. {2} 0 There are zero 1s and one 2 hence the number of 1s is not 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 {1, 4, 1, 4, 5, 6} 5 1 because a[2] + a[3] == 5 and 2+3==5. In other words, the sum of the values is equal to the sum of 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 {1, 4, 1} 5 0 because although a[0] + a[1] == 5, 0+1 != 5; and although a[1]+a[2]==5, 1+2 != 5 {8, 8, 8, 8, 7, 7, 7} 15 0 because there are several ways to get the values to 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;
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved