Question

In: Computer Science

In Java, a set of integers is given. write a function to find 2 integers in...

In Java, a set of integers is given. write a function to find 2 integers in this set that sums upto a target value.

i.e [1,5,2,0,11,3] target = 7

result [5,2]

i.e [1,5,4,0,14,-7] target = 9

result [5,4]

NOTE: THE SAME INTEGER CANNOT BE USED TWICE !!!

Solutions

Expert Solution

Code:

import java.lang.*;
import java.util.*;
public class sum{
   public static void main(String[]args){
       ArrayList<Integer> list=new ArrayList<Integer>();
       System.out.println("Enter length of the list");
       Scanner s= new Scanner(System.in);
       int i=s.nextInt();
       while(i!=list.size()){
           System.out.println("Enter a number: ");
           int input=s.nextInt();
           list.add(input);
       }
      
       System.out.println("Enter target number: ");
       int target=s.nextInt();//take input and store in target
       for (int a=0;a<list.size()-1;a++){
           for (int b=a+1;b<list.size();b++){
               if (list.get(a)+list.get(b)==target && list.get(a)!=list.get(b)){
                   System.out.println("result ["+list.get(a)+","+list.get(b)+"]");
               }
           }
       }
   }
}

Code in the image:

Output:


Related Solutions

Can you write code in Java to find the power set of a given set? For...
Can you write code in Java to find the power set of a given set? For example if S={a,b} the power set is P={{},{a},{b},{a,b}} ( you can also choose any of your favorite programming language). Explaning the code in comment please.
Write a Java function to swap two integers.
Write a Java function to swap two integers.
2. Answer the following question (a) Write a Java program to find the number of integers...
2. Answer the following question (a) Write a Java program to find the number of integers within the range of two specified numbers and that are divisible by another number. For example, x = 5, y=20 and p =3, find the number of integers within the range [x, y] and that are divisible by p. (b) Write explicitly on the following OOP concepts: i. Encapsulation ii. Inheritance iii. Polymorphism (c) Develop a Java application that computes the two roots of...
Write a program in Java that reads in a set of positive integers and outputs how...
Write a program in Java that reads in a set of positive integers and outputs how many times a particular number appears in the list. You may assume that the data set has at most 100 numbers and -999 marks the end of the input data. The numbers must be output in increasing order. For example, for the data 15 40 28 62 95 15 28 13 62 65 48 95 65 62 65 95 95 -999 The output is...
Write a Java program to read a set of integers from a file, dataX, and a...
Write a Java program to read a set of integers from a file, dataX, and a set of ranges from a second file, rangeX, and, for each range [a, b] in rangeX, report the SUM of all the integers in dataX which are in the range [a, b]. As the integers are read from file dataX, insert them in a binary search tree. After all the integers have been inserted into the binary search tree, read the ranges from file...
Write a program in java to determine whether a given function F from the set {0,1,2,3,...,m}...
Write a program in java to determine whether a given function F from the set {0,1,2,3,...,m} to (0,1,2,3,...,n} is one-to-one and/or onto. The inputs to your program will be: - an integer m - an integer n, and - a two dimensional boolean array F of size (m+1) x (n+1), where F[ i , j ] = 1 if F(i) = j.
Develop a C++ function to find the number of even and odd integers in a given...
Develop a C++ function to find the number of even and odd integers in a given array of integers Pass in an array of integers Pass in the size of the array of integers Pass in a reference parameter for the number of even values Pass in a reference parameter for the number of odd values The function doesn't return anything Develop a C++ program to test your function
For the following Ackerman function defined for non-negative integers, write a Java program that: Contains a...
For the following Ackerman function defined for non-negative integers, write a Java program that: Contains a method called computeAckermann that takes two integer values as parameters and calculates and returns the value of the Ackerman function. The method should be defined inside a class named Ackermann. Test the implementation of Ackerman in (a) by calling it from main function and printing the returned value. The main function should be defined in a separate class named AckermannDemo.
Write a C++ program to find the number of pairs of integers in a given array...
Write a C++ program to find the number of pairs of integers in a given array of integers whose sum is equal to a specified number.
Write a Java program to find the sum of all integers between 200 to 250 which...
Write a Java program to find the sum of all integers between 200 to 250 which are divisible by 7. Sample Output: Numbers between 200 and 250, divisible by 7: 203 210 217 224 231 238 245 The sum is: 1568
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT