You may assume that each input would have exactly one solution, and you may not use the same element twice.
You can return the answer in any order.
Example 1:
Example 1
Input: nums = [2,7,11,15], target = 26
Output: [2,3]
Output: Because nums[2] + nums[3] == 26, we return [2, 3].
Example 2:
Example 2
Input: nums = [3,2,4], target = 6
Output: [1,2]
Example 3:
Example 3
Input: nums = [3,3], target = 6
Output: [0,1]
Method 1 : Brute force(Loop)
JAVApackage bracecoder;
import java.util.Scanner;
public class TwoSum {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan=new Scanner(System.in);
System.out.println("Enter number of count: ");
int n=scan.nextInt();
int[] nums=new int[n];
System.out.println("Enter the number one by one: ");
for(int i=0;i
OUTPUT
Method 2 : Hashmap
JAVA
package bracecoder;
import java.util.HashMap;
import java.util.Scanner;
public class TwoSum2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan=new Scanner(System.in);
System.out.println("Enter number of count: ");
int n=scan.nextInt();
int[] nums=new int[n];
System.out.println("Enter the number one by one: ");
for(int i=0;i map=new HashMap();
for(int i=0;i
OUTPUT
Comments (0)
Please sign in to leave a comment.
No comments yet. Be the first to share your thoughts!