In: Computer Science
Please follow the instructions carefully and complete the code given below.
Language: Java
Instructions from your teacher:
(This is a version of an interview question I once saw.) In this problem, we will write a program that, given an integer k, an integer n, 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. Hint: you can assume that the contains in a HashSet is O(1). Your program should take as input a number k on its own line, followed by a number n on its own line, followed by a space-separated list of n 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
6
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:
3
4
1 2 3 4
then we should print 1since there is one pair that sums to 1: 1+2.
The problem PairFinder provides starter code for reading and printing; your task is to fill in thefindPairs()method.
.......................................................................................................................
import java.util.Scanner;
public class Main {
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 value of n
int n = Integer.parseInt(sc.nextLine());
// Read in the list of numbers
int[] numbers = new int[n];
String input = sc.nextLine();
if (input.equals("")) {
numbers = new int[0];
} else {
String[] numberStrings = input.split(" ");
for (int i = 0; i < n; i++) {
numbers[i] = Integer.parseInt(numberStrings[i]);
}
}
System.out.println(findPairs(numbers, k));
}
private static int findPairs(int[] numbers, int k) {
// TODO fill in this function
throw new UnsupportedOperationException();
}
}
Thanks for the question. Below is the code you will be needing Let me know if you have any doubts or if you need anything to change. Thank You !! =========================================================================== import java.util.Arrays; import java.util.Scanner; public class Main { 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 value of n int n = Integer.parseInt(sc.nextLine()); // Read in the list of numbers int[] numbers = new int[n]; String input = sc.nextLine(); if (input.equals("")) { numbers = new int[0]; } else { String[] numberStrings = input.split(" "); for (int i = 0; i < n; i++) { numbers[i] = Integer.parseInt(numberStrings[i]); } } System.out.println("Pairs found: "+findPairs(numbers, k)); } private static int findPairs(int[] numbers, int k) { Arrays.sort(numbers); int start = 0; int end = numbers.length - 1; int count = 0; while (start <= end) { int total = numbers[start] + numbers[end]; if (total == k) { count++; start++; end--; } else if (total > k) end--; else start++; } return count; } }
============================================================