DS Question Paper

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

1.

Find equilibrium index of an array


Given an integer array, find the equilibrium index in it.

For an array A consisting n elements, index i is an equilibrium index if the sum of

elements of subarray A[0…i-1] is equal to the sum of elements of subarray

A[i+1…n-1]. i.e.

(A[0] + A[1] + … + A[i-1]) = (A[i+1] + A[i+2] + … + A[n-1]), where

0 < i < n-1

Similarly, 0 is an equilibrium index if A[1] + A[2] + … + A[n-1] sums to 0 and

n-1 is an equilibrium index if A[0] + A[1] + … + A[n-2] sums to 0.

To illustrate, consider the array {0, -3, 5, -4, -2, 3, 1, 0}. The equilibrium

index is found at index 0, 3, and 7.

2. Longest Bitonic Subarray Problem

The Longest Bitonic Subarray (LBS) problem is to find a subarray of a given sequence in

which the subarray’s elements are first sorted in increasing order, then in decreasing

order, and the subarray is as long as possible. Strictly ascending or descending

subarrays are also accepted.


For example,

Longest bitonic subarray of the sequence { 3, 5, 8, 4, 5, 9, 10, 8,


5, 3, 4 } is { 4, 5, 9, 10, 8, 5, 3 }

For sequences sorted in increasing or decreasing order, the output is


the same as the input sequence, i.e.,

[1, 2, 3, 4, 5] ——> [1, 2, 3, 4, 5]

[5, 4, 3, 2, 1] ——> [5, 4, 3, 2, 1]

You might also like