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 in the array, and return false if every element is distinct. Solution Approach-1 Lets try a simple nested for loops approach. For each integer iterate the […]
Majority Element – Leetcode #169
Problem Statement This problem is from Leetcode – Majority Element. Given an array nums of size n, return the majority element. The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array. Solution This problem can easily be solved by […]
Single Number – Leetcode #136
Problem Statement This problem is from Leetcode – Single Number. The problem statement is as below: Given a non-empty array of integers nums, every element appears twice except for one. Find that single one. You must implement a solution with a linear runtime complexity and use only constant extra space. Solution This problem is asking us to figure out […]
Valid Palindrome – Leetcode #125
Problem Statement This problem is from Leetcode – Valid Palindrome. The problem statement is as below: A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers. Given a string s, return true if […]
Merge Sorted Array – Leetcode #88
Problem Statement This problem is from Leetcode – Merge Sorted Array. The problem statement is as below: You are given two integer arrays nums1 and nums2, sorted in non-decreasing order, and two integers m and n, representing the number of elements in nums1 and nums2 respectively. Merge nums1 and nums2 into a single array sorted […]
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 of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. Solution This problem is […]
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 is as below: 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 […]
Longest Common Prefix – Leetcode #14
Problem Statement This problem is from Leetcode – Longest Common Prefix. The problem statement is as below: Write a function to find the longest common prefix string amongst an array of strings. If there is no common prefix, return an empty string “” Solution Lets see the brute force approach: Lets see the code sample […]
Palindrome Number – Leetcode #9
Problem Statement This problem is from Leetcode – Palindrome Number. The problem statement is as below: Given an integer x, return true if x is a palindrome, and false otherwise; SOLUTION In this question we are given an integer. We need to check if its a palindrome or not. Approach Lets see the code below: […]
Two Sum Problem – Leetcode #1
Problem Statement This problem is from Leetcode – Two Sum Problem. The problem statement is as below: 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 […]