Close

## Finding all Pythagorean triples in an array

Given an array of unique elements, Write a program to print all Pythagorean triples in it. A Pythagorean triple is a set of three numbers such that square of one number is the sum of squares of the other two. For example (3,4,5) is Pythagorean triple because 52 = 32 + 42 Consider the array {5, 6, 3,…

## Finding the depth of n-ary tree given parent id representation

Given an a rooted n-ary tree represented as a parent id array, Write a program to find the depth of it. The depth of a tree is defined as the maximum length of the path from root to any leaf node. For example the following tree has a depth of 2 (for the path 0-2-4) It…

## Counting the number of inversions in a list

Given an array of numbers, how do we count the number of inversions in it? An inversion is an ordered pair if indices (i,j) such that i < j and A[i] > A[j] . For example consider the array {3, 2, 1, 5, 4} The inversions are {(0,1), (0,2), (1,2), (3,4)} totaling 4. We can…

## Maximum number of overlapping intervals

Given a set of intervals, how do we find the maximum number of intervals overlapping at any point of time. For example let us consider the following intervals. { (0,2), (3, 7), (4,6), (7,8), (1,5) } The maximum number of intervals overlapped is 3 during (4,5). This can be asked in many forms in a…

## Last digit of a power

Given a number expressed in base, exponent form (ab) where a is arbitrary big number For example contains 100 digits, We have to write a program to find the last digit in it’s expanded form. Eg: 45712 has a last digit of 1 Since the range of numbers is huge, it is impossible to expand…