From ee73f6dbfd712713c845ec95bb7c2fe7471a8487 Mon Sep 17 00:00:00 2001 From: Betcy-Rajan Date: Wed, 19 Mar 2025 00:56:26 +0530 Subject: [PATCH 1/5] Added solution for Two Sum --- Challenge-1/two-sum.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 Challenge-1/two-sum.py diff --git a/Challenge-1/two-sum.py b/Challenge-1/two-sum.py new file mode 100644 index 0000000..58a0607 --- /dev/null +++ b/Challenge-1/two-sum.py @@ -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)) From 485b9bef4951b1dd5be37c594d6c5c941dcd249b Mon Sep 17 00:00:00 2001 From: Betcy-Rajan Date: Wed, 19 Mar 2025 12:50:13 +0530 Subject: [PATCH 2/5] Added solution for Best time to buy and sell stock --- .../best-time-to-buy-and-sell-stock.py | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 Challenge-2/best-time-to-buy-and-sell-stock.py diff --git a/Challenge-2/best-time-to-buy-and-sell-stock.py b/Challenge-2/best-time-to-buy-and-sell-stock.py new file mode 100644 index 0000000..c0cd675 --- /dev/null +++ b/Challenge-2/best-time-to-buy-and-sell-stock.py @@ -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)) From d7c92f0ecf393ff6a7f1c9ffb6fde33bc11caf47 Mon Sep 17 00:00:00 2001 From: Betcy-Rajan Date: Thu, 20 Mar 2025 12:49:26 +0530 Subject: [PATCH 3/5] Added solution for Contains Duplicates --- Challenge-3/contains-duplicates.py | 46 ++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 Challenge-3/contains-duplicates.py diff --git a/Challenge-3/contains-duplicates.py b/Challenge-3/contains-duplicates.py new file mode 100644 index 0000000..08b7644 --- /dev/null +++ b/Challenge-3/contains-duplicates.py @@ -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)) From d5d4c659af20c12bf84862ff47bbb453f6aceee4 Mon Sep 17 00:00:00 2001 From: Betcy-Rajan Date: Sun, 23 Mar 2025 09:25:58 +0530 Subject: [PATCH 4/5] Added solution for sum-two-numbers --- Challenge-4/sum-two-numbers.py | 45 ++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 Challenge-4/sum-two-numbers.py diff --git a/Challenge-4/sum-two-numbers.py b/Challenge-4/sum-two-numbers.py new file mode 100644 index 0000000..b3bc2ca --- /dev/null +++ b/Challenge-4/sum-two-numbers.py @@ -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 + + + + + From d8d31f9971d3d7e54d25053803022b1a510f13f1 Mon Sep 17 00:00:00 2001 From: Betcy-Rajan Date: Sun, 23 Mar 2025 10:11:10 +0530 Subject: [PATCH 5/5] Added solution for Product of Array Except itself --- Challenge-5/Product-of-array-except-itself.py | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 Challenge-5/Product-of-array-except-itself.py diff --git a/Challenge-5/Product-of-array-except-itself.py b/Challenge-5/Product-of-array-except-itself.py new file mode 100644 index 0000000..6bffd95 --- /dev/null +++ b/Challenge-5/Product-of-array-except-itself.py @@ -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] \ No newline at end of file