Sign Up

Have an account? Sign In Now

Sign In

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here
Sign InSign Up

SIKSHAPATH

SIKSHAPATH Navigation

  • Home
  • Questions
  • Blog
    • Computer Science(CSE)
    • NPTEL
    • Startup
  • Shop
    • Internshala Answers
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Questions
  • Blog
    • Computer Science(CSE)
    • NPTEL
    • Startup
  • Shop
    • Internshala Answers
Home/Questions/Q 4325
Next
In Process
abhinaw_9754
  • 1
abhinaw_9754
Asked: November 14, 20212021-11-14T20:13:13+05:30 2021-11-14T20:13:13+05:30In: Other

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.

  • 1
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.

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

Input: nums = [2,7,11,15], target = 9

Output: [0,1]

Output: Because nums[0] + nums[1] == 9, we return [0, 1].

Constraints:

2 <= nums.length <= 104

-109 <= nums[i] <= 109

-109 <= target <= 109

Only one valid answer exists.

given an array of integers nums and an integer targetreturn indices of the two numbers such that they add up to target.two sum problem
  • 2 2 Answers
  • 3k Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook

    2 Answers

    • Voted
    • Oldest
    • Recent
    1. Professor
      2021-11-14T20:54:45+05:30Added an answer on November 14, 2021 at 8:54 pm
      
      //YOU CAN ALSO USE FOLLOWING CODE:
      
      import java.util.HashMap;
      import java.util.Scanner;
      import java.util.Map;
      
      class TwoSum {
          // Time complexity: O(n)
              private static int[] findTwoSum(int[] nums, int target) {
                      Map numMap = new HashMap();
                              for (int i = 0; i < nums.length; i++) {
                                          int complement = target - nums[i];
                                                      if (numMap.containsKey(complement)) {
                                                                      return new int[] { numMap.get(complement), i };
                                                                                  } else {
                                                                                                  numMap.put(nums[i], i);
                                                                                                              }
                                                                                                                      }
                                                                                                                              return new int[] {};
                                                                                                                                  }
                                                                                                                                  }
      
      
      
      
      
      • 2
      • Reply
      • Share
        Share
        • Share on WhatsApp
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
    2. Professor
      2021-11-14T20:52:23+05:30Added an answer on November 14, 2021 at 8:52 pm
      
      
      import java.util.HashMap;
      import java.util.Scanner;
      import java.util.Map;
      
      class TwoSum {
      
          
              private static int[] findTwoSum_BruteForce(int[] nums, int target) {
                      for (int i = 0; i < nums.length; i++) {
                                  for (int j = i + 1; j < nums.length; j++) {
                                                  if (nums[i] + nums[j] == target) {
                                                                      return new int[] { i, j };
                                                                                      }
                                                                                                  }
                                                                                                          }
                                                                                                                  return new int[] {};
                                                                                                                      }
      
          public static void main(String[] args) {
                  Scanner keyboard = new Scanner(System.in);
              int n = keyboard.nextInt();
                      int[] nums = new int[n];
              for(int i = 0; i < n; i++) {
                          nums[i] = keyboard.nextInt();
                                  }
                                          int target = keyboard.nextInt();
              keyboard.close();
              int[] indices = findTwoSum_BruteForce(nums, target);
              if (indices.length == 2) {
                          System.out.println(indices[0] + " " + indices[1]);
                                  } else {
                                              System.out.println("No solution found!");
                                                      }
                                                          }
                                                          }
      
      
      
      
      • 0
      • Reply
      • Share
        Share
        • Share on WhatsApp
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn

    Leave an answer
    Cancel reply

    You must login to add an answer.

    Forgot Password?

    Need An Account, Sign Up Here

    Sidebar

    Stats

    • Questions 1k
    • Answers 1k
    • Posts 138
    • Best Answers 65
    • NPTEL Python for Data Science Assignment 1 Answers 2023
    • NPTEL Solution: Cloud Computing Assignment 2 Answers 2023
    • Solution Revealed: NPTEL Programming in Java Week 1 Assignment 2023
    • NPTEL Joy of Computing Using Python Assignment Answers Week 1 2023
    • NPTEL Cloud Computing Assignment 1 Answers 2023

    Popular Questions

    • I'M ADMIN

      Internshala ethical hacking final test answers: Question sequence differs but ...

      • 6 Answers
    • AK

      You have a green lottery ticket, with ints a, b, ...

      • 4 Answers
    • mj

      Solve by Gauss Elimination Method 5x+y+z+w=4 ,x+7y+z+w=12 , x+y+6z+w=-5, x+y+z+4w=-6

      • 3 Answers

    Explore

    • Recent Questions
    • Questions For You
    • Answers With Time
    • Most Visited
    • New Questions
    • Recent Questions With Time

    Footer

    Helpful Links

    • Disclaimer
    • Privacy Policy Notice
    • TERMS OF USE
    • FAQs
    • Refund/Cancellation Policy
    • Delivery Policy for Sikshapath

    Follow

    © 2021-2022 Sikshapath. All Rights Reserved

    Insert/edit link

    Enter the destination URL

    Or link to existing content

      No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.