Suppose we have one undirected, connected tree where N nodes are present. These are labelled as 0...N-1 and N-1 edges are given. The ith edge connects nodes edges[i][0] and edges[i][1] together. We have to find a list where ans[i] is the sum of the distances between node i and all other nodes.So, if the input is like N = 6 and edges = [(0, 1), (0, 2), (2, 3), (2, 4), (2, 5)], then the output will be [8, 12, 6, 10, 10, 10]To solve this, we will follow these steps −Define a function dfs1(), this will take node, parent, ... Read More
Suppose we have a positive integer N, we have to find how many different ways can we write it as a sum of consecutive positive integers?So, if the input is like 10, then the output will be 3, this is because we can represent 10 as 5 + 5 and 7 + 3, so there are two different ways.To solve this, we will follow these steps −ret := 1for initialize i := 2, (increase i by 1), do −sum := (i * (i + 1)) / 2if sum > N, then −Come out from the looprem := N - sumret ... Read More
In this problem, we are given an array of characters. Our task is to create a program to print the maximum length of the subarray whose first and last elements are same in C++.Let’s take an example to understand the problem, Input − array = {‘t’, ‘u’, ‘t’, ‘o’, ‘r’, ‘i’, ‘a’, ‘l’, ‘s’, ‘p’, ‘o’, ‘i’, ‘n’, ‘t’ }Output − 14Explanation −The subarray {‘t’, ‘u’, ‘t’, ‘o’, ‘r’, ‘i’, ‘a’, ‘l’, ‘s’, ‘p’, ‘o’, ‘i’, ‘n’, ‘t’ } starts and ends with t.To solve this problem, we find the first and last occurrence a character in the array and ... Read More
In this problem, we are given an array, are of n elements. Our task is to create a program to find the maximum modulo of all pairs of array where arr[i] >= arr[j].Here, we have to find the maximum value of arr[i] % arr[j] where arr[i] >= arr[j].Let’s take an example to understand the problem, Input − arr[] = {3, 5, 9}Output − 4Explanation −All possible Pairs arr[i] and arr[j], 5, 3 => 5%3 = 2 9, 3 => 9%3 = 0 9, 5 => 9%5 = 4To solve this problem, a simple and direct approach will be run two ... Read More
In this problem, we are given an array of size n and a number M. Our task is to create a program that will find the maximum number of unique integers in Sub-array of given size.Here, we will have to find the sub-array of size M that has the maximum number of unique elements.Let’s take an example to understand the problem, Input − array = {4, 1, 2, 1 , 4, 3}. M = 4Output − 4Explanation −All possible combinations of sub-arrays of size 4. {4, 1, 2, 1} = 3 unique elements {1, 2, 1, 4} = 3 unique ... Read More
In this problem, we are given two integers N and M, N is the number of people in group 1, and M is the number of people in group 2. Our task is to create a program to find the maximum number of 3-person teams formed from two groups.We will be creating teams of 3 people by selecting a person from these groups such that the maximum teams can be made. Each team must have at least one person from each group.Let’s take an example to understand the problem, Input − N = 5, M = 3Output − 2Explanation −The ... Read More
In this problem, we are given a binary tree with each node containing a value. Our task is to create a program to find the maximum path sum between two leaves of a binary tree.Here, we have to find the path form one leaf node to another leaf node that will provide the maximum sum of values. This maximum sum path can/cannot include the root node.Binary Tree is a tree data structure in which each node can have a maximum of two child nodes. These are called a left child and right child.Example −Let’s take an example to understand the ... Read More
In this problem, we are given numbers that are in the form of a triangle. Our task is to create a program that will find the maximum path sum in a triangle.The elements are arranged starting from the 1st row with 1 one element and then next rows with an increasing number of elements till there are elements in the nth row.So, the program will find the path that will provide the maximum sum of elements in the triangle. So, we have to find the path that will provide the maximum sum.Let’s take an example to understand the problem −Input ... Read More
In this problem, we are given numbers in the form of an inverted triangle. Our task is to create a program that will find the maximum path sum in an inverted triangle.Inverted triangle form of number is an arrangement when the first row contains n elements, second n-1, and so on.Here, we have to find the maximum sum that can 3 be obtained by adding one element from each row.Let’s take an example to understand the problem −Input −5 1 9 3 6 2Output − 17Explanation − Here, I have found the path from the last row to the ... Read More
In this problem, we are given a 2D matrix of size M*N. Our task is to create a program that will find the maximum path sum in the matrix.Here, the maximum path sum in the matrix is defined as the sum of all elements for one row to the last row. The allowed moves for traversing the path are downward move and diagonal move. The start and endpoints can be any element of the first and last row of the matrix respectively.Let's take an example to understand the problemInput −matrix [][] = 3 5 9 1 7 2 ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP