Spread the word.

Share the link on social media.

Share
  • Facebook
Have an account? Sign In Now

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

Sorry, you do not have permission to ask a question, 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

SIKSHAPATH Latest Questions

abhinaw_9754
  • 1
  • 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
  • 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
      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
      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

    store ads

    Stats

    • Questions 1k
    • Answers 1k
    • Posts 150
    • Best Answers 89
    • Cloud Computing Notes 101: Learn Fundamentals, Service Models, and Virtualization!
    • This Free AI Tool Translates Entire Books in Minute !
    • AI News: 🎬 Hollywood’s AI Studios, 🎓 OpenAI’s Latest Gift to Educators, 🚚 Class8 Bags $22M, 🧠 Google Gemini’s Memory Upgrade
    • AI NEWS: Legal Action Against OpenAI, $16M Paid, & Elon Musk’s Praise from Investor 🤖💰📑 | AI Boosts Cloud Seeding for Water Security 🌱💧
    • AI News: 🎬AI Video Tool Scam Exposed🤯, 🛰️ AI-Powered Drones to Ukraine 😱, Google’s $20M AI Push, Sam Altman Joins SF’s Leadership Team

    Explore

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

    Footer

    SIKSHAPATH

    Helpful Links

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

    Follow Us

    © 2021-24 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.