2Sum 2 – Input array is sorted

Problem This problem is similar to 2Sum Problem, only difference is input array is now sorted. Detailed problem Given a 1-indexed array of integers numbers that is already sorted in non-decreasing order, find two numbers such that they add up to a specific target number. Let these two numbers be numbers[index1] and numbers[index2] where 1 <= index1 < index2 <= numbers.length. Return the indices of the two numbers, index1 and index2, added by one as an integer array [index1, index2] of length 2. ...

Permutations of Array 1

Problem Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order. OR Given a collection of numbers, return all possible permutations. Examples Example 1: nums = [1,2,3] Output: [ [1,2,3] [1,3,2] [2,1,3] [2,3,1] [3,1,2] [3,2,1] ] ...

Floyd-Warshall Algorithm

Introduction The Floyd-Warshall algorithm is a classic algorithm used to find the shortest paths between all pairs of vertices in a weighted graph. It’s especially useful for dense graphs and can handle both positive and negative edge weights (though not negative cycles). Algorithm Initialization: Create a distance matrix dist where dist[i][j] is the initial weight of the edge from vertex i to vertex j, or infinity if no edge exists. Set dist[i][i] = 0 for all vertices i. Dynamic Programming Update: For each intermediate vertex k, update the distance matrix to consider paths that pass through vertex k. For each pair of vertices (i, j), compute if a shorter path exists by going through k: if (dist[i][k] + dist[k][j] < dist[i][j]) { dist[i][j] = dist[i][k] + dist[k][j]; } Result: After processing all vertices, dist[i][j] will contain the shortest path distance from vertex i to vertex j. Pseudocode function floydWarshall(graph): dist = matrix of size VxV, initially filled with graph weights (or infinity if no edge exists) for each vertex v: dist[v][v] = 0 for each intermediate vertex k: for each source vertex i: for each destination vertex j: if dist[i][k] + dist[k][j] < dist[i][j]: dist[i][j] = dist[i][k] + dist[k][j] return dist ...

Integer To Roman Problem

Problem Roman numerals Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. Examples Example 1: Input: num = 3 Output: "III" Explanation: 3 is represented as 3 ones. Example 2: Input: num = 58 Output: "LVIII" Explanation: L = 50, V = 5, III = 3. ...

String Compression Problem

Problem Given an array of characters chars, compress it using the following algorithm: Begin with an empty string s. For each group of consecutive repeating characters in chars: If the group’s length is 1, append the character to s. Otherwise, append the character followed by the group’s length. The compressed string s should not be returned separately, but instead, be stored in the input character array chars. Note that group lengths that are 10 or longer will be split into multiple characters in chars. ...

Remove Nth Node From End of List

Problem Given a linked list, remove the nth node from the end of list and return its head. Examples Example 1: Input: head = [1,2,3,4,5], n = 2 Output: [1,2,3,5] Explanation: Given linked list 1->2->3->4->5 and n = 2, the result is 1->2->3->5. ...

Letter Combinations of a Phone Number Problem

Problem Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. Return the answer in any order. A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters. ...

4Sum

Problem Given an array nums of n integers, return an array of all the unique quadruplets [nums[a], nums[b], nums[c], nums[d]] such that: 0 <= a, b, c, d < n a, b, c, and d are distinct. nums[a] + nums[b] + nums[c] + nums[d] == target You may return the answer in any order. Similar Problem on Pramp Your function should return an array of these numbers in an ascending order. If such a quadruplet doesn’t exist, return an empty array. ...

Add Two Numbers represented as Linked List in Reversed Order

Problem You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list. You may assume the two numbers do not contain any leading zero, except the number 0 itself. Examples Example 1: (2 -> 4 -> 3) + (5 -> 6 -> 4) => 7 -> 0 -> 8 Input: l1 = [2,4,3], l2 = [5,6,4] Output: [7,0,8] Explanation: 342 + 465 = 807 ...

Reordered Power of 2 Problem

Problem You are given an integer n. We reorder the digits in any order (including the original order) such that the leading digit is not zero. Return true if and only if we can do this so that the resulting number is a power of two. Examples Example 1: Input: n = 1 Output: true ...

This site uses cookies to improve your experience on our website. By using and continuing to navigate this website, you accept this. Privacy Policy