Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions Challenge-1/two-sum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# 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

def twoSum(nums, target):
Sums={}
for i in range(len(nums)):
n=nums[i]
rem=target-n
if rem in Sums:
return [Sums[rem],i]
Sums[n]=i
return [-1,-1]

nums=list(map(int,input("Enter the numbers:").split()))
target=int(input("Enter the target:"))
print(twoSum(nums,target))
29 changes: 29 additions & 0 deletions Challenge-2/best-time-to-buy-and-sell-stock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# You are given an array prices where prices[i] is the price of a given stock on the ith day.
# You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock.
# Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.

# Example 1:
# Input: prices = [7,1,5,3,6,4]
# Output: 5
# Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
# Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell.

# Example 2:
# Input: prices = [7,6,4,3,1]
# Output: 0
# Explanation: In this case, no transactions are done and the max profit = 0.


# Constraints:
# 1 <= prices.length <= 105
# 0 <= prices[i] <= 104

prices=list(map(int,input("Enter the prices:").split()))
def maxProfit(prices):
minPrice = float('inf')
maxProfit=0
for i in prices:
minPrice=min(minPrice,i)
maxProfit=max(maxProfit,i-minPrice)
return maxProfit
print(maxProfit(prices))
46 changes: 46 additions & 0 deletions Challenge-3/contains-duplicates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.



# Example 1:

# Input: nums = [1,2,3,1]

# Output: true

# Explanation:

# The element 1 occurs at the indices 0 and 3.

# Example 2:

# Input: nums = [1,2,3,4]

# Output: false

# Explanation:

# All elements are distinct.

# Example 3:

# Input: nums = [1,1,1,3,3,4,3,2,4,2]

# Output: true



# Constraints:

# 1 <= nums.length <= 105
# -109 <= nums[i] <= 109

nums=list(map(int,input("Enter the numbers:").split()))
def containsDuplicate(nums):
Num={}
for num in nums:
if num in Num:
return True
Num[num]=Num.get(num, 0) + 1
return False
print(containsDuplicate(nums))
45 changes: 45 additions & 0 deletions Challenge-4/sum-two-numbers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Given two integers a and b, return the sum of the two integers without using the operators + and -.



# Example 1:

# Input: a = 1, b = 2
# Output: 3
# Example 2:

# Input: a = 2, b = 3
# Output: 5


# Constraints:

# -1000 <= a, b <= 1000


a,b=map(int,input("Enter the numbers:").split())
def getSum(a, b):
while b!=0:
carry=a&b
a=a^b
b=carry<<1
return a
print(getSum(a,b))

## a=2 b=3
## 0010
## 0011
## carry=0010
## a=0001
## b=0100
## carry=0000
## a=0101
## b=0000
## a=5
## b=0
## return 5





43 changes: 43 additions & 0 deletions Challenge-5/Product-of-array-except-itself.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
nums=list(map(int,input().split()))
# def productExceptSelf(nums):
# n=len(nums)
# answer=[1]*n
# for i in range(0,len(nums)):
# product=1
# for j in range(len(nums)-1,-1,-1):
# if i==j:
# continue
# product*=nums[j]
# answer[i]=product
# return answer
# print(productExceptSelf(nums))
# #TC is O(n^2) and SC is O(n)

def productExceptSelf(nums):
n=len(nums)
ans=[0]*n
left=1
right=1
for i,val in enumerate(nums):
ans[i]=left
left*=val #val = nums[i]
for i in range(n-1,-1,-1):
ans[i]*=right
right*=nums[i]
return ans
print(productExceptSelf(nums))
#TC is O(n) and SC is O(1)
#i nums[i] left (before update) ans[i] (stores left) left (after update)
# 0 1 1 1 1 × 1 = 1
# 1 2 1 1 1 × 2 = 2
# 2 3 2 2 2 × 3 = 6
# 3 4 6 6 6 × 4 = 24

#i nums[i] right (before update) ans[i] (stores right) right (after update)
# 3 4 1 6 x1 =6 1 × 4 = 4
# 2 3 4 2 x 4 =8 4 × 3 = 12
# 1 2 12 1 x 12 =12 12 × 2 = 24
# 0 1 24 1 x 24 =24 24 × 1 = 24


#ans=[24,12,8,6]