Answered step by step
Verified Expert Solution
Link Copied!

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

image text in transcribed

image text in transcribed

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, make sure there are no errors, post full code including starter code

(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: 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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases And Information Systems 22nd European Conference Adbis 2018 Budapest Hungary September 2 5 2018 Proceedings Lncs 11019

Authors: Andras Benczur ,Bernhard Thalheim ,Tomas Horvath

1st Edition

3319983970, 978-3319983974

More Books

Students also viewed these Databases questions