#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Jan 12 18:15:26 2022

@author: shivengoyal
"""

"""
1. Two Sum
    Easy


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 1:

Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
Example 2:

Input: nums = [3,2,4], target = 6
Output: [1,2]
Example 3:

Input: nums = [3,3], target = 6
Output: [0,1]
 

Constraints:

2 <= nums.length <= 104
-109 <= nums[i] <= 109
-109 <= target <= 109
Only one valid answer exists.
 

Follow-up: Can you come up with an algorithm that is less than O(n2) time complexity?
"""



# SOLUTION 

def twoSum(nums, target):
        
    #SOLUTION 1 - BRUTE FORCE
    for i in range(0, len(nums)):
        for j in range(i+1, len(nums)):
            if(nums[i] + nums[j] == target):
                indices = [i,j]
    return indices
        
    #Solution 2 - Using Dictionary.  
        
    d = {}
        
    for i in range(0, len(nums)):
        d[i] = target - nums[i]   # 
            
    print("Dictionary is ",d)
        
    for key,value in d.items():
        if value in nums and nums.index(value)!= key:   
            print("Indices Are", key, nums.index(value))
            return key,nums.index(value)
            

User_Target = input("Enter Target: ")
User_Array =  input("Enter Array seperated by ,:  ")
u=User_Array.split(',')
arr=[]
for i in u:
    arr.append(int(i))

print(twoSum(arr, User_Target))

eee39d3ae586c379fcd10ce3b2f0c84409001938
<mask token>

<mask token>


def twoSum(nums, target):
    for i in range(0, len(nums)):
        for j in range(i + 1, len(nums)):
            if nums[i] + nums[j] == target:
                indices = [i, j]
    return indices
    d = {}
    for i in range(0, len(nums)):
        d[i] = target - nums[i]
    print('Dictionary is ', d)
    for key, value in d.items():
        if value in nums and nums.index(value) != key:
            print('Indices Are', key, nums.index(value))
            return key, nums.index(value)


<mask token>

<mask token>


def twoSum(nums, target):
    for i in range(0, len(nums)):
        for j in range(i + 1, len(nums)):
            if nums[i] + nums[j] == target:
                indices = [i, j]
    return indices
    d = {}
    for i in range(0, len(nums)):
        d[i] = target - nums[i]
    print('Dictionary is ', d)
    for key, value in d.items():
        if value in nums and nums.index(value) != key:
            print('Indices Are', key, nums.index(value))
            return key, nums.index(value)


<mask token>
for i in u:
    arr.append(int(i))
print(twoSum(arr, User_Target))

<mask token>


def twoSum(nums, target):
    for i in range(0, len(nums)):
        for j in range(i + 1, len(nums)):
            if nums[i] + nums[j] == target:
                indices = [i, j]
    return indices
    d = {}
    for i in range(0, len(nums)):
        d[i] = target - nums[i]
    print('Dictionary is ', d)
    for key, value in d.items():
        if value in nums and nums.index(value) != key:
            print('Indices Are', key, nums.index(value))
            return key, nums.index(value)


User_Target = input('Enter Target: ')
User_Array = input('Enter Array seperated by ,:  ')
u = User_Array.split(',')
arr = []
for i in u:
    arr.append(int(i))
print(twoSum(arr, User_Target))

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Jan 12 18:15:26 2022

@author: shivengoyal
"""

"""
1. Two Sum
    Easy


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 1:

Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
Example 2:

Input: nums = [3,2,4], target = 6
Output: [1,2]
Example 3:

Input: nums = [3,3], target = 6
Output: [0,1]
 

Constraints:

2 <= nums.length <= 104
-109 <= nums[i] <= 109
-109 <= target <= 109
Only one valid answer exists.
 

Follow-up: Can you come up with an algorithm that is less than O(n2) time complexity?
"""



# SOLUTION 

def twoSum(nums, target):
        
    #SOLUTION 1 - BRUTE FORCE
    for i in range(0, len(nums)):
        for j in range(i+1, len(nums)):
            if(nums[i] + nums[j] == target):
                indices = [i,j]
    return indices
        
    #Solution 2 - Using Dictionary.  
        
    d = {}
        
    for i in range(0, len(nums)):
        d[i] = target - nums[i]   # 
            
    print("Dictionary is ",d)
        
    for key,value in d.items():
        if value in nums and nums.index(value)!= key:   
            print("Indices Are", key, nums.index(value))
            return key,nums.index(value)
            

User_Target = input("Enter Target: ")
User_Array =  input("Enter Array seperated by ,:  ")
u=User_Array.split(',')
arr=[]
for i in u:
    arr.append(int(i))

print(twoSum(arr, User_Target))
