Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

write a JAVA program that reads an array, and then checks to see how many distinct pairs of numbers in the array add up to

write a JAVA program that reads an array, and then checks to see how many distinct pairs of numbers in the array add up to a given value.

image text in transcribedimage text in transcribed

In this lab you will write a program that reads an array and then checks to see how many distinct pairs of numbers in the array add up to a given value. Your program will do the following: 1. Read in integer that indicates the size of the array. I'll call that number N for this write-up. 2. Allocate an array of integers of size N. 3. Read Nintegers into the array 4. Read another integer, which I'll call target. 5. Search the array for pairs of numbers whose sum is target, and keep a count of how many such pairs there are. 6. Print out the count of pairs. Step 5 is the hard part. For example, given this data: 10 1 5 -3 -8 1-7 4 9 4 -9 0 We see that N = 10, and the array will be { 1, 5, -3, -8, 1, -7, 4, 9, 4, -9 }. The target value we'll looking for is. The output for that example will be since there is example one pair of distinct numbers in the array (9 and -9) which add up to 0. On the other hand, if the input were this: 10 1 5 -3 -8 1 - 7 4 9 4 -9 the output would be 3 since there are three pairs of numbers which add up to 6 (1 and 5, 9 and 3, 5 and 1). As a final example, with these same numbers, if target is -14, the output is 0 (If your program gives a result of 1 for this test, you are looking for distinct pairs of numbers (-7 + 7 = -14, but there is only one-7 in the array) The solution I'm looking for uses a pair of nested for loops. We'll see later on that there are even faster ways to do this. But for now, stick to the nested for loops. Hints: 1. For step 5, you want to check every number against every other number. A pair of nested for loops may be useful here. 2. Remember not to check a number against itself! For example, if target value is 10, and there is one 5 in the array, then 5+5 does not count. 3. The naive approach will give you twice the number of matches as you would expect. If this happens to you, you may fix it by just dividing your answer by 2, but if you can look for a way to avoid that. 1 import java.util.Scanner; 2 3 public class TwoSum { 4 public static void main(String[] args) { nmt in DOO 5 6 7 8 Scanner scnr = new Scanner(System.in); // Your code goes here. Read the instructions carefully! // And don't forget to read the hints at the end. scnr.close(); 10 11 12 13 14 15 } 16 }

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

More Books

Students also viewed these Databases questions

Question

Explain how to build high-performance service delivery teams.

Answered: 1 week ago

Question

Understand what a service-oriented culture is.

Answered: 1 week ago