Question: Starter Code: import java.util.Scanner; public class PairFinder { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // Read in the value of

 Starter Code: import java.util.Scanner; public class PairFinder { public static void

main(String[] args) { Scanner sc = new Scanner(System.in); // Read in the

Starter Code:

import java.util.Scanner;

public class PairFinder { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // Read in the value of k int k = Integer.parseInt(sc.nextLine()); // Read in the list of numbers int[] numbers; String input = sc.nextLine(); if (input.equals("")) { numbers = new int[0]; } else { String[] numberStrings = input.split(" "); numbers = new int[numberStrings.length]; for (int i = 0; i

Code in Java, try to include some comments

(20 points) (This is a version of an interview question I once saw.) In this problem, we will write a program that, given an integer k and a list of integers, outputs the number of pairs in in the list that add to k. To receive full credit for design, your algorithm must have a runtime in O(n), where n is the length of the list of integers. Your program should take as input a number k on its own line, followed by a space-separated list of integers. It should then output the number of pairs in the list that add to k. Order does not matter when considering a pair (in other words, in the list [2,1] there is one distinct pair that sums to 3, not two). You may assume that all numbers in the input are distinct. For example, if our file input.txt is 1 2 3 4 -2 -3 then we should print: 2 since there are two pairs that sum to 1: 3+(-2) and 4-+(-3). As another example, if input.txt is 1 2 3 4 then we should print: since there is one pair that sums to 1: 1+2. The class PairFinder provides starter code for reading and printing; your task is to fill in the findPairs) method

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 Databases Questions!