Answered step by step
Verified Expert Solution
Question
1 Approved Answer
In Java Suppose you are given an array of h integers, and you need to Tind all palrs oT Vaiues in the array (if any)
In Java
Suppose you are given an array of h integers, and you need to Tind all palrs oT Vaiues in the array (if any) that sum to a given integer k. In a class named PairFinder, write code that performs this task for you and outputs all of the pairs that it finds. For example, if k is 12 and the array is [10, 4, 7, 7, 8, 5, 151, your code should output something like the following 4+8=12 7+5=12 7+5=12 Note that we get two 75 sums because 7 appears twice in the array. However, while the methods that you write may print a given pair of values more than once in such cases, it is not necessary to do so. In addition, the order in which the sums (and the terms within each sum) are printed does not matter. If no pairs are found, the methods do not need to print anything. 1. Implement a static method named findPairSums that requires O(n2) steps to solve this problem. The method should have the following header: public static void findPairSums(int k, int[] arr) In addition, you should add test code for it to a main method. You may find it helpful to call the randomArrayO method from our SortCount class to generate test arrays, although it also makes sense to use literal arrays that you define. 2. Implement a static method named findPairSumsFaster) that takes the same parameters as findPairSums, but that requires only O(nlogn) steps in the average case to solve this problem. (Hint: you should begin by sorting the array using one of the methods from our Sort class. Once you have done so, only O(n) additional steps are needed to find the pairs.) Here again, you should add test code to the main methodStep 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