title-img


Subtrees And Paths

Given a rooted tree of N nodes, where each node is uniquely numbered in between [1..N]. The node 1 is the root of the tree. Each node has an integer value which is initially 0. You need to perform the following two kinds of queries on the tree: add t value: Add value to all nodes in subtree rooted at t max a b: Report maximum value on the path from a to b Input Format First line contains N, number of nodes in the tree. Next N-1 lines contain two space separated integers x and y whic

View Solution →

Triplets

There is an integer array d which does not contain more than two elements of the same value. How many distinct ascending triples (d[i],< d[j] < d[k] , i < j < k ) are present? Input format The first line contains an integer, N, denoting the number of elements in the array. This is followed by a single line, containing N space-separated integers. Please note that there are no leading spaces before the first number, and there are no trailing spaces after the last number. Output format:

View Solution →

BST maintenance

Consider a binary search tree T which is initially empty. Also, consider the first N positive integers {1, 2, 3, 4, 5, ....., N} and its permutation P {a1, a2, ..., aN}. If we start adding these numbers to the binary search tree T, starting from a1, continuing with a2, ... (and so on) ..., ending with aN. After every addition we ask you to output the sum of distances between every pair of T's nodes. Input Format The first line of the input consists of the single integer N, the size of

View Solution →

Append and Delete

You have two strings of lowercase English letters. You can perform two types of operations on the first string: 1. Append a lowercase English letter to the end of the string. 2. Delete the last character of the string. Performing this operation on an empty string results in an empty string. Given an integer, k, and two strings, s and t, determine whether or not you can convert s to t by performing exactly k of the above operations on s. If it's possible, print Yes. Otherwise, print No.

View Solution →

Sherlock and Squares

Watson likes to challenge Sherlock's math ability. He will provide a starting and ending value that describe a range of integers, inclusive of the endpoints. Sherlock must determine the number of square integers within that range. Note: A square integer is an integer which is the square of an integer, e.g. .1, 4, 9, 16, 25. Example a = 24 b = 49 There are three square integers in the range: 25, 36 and 49. Return 3. Function Description Complete the squares function in the edit

View Solution →