Algo-Tree icon indicating copy to clipboard operation
Algo-Tree copied to clipboard

Two sum

Open poojitha2002 opened this issue 3 years ago • 12 comments

` /*

Problem statement: Given an array of integers 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.

*/

import java.util.*; public class MyClass {

// This is the function which finds the two indices, whose values add upto given target
public static int[] twoSum(int[] nums, int target) 
{
   // Declaring an array to store the result (indices that add upto given target)
   int []a=new int[2];
   
   // We can use hashmap to store element as key and its index as value.
   // Declaring hashmap "hm"
   HashMap<Integer,Integer>hm=new HashMap<>();
   
   // Traverse the given array 
   
   for(int i=0;i<nums.length;i++)
    {
        // Now current number is nums[i] so we need to calculate a number when added to current number gives target or not.
        // So calculate target-nums[i] which is the other number when added to nums[i] gives target
        int other=target-nums[i];
        // Checking if other is present in our hashmap or not and note that it should not be the same as current number index.
        if(hm.containsKey(other) && hm.get(other)!=i)
          {
            // If other is present in hashmap then the two indices are i, hm.get(other). Store them in array and return that array.
            a[0]=i;
            a[1]=hm.get(other);
            return a;  
          }
        else
          {
            // If that number called "other" is not present in hashmap then add it to our hashmap
            hm.put(nums[i],i);
          }
    }
    // Return our final array
    return a;
}   
public static void main(String args[]) {
 Scanner s=new Scanner(System.in);
 int n=s.nextInt(); // Scanning number of elements 
 int []a=new int[n]; // Declaring an array to store the elements
 for(int i=0;i<n;i++)
 a[i]=s.nextInt();  // Scanning elements one by one
 int target=s.nextInt(); // Scanning target 
 int []result=twoSum(a,target); //Calling the function twoSum() and storing the result in an array named "array"
 for(int i=0;i<result.length;i++)
 System.out.print(result[i]+" "); // Printing the result
}

}

/*

Input 1: 5 2 4 5 1 7 8 Output: 4 3

Input 2: 4 2 7 11 15 9 Output: 1 0


Time Complexity : O(n) Space Complexity: O(n)

*/ `

poojitha2002 avatar Mar 08 '21 15:03 poojitha2002

I am a GSSOC'21 participant and I wish to work on this using C++. Hope to hear back from you . I will give some testcases with proper explanation too. Thank you .

machine-sg avatar Mar 09 '21 05:03 machine-sg

I am a GSSOC'21 participant and i want to solve target sum problem using java programming language. Can you please assign it to me?

Aditya7494 avatar Mar 09 '21 09:03 Aditya7494

Ok, Thank you.

On Fri, 12 Mar 2021 at 14:55, Tarun Yadav @.***> wrote:

Assigned #203 https://github.com/Algo-Phantoms/Algo-Tree/issues/203 to @machine-sg https://github.com/machine-sg.

— You are receiving this because you were assigned. Reply to this email directly, view it on GitHub https://github.com/Algo-Phantoms/Algo-Tree/issues/203#event-4449646405, or unsubscribe https://github.com/notifications/unsubscribe-auth/AOPOFNMLK5YMZG267SM2RALTDHM2FANCNFSM4YZSVIUQ .

machine-sg avatar Mar 13 '21 04:03 machine-sg

Thank you for assigning me.

Aditya7494 avatar Mar 13 '21 05:03 Aditya7494

@tarun26091999 I have sent the PR. Please accept it. Thank you.

machine-sg avatar Mar 13 '21 07:03 machine-sg

Hi ,

I am GSSOC'21 participant. Please assign me this. I will do this in C with proper comments and test cases.

@tarun26091999

Manish5237 avatar Mar 14 '21 05:03 Manish5237

Hi I'm a GSSoC'21 participant .I want to write code for this problem in Java. Please assign me this issue.

GarimaKhare15 avatar Mar 14 '21 18:03 GarimaKhare15

Hello! I am a GSSoC'21 Participant. I would like to work on this issue. Would be highly obliged if you could assign it to me. I will be using Java. Thanks!

khusheekapoor avatar Mar 20 '21 06:03 khusheekapoor

I would like to work on this issue in python. Kindly assign it to me @plazzy99 @tarun26091999 @rudrakshi99

shahina-bano avatar Mar 28 '21 15:03 shahina-bano

hello @tarun26091999, I am anshika dubey a gssoc21 participant please assign me this issue. I will be using c++ language

anshika272 avatar Mar 28 '21 16:03 anshika272

Sir I am a participant of GSSOC'21. I have not contributed in any of the projects till now. But I really want to contribute and I am a beginner and therefore I am in search of issue. So Sir I request you to assign this issue to me. It would really help me in boosting my confidence by contributing in one project of Open Source. Therefore Sir its my humble request to assign this issue to me. I would satisfy all the requirements that would be needed for this issue. I will be using JAVA language.

AdityaRaj-ar avatar Mar 29 '21 05:03 AdityaRaj-ar

@tarun26091999 .I'm a GSSOC'21 participant ,I would implement it using C++ , please assign this to me

nikhv avatar Apr 02 '21 07:04 nikhv