Day #1: My Start With 100 Days Of Code

I am a Software Engineer from Hyderabad. Here to learn and share what I learn.
Search for a command to run...

I am a Software Engineer from Hyderabad. Here to learn and share what I learn.
No comments yet. Be the first to comment.
Hello, today is the Day 2 of the #100DaysOfCodeChallenge. Received a problem previously asked by Uber with a hard tag to it. Tried my best in solving it, looking for better solutions if possible. But before that, an addition to yesterday's solution...
Unveil Your Brilliance with DIY Portfolio – Your Personalized Showcase, One File Edit Away!

Building a backend server application using Node.js, Express and JDoodle API to execute scripts and return the output as response.

Learning how to create mind maps to understand what's needed for the project and setting up the project folder with required dependencies quickly.

A series of articles on how to build a simple web app which serves an online IDE using the MERN stack to run code in multiple languages.

Hello everyone. Today is the Day 8 of the #100DaysOfCodeChallenge. Received a problem previously asked by Google with an easy tag to it. The Question On Day #8: A unival tree (which stands for "universal value") is a tree where all nodes under it hav...

On this page
Hi! I'm Manish. I was wondering what to do in this COVID-19 pandemic, to stay a bit productive and then the phrase 100 Days Of Code striked me while surfing the internet and I decided to take up the challenge.
To begin with that, I have enolled for the email subscription by Daily Coding Problems. The team at DCP sends us an email with one coding problem a day and I believed that it would be the right thing for the challenge. To add to that, I have decided to share my experience on solving the question too and start writing on dev.
The Question On Day #1:
Given a list of numbers and a number `k`, return whether any two numbers from the list add up to `k`.
For example, given `[10, 15, 3, 7]` and `k` of `17`, return true since `10 + 7` is `17`.
Python Code
l=[10,15,3,7]
k=17
flag=0
for i in range(len(l)-1):
if (k-l[i]) in l[i+1:]:
#slicing to prevent additional checks
print("pair exists")
flag=1
break
if flag==0:
print("pair doesn't exist")
Points To Note:
Time Complexity Of The Above Solution
The traversal through the list takes a time of O(n) where n is the length of list. For each element, there is an another check with in operator which again takes a time complexity of O(n-i) for a list in python ref.
The total time complexity for the solution will result in a time complexity of O(n^2).
An approach better to the above solution is to convert the list into a set first, does making the in operator efficient to use.
The time complexity of using the in operator on a set is O(1) on average and O(n) in the worst case.
Python Code
l=[10,15,3,7]
k=17
s=set(l) #converting list l into a set and storing it in s
flag=0
for i in s:
if (k-i) in s:
print("pair exists")
flag=1
break
if flag==0:
print("pair doesn't exist")
Time Complexity Of The Above Solution
The conversion from list to a set takes a time of O(n) where n is the length of list. Hence, the total time complexity for the above solution is O(n)+O(nx1) [O(nx1) on average and O(nxn) in the worst case for the loop and condition check using in operator on the set s].
Thus the total time complexity on average results to O(n) and O(n^2) in the worst case scenario.
This question helped me in understanding the time complexities of individual data structure operations in python. I would love to hear from my fellow developers and take suggestions on improving the above logic. I am a pretty newbie to solving such questions, please consider it. I also request you to drop suggestions to improve my article readability or understanding.
Thanks and cheers:)