# All posts in Algorithms

Problem Statement Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times. Example 1 Input: intervals = [[1,3],[6,9]], newInterval = [2,5] Output: [[1,5],[6,9]] Example 2 Input: intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval . . . Read more

Problem Statement Given an array of non-negative integers , you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index. Example 1 Input: nums = [2,3,1,1,4] Output: . . . Read more

Problem Statement Given the head of a linked list, rotate the list to the right by places. Example 1: Input: head = [1,2,3,4,5], k = 2 Output: [4,5,1,2,3] Example 2: Input: head = [0,1,2], k = 4 Output: [2,0,1] Solution # Definition for singly-linked list. # class ListNode: # def . . . Read more

Problem Statement Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum. Example 1: Input: nums = [-2,1,-3,4,-1,2,1,-5,4] Output: 6 Explanation: [4,-1,2,1] has the largest sum = 6. Example 2: Input: nums = [1] Output: 1 Example . . . Read more

Problem Statement The gray code is a binary numeral system where two successive values differ in only one bit. Given an integer n representing the total number of bits in the code, return any sequence of gray code. A gray code sequence must begin with 0. Example 1 Input: n . . . Read more

Problem Statement Given an array of non-negative integers , you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum number of jumps. You can assume . . . Read more

Problem Statement Given an array of integers, find the subset of non-adjacent elements with the maximum sum. Calculate the sum of that subset. It is possible that the maximum sum is 0, the case when all elements are negative. Example The following subsets with more than 1 element exist. These exclude . . . Read more

Problem Statement Given an set of non-negative integers, and a value sum, print out all the subsets of the given array whose elements have sum equal to given sum. Solution To solve the problem we use the Dynamic programming. For the first step we will create a 2D array, dp, . . . Read more

Problem Statement 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 “”. Examples Solution

Problem Statement Given a string containing just the characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[‘ and ‘]’, determine if the input string is valid. An input string is valid if: Open brackets must be closed by the same type of brackets. Open brackets must be closed in the correct order. Note that an empty string is also considered valid. . . . Read more