CSE220 Lab 01 - Arrays (Fall 2022)
CSE220 Lab 01 - Arrays (Fall 2022)
Linear Arrays
NOTE: Each method carries 5 marks
7. Splitting an Array
Suppose the elements of an array A containing positive integers, denote the weights in
kilograms. And we have a beam balance. We want to put the weights on both pans of
the balance in such a way that for some index 0 < i < A.length - 1, all values starting
from A[0], A[1], upto A[ i - 1], should be on the left pan. And all values starting from A[ i ]
upto A[ A.length - 1] should be on the right pan and the left and right pan should be
balanced. If such an i exists, return true . Else, return false.
8. Array series
Write a method that takes an integer value n as a parameter. Inside the method, you
should create an array of length n squared (n*n) and fill the array with the following
pattern. Return the array at the end and print it.
If,
n=2: { 0,1, 2,1 } (spaces have been added to show two distinct groups).
groups).
n=4 : {0, 0, 0, 1, 0, 0, 2, 1, 0, 3, 2, 1, 4, 3, 2, 1} (spaces have been added to show
four distinct groups).
Explanation: There are two bunches here {2,2} and {4,4,4}. The largest bunch is {4,4,4}
containing 3 elements so 3 is returned.
Explanation: There are three bunches here {1,1} and {2,2} and {1,1,1,1}. The largest
bunch is {1,1,1,1} containing 4 elements so 4 is returned.
10. Repetition
Write a method that takes in an array as a parameter and counts the repetition of each
element. That is, if an element has appeared in the array more than once, then its
‘repetition’ is its number of occurrences. The method returns true if there are at least
two elements with the same number of ‘repetition’. Otherwise, return false.
Explanation: Two numbers repeat in this array: 4 and 6. 4 has a repetition of 3, 6 has a
repetition of 3. Since two numbers have the same repetition output is True.
Explanation: Three numbers repeat in this array:3,4 and 6 .3 has a repetition of 2, 4 has
a repetition of 3, 6 has a repetition of 4. Since no two numbers have the same repetition
output is False.