Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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 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) 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