Day 1 Coding Challenge
Shraddha M Thakur
Software Developer | Actively Seeking Opportunities | Computer Applications Graduate | Passionate about Product Innovation
Today I have solved the first Problem of day 1.
Problem - Two sum Problem
Given an array of size n, and target variable, we need to find exactly one solution with indices of precisely two elements whose sum is equal to the target variable.
Condition - the same element should not be repeated twice.
Difficulty level - Easy
Solution - Categorize into Two solutions.
I) Brute - Force solution - We loop through every element of the array and compare with another element by looping again.
Time Complexity - O(n*n) due to two for loop.
Space Complexity - O(1)
II) Using HashMap - We can store the array value as Key and indices as value.
We are finding the complement by logic.
Complement= target - curr[element]
and store it in Map if we didn't get the complement variable.
Condition to check complement should not be equal to n[element].
Time Complexity - O(n)
Space Complexity - O(n)
In this problem, map is used to minimize time complexity, brushed up the knowledge of the map.