Given an array of integers, we can perform one operation where we replace any element array[i] with its squared value array[i] * array[i]. Our goal is to find the maximum possible subarray sum after performing this operation. So, if the input is like array = [4, 1, -2, -1], then the output will be 17. If we replace the value in array[0] with its squared value, the array becomes [16, 1, -2, -1]. The maximum subarray from this is [16, 1], which has the sum 16 + 1 = 17. Algorithm We use dynamic programming with ... Read More
Suppose we have a 2D array called adPair of size n-1 where each adPair[i] has two elements [ui, vi] representing that the elements ui and vi are adjacent in an array called nums. In nums there are n unique elements. We have to find the array nums. So, if the input is like adPair = [[3, 2], [4, 5], [4, 3]], then the output will be [2, 3, 4, 5]. Algorithm To solve this, we will follow these steps ? my_map := an empty map to store adjacency list for different ... Read More
Suppose, we are given a binary tree and are asked to find the distance between two nodes in the binary tree. We find out the edges between the two nodes like in a graph and return the number of edges or the distance between them. A node of a tree has the structure as below − data : right : left : So, if the input is like 5 ... Read More
Suppose we have one m x n matrix and another value k. The value of coordinate (a, b) of the matrix is the XOR of all matrix[i, j] where i is in range (0 to a) and j is in range (0 to b). We have to find the k-th largest value (1-indexed) of all the coordinates of the matrix. Problem Example Consider the following matrix: 5 2 1 6 If k = 1, then the output will be 7 because the value of coordinate (0, 1) is 5 XOR ... Read More
Suppose we have two strings s and t with only lowercase letters. In one operation, we can change any character in s or t to any lowercase letter. We have to satisfy one of the following three conditions ? Every letter in s is strictly smaller than every letter in t in the alphabet. Every letter in t is strictly smaller than every letter in s in the alphabet. Both s and t consist of only one distinct letter. We have to find the minimum number of ... Read More
Suppose we have an array enc that represents an encoded permutation. There is an array perm that is a permutation of the first n (odd) positive integers. This list is encoded into array enc of length n-1, such that enc[i] = perm[i] XOR perm[i+1]. We need to find the original array perm. So, if the input is like enc = [2, 5, 6, 3], then the output will be [7, 5, 0, 6, 5]. Here [7 XOR 5, 5 XOR 0, 0 XOR 6, 6 XOR 5] = [2, 5, 6, 3]. Algorithm To solve this, we ... Read More
Suppose we have a number n, an array called languages, and an array called friendships. There are n languages numbered from 1 through n, languages[i] represents a set of languages the ith user knows, and friendships[i] holds a pair [ui, vi] denoting a friendship between users ui and vi. We can select one language and teach it to some users so that all friends can communicate with each other. We have to find the minimum number of users required to teach. Note that friendships are not transitive, so if x is a friend of y and y is a ... Read More
Suppose we have an array called quality for each different worker, and have another array called wages and a value K. The i-th worker has a quality[i] and a minimum wage expectation wage[i]. We want to hire K workers to form a paid group. When we are hiring a group of K workers, we must pay them according to the following rules: Each worker in the paid group should be paid in the ratio of their quality by comparing with others in the paid group. Every worker in the paid ... Read More
Given a string, we need to check whether it can be split into exactly three palindromic substrings. A palindrome reads the same forwards and backwards. So, if the input is like s = "levelpopracecar", then the output will be True because we can split it like "level", "pop", "racecar" − all are palindromes. Algorithm To solve this, we will follow these steps − Create a 2D DP table to store which substrings are palindromes Fill the DP table using dynamic programming Try all possible ways to split the string into three parts Check if all ... Read More
The minimum cost to merge stones problem involves merging K consecutive piles of stones into one pile, where the cost equals the total stones in those K piles. We need to find the minimum cost to merge all piles into a single pile. This is a classic dynamic programming problem that uses interval DP to find the optimal way to merge stone piles. Understanding the Problem Given N piles of stones and a merge factor K, we can only merge exactly K consecutive piles at a time. The key insight is that for a solution to exist, ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance