Question: Design algorithms for the following two tasks and code them in Java: 1 . Write a Java method called fillArray that accepts a positive integer

Design algorithms for the following two tasks and code them in Java:
1. Write a Java method called fillArray that accepts a positive integer n as its only parameter. The method returns an array of length \( n-1\). This array is filled with unique integers in the range \(0..\mathrm{n}-1\). In other words, the array contains all but one of the numbers in that range. The numbers are not sorted; they are in random order.
2. Write a second Java method called findMissingNumber that accepts as its only parameter an array of the type produced by fillArray. Find and return the number in the range \(0..\mathrm{n}-1\) that is missing from the array.
For both methods:
- The algorithm must be \(\mathrm{O}(\mathrm{n})\) for time complexity; and
- You are only allowed to use O(1) additional space besides the array itself. In other words, you cannot use a second array.
Include in your class a main method that tests both methods.
Include the following clearly labeled information in your submission document for Question 3:
a) The code for your complete Java class, including your main method, fillArray, and findMissingNumber.
b) Output for multiple test cases for both \(\mathrm{n}=10\) and \(\mathrm{n}=20\). For each test case, display (and clearly label):
- the contents of the array produced by fillArray (display all numbers on the same line); and
- the missing number as returned by findMissingNumber.
c) An explanation of why your fillArray method is \(\mathrm{O}(\mathrm{n})\) for time complexity
d) An explanation of why your fillArray method is \(\mathrm{O}(1)\) for space usage, other than the array itself
e) An explanation of why your findMissingNumber method is \(\mathrm{O}(\mathrm{n})\) for time complexity
f) An explanation of why your findMissingNumber method is \(\mathrm{O}(1)\) for space usage, other than the array itself
Design algorithms for the following two tasks and

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!