Question

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

(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<Integer> res = findSums(arr);
       System.out.println(res.toString()); //Should return [6, 5, 7, 9]
   }
   @SuppressWarnings("unlikely-arg-type")
   public static HashSet<Integer> findSums(int[] arr){
       HashSet<Integer> sums = new HashSet<Integer>();
       HashSet<Integer> hashtable = new HashSet<Integer>();
       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;
   }

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.HashSet;

public class FindSum {

public static void main(String[] args) {
int ar[]={1,5,4,6,7,9};
HashSet<Integer> res=findSum(ar);
System.out.println(res.toString());
  

}
public static HashSet<Integer> findSum(int element[]){
HashSet<Integer> sums=new HashSet<Integer>();
HashSet<Integer> hashtable =new HashSet<Integer>();
for(int i=0;i<element.length;i++){
hashtable.add(element[i]); //add all element to hashtable
}
for(int i=0;i<element.length;i++){
for(int j=i;j<element.length;j++){ //it will calculate each pair
int sum=element[i]+element[j]; //sum of the that pair
if(hashtable.contains(sum)){ //if that sum present in hashtable add to to set
sums.add(sum);
}
}
}
return sums; //return set
}

}

output

[5, 6, 7, 9]

Add a comment
Know the answer?
Add Answer to:
(JAVA) Given an array of unique positive integers, write a function findSums that takes the array...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 2. Use hashing (solve_with_Hash(int[] array, int k)) Initialize a counter variable to O: Insert a...

    2. Use hashing (solve_with_Hash(int[] array, int k)) Initialize a counter variable to O: Insert all elements of array in a hashtable For every element in array: counter0 a. b. c. .Look for array[i] . Look for array [幻 + k in the hash map, if found then increment counter. -k in the hash map, if found then increment counter. Remove arrayli] from hash table. d. return counter For example: Input : array[] {1, 5, 3, 4, 2), k 3 Output:...

  • In Java Write a program that reads an arbitrary number of 25 integers that are positive...

    In Java Write a program that reads an arbitrary number of 25 integers that are positive and even. The program will ask the user to re-enter an integer if the user inputs a number that is odd or negative or zero. The inputted integers must then be stored in a two dimensional array of size 5 x 5. Please create 3 methods: 1. Write a method public static int sum2DArray( int [1] inputArray ) The method sums up all elements...

  • IN JAVA please Given a sorted array and a target value, return the index if the...

    IN JAVA please Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. Your code will be tested for runtime. Code which does not output a result in logarithmic time (making roughly log(2) N comparisons) will fail the tests. A sample main function is provided so that you may test your code on sample inputs. For testing purposes, the...

  • Problem Definition: Problem: Given an array of integers find all pairs of integers, a and b,...

    Problem Definition: Problem: Given an array of integers find all pairs of integers, a and b, where a – b is equal to a given number. For example, consider the following array and suppose we want to find all pairs of integers a and b where a – b = 3 A = [10, 4, 6, 16, 1, 6, 12, 13] Then your method should return the following pairs: 4, 1 15, 12 13, 10 A poor solution: There are...

  • JAVA: array return types May you please help me fix this coding. Sum is SUPPOSE to...

    JAVA: array return types May you please help me fix this coding. Sum is SUPPOSE to be 1253 import java.util.Scanner; public class Array { public static int sum(int[] arr) { int i; int total = 0; for ( i =0; i < arr.length; ++i) { total = total + arr[i]; } return total; } public static void main(String[] args) { int[] myArray = {45, 22, 18, 89, 82, 79, 15, 69, 100, 55, 48, 72, 16, 98, 57, 75, 44,...

  • Java 8 9m left Jav 27 28 ALL 29 0 Given an integer array, separate the...

    Java 8 9m left Jav 27 28 ALL 29 0 Given an integer array, separate the values of the array into two subsets, A and B, whose intersection is null and where the addition of the two subsets equals the entire array. The sum of values in set A must be strictly greater than the sum of values in set B, and the number of elements in set A must be minimal. Return the values in set A in increasing...

  • JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a...

    JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a parameter. This method calculates and returns the largest even number in the list. If there are no even numbers in the array, the method should return 0. You can assume that the array is not empty. For example: Test Result int[] values = {1, 4, 5, 9}; System.out.println(getMaxEven(values)); 4 System.out.println(getMaxEven(new int[]{1, 3, 5, 9})); 0 public static int --------------------------------------------------------------------------------- 2. Write a static method...

  • the code needs to be modifed. require a output for the code Java Program to Implement...

    the code needs to be modifed. require a output for the code Java Program to Implement Insertion Sort import java.util.Scanner; /Class InsertionSort * public class Insertion Sort { /Insertion Sort function */ public static void sort( int arr) int N- arr.length; int i, j, temp; for (i-1; i< N; i++) j-i temp arrli; while (j> 0 && temp < arrli-1) arrli]-arrli-1]; j-j-1; } arrlj] temp; /Main method * public static void main(String [] args) { Scanner scan new Scanner( System.in...

  • The following code is a Java code for insertion sort. I would like this code to...

    The following code is a Java code for insertion sort. I would like this code to be modified by not allowing more than 10 numbers of integer elements (if the user enters 11 or a higher number, an error should appear) and also finding the range of the elements after sorting. /* * Java Program to Implement Insertion Sort */ import java.util.Scanner; /* Class InsertionSort */ public class InsertionSortTwo { /* Insertion Sort function */ public static void sort( int...

  • JAVA Objectives: 1. Apply linear search algorithm 2. Apply select sort algorithm 3. Apply array iteration...

    JAVA Objectives: 1. Apply linear search algorithm 2. Apply select sort algorithm 3. Apply array iteration skill Problem description: Write the following eight methods and write a main function to test these methods // return the index of the first occurrence of key in arr // if key is not found in arra, return -1 public static int linearSearch(int arr[], int key) // sort the arr from least to largest by using select sort algorithm public stati void selectSort(int arr[])...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT