Problem Statement This problem is from Leetcode – Missing Number. The problem statement is as below: Given an array nums of n distinct numbers from the range [0, n], return the single number in that range that is missing from the array. Solution Approach-1 Lets try a simple for loop approach first. See the code […]
Contains Duplicate – Leetcode #217
Problem statement This problem is from Leetcode – Contains Duplicate. Problem statement is given below: Given an integer array nums, return true if any value appears at least twice, otherwise return false. Solution Approach-1 Lets try a simple nested for loops approach. For each integer iterate the rest of the array to check whether another […]
Majority Element – Leetcode #169
Problem Statement This problem is from Leetcode – Majority Element. Given an array nums, return the element that appears more than half the time. Assume such an element always exists. Solution This problem can easily be solved by pushing the integers in the given array into a Hashmap and keeping a count of how many […]
Single Number – Leetcode #136
Problem Statement This problem is from Leetcode – Single Number. The problem statement is as below: Given an integer array where every element appears twice except one, find the single element that appears only once. Solution This problem is asking us to figure out that one integer that isn’t repeating. Lets take the Hashmap approach […]
Valid Palindrome – Leetcode #125
Problem Statement This problem is from Leetcode – Valid Palindrome. The problem statement in short is as below: Given a string s, return true if it is a palindrome after converting to lowercase and removing all non-alphanumeric characters; otherwise, return false. Solution This problem is asking us to check whether the given string is a […]
Merge Sorted Array – Leetcode #88
Problem Statement This problem is from Leetcode – Merge Sorted Array. The problem statement in short is as below: Merge two sorted arrays nums1 and nums2 into nums1 in-place. nums1 has length m + n with the first m elements valid; nums2 has length n. Solution Approach-1 See the code sample below: You can check […]
Length of Last Word – Leetcode #58
Problem Statement This problem is from Leetcode – Length of Last Word. The problem statement is as below: Given a string s consisting of words and spaces, return the length of the last word in the string Solution Approach-1 Lets try the most obvious approach first, by using split() Lets see the code sample below: […]
Search Insert Position – Leetcode #35
Problem Statement This problem is from Leetcode – Search Insert Position. The problem statement is given below: Given a sorted array and a target, return the target’s index or the insertion index if not found. Solution This problem is asking us to figure out the correct index position for a given number in a sorted […]
Best Time to Buy and Sell Stock – Leetcode #121
Problem Statement This problem is from Leetcode – Best Time to Buy and Sell Stock. The problem statement in short is as below: Given stock prices, find the max profit from one buy-sell transaction. Return 0 if no profit. Solution This problem is asking us to figure out that as we are moving forward in […]
Longest Common Prefix – Leetcode #14
Problem Statement This problem is from Leetcode – Longest Common Prefix. The problem statement is as below: Given an array of strings, return the longest common prefix. If none, return an empty string. Solution Lets see the brute force approach: Lets see the code sample below: This code returns the following output: You can check […]
Palindrome Number – Leetcode #9
Problem Statement This problem is from Leetcode – Palindrome Number. The problem statement in short is as below: Check if an integer x is a palindrome; return true if it is, false if not. SOLUTION In this question we are given an integer. We need to check if its a palindrome or not. Approach Lets […]
Two Sum Problem – Leetcode #1
Problem Statement This problem is from Leetcode – Two Sum Problem. The problem statement is as below: Given an array and a target, return indices of two numbers that add up to the target. SOLUTION This question is asking us to find two numbers in the given array nums which add up to the given […]