Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(JAVA) Given an array of unique positive integers, write a function findSums that takes the array input and: 1. Creates a hashtable called hashT and

(JAVA) Given an array of unique positive integers, write a function findSums that takes the array input and:

1. Creates a hashtable called hashT and inserts all the elements of the input array in the hashtable.

2. Finds pairs of elements in the hashtable whose sum is another element (sum) in the hashtable and print the pairs in the console.

3. Returns another hashtable names sums of those sum elements. For example:

Input: [1,5,4,6,7,9]

Output: [6,5,7,9]

Explanation: 6 = 1 + 5 ; 5 = 1 + 4 ; 7 = 1 + 6 ; 9 = 5 + 4

This is what I have so far

import java.util.HashSet;

public class FindSum {

public static void main(String[] args) { // TODO Auto-generated method stub int[]arr = {1, 5, 4, 6, 7, 9}; HashSet res = findSums(arr); System.out.println(res.toString()); //Should return [6, 5, 7, 9] } @SuppressWarnings("unlikely-arg-type") public static HashSet findSums(int[] arr){ HashSet sums = new HashSet(); HashSet hashtable = new HashSet(); for(int i = 0; i < arr.length; i++ ) { sums.add(arr[i]); hashtable.add(arr[i]); if(hashtable.contains(sums)) { sums = sums - arr[i]; } } return sums; }

}

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

Oracle PL/SQL Programming Database Management Systems

Authors: Steven Feuerstein

1st Edition

978-1565921429

More Books

Students also viewed these Databases questions