Answered step by step
Verified Expert Solution
Question
1 Approved Answer
in java Given an array of n distinct none zero values as well as a target value T, determine in O(n) time whether or not
in java
Given an array of n distinct none zero values as well as a target value T, determine in O(n) time whether or not there exist two distinct values in the array that sum to T. The given array maybe sorted or may not be already sorted. It will be specified in the input whether it is sorted or not and you have to fulfill specific requirement based on the sorted status. (For example, if the array contained 3, 5, 6, 7, and 9 and T = 14, then the method you are to write should return pair (5,9). since 5+9 - 14. It should return pair (0,0) if for the same array of values T - 17.) Input File Format (Your code must read from a file called in. Ixt as we will also pass in.txt file to your code while grading) The first line of the file will have a single positive integer k, representing the number of test cases in the file. The next 2*k lines will contain the test cases, with two lines being used for each test case. The first value on the first line of each test case will be sorted status (O means sorted, 1 means unsorted). The next number in the line is n, the size of the array. The rest of the line will contain n distinct none zero integers, each separated by spaces. The second line of each test case will contain a single integer, T, the target for the problem. Here's an example in.txt file: 4 1 5 3 5 6 79 14 0316 3 11 0 6 4 1 -9 6 45 10 16-91 4 6 10 45 16 16 Output Format For each test case, output a line with one of the following two formats: Test case #r: The target of X is achievable by ni and n2. Test case #m: The target of X 19 NOT achievable. where m (15m sk), represents the appropriate test case in the file. Example output for the above in.txt file Test case#1: Target 14 achievabie by 5 and 9 Test case#2: Target 11 18 NOT achievable. Test case#3: Target 16 achievable by 6 and 10 Test case #4: Target 16 achievable by 6 and 10 1. If you see that a particular test case is sorted (sorted status = 1), your code must process it with O(n) operation to receive more than 50% credit on this part. For this part of the implementation, you are not allowed to use Hashset. 2. For finding the pair in the sorted array, you must implement the following function: getCandidatePair(int A[], int target): This function receives an int array and the target and returns the pair of ints from the array that can addup to target. For this purpose, you may consider to have another class and return an object of that class. If a pair does not exist, the function should return the pair as (@, o). Note that you are not allowed to print any information in this function as part of the output. 3. If you see that a particular test case is not sorted (sorted status = 0), your code must process it with O(n) operation to receive more than 75% credit on this part. If your code works with O(n log n), you can get up to 75% on this part. Note that Arrays.sort() method sometimes can result in O(n^2). The Collections.sort() method could be a good idea to guarantee O(n log n) sorting. If you would like to get 100% credit for this part and get O(n) run-time, then Hashset would be the best option (Note that HashSet insert and lookup 0(1)). If you are unable to find a solution within even Inlogn), then at least solve it with O(n^2) to receive some partial creditStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started