0% found this document useful (0 votes)
24 views

S Such That (X, Y) and (Y, X) Are Treated As Same and - A - B - Is The Absolute

An algorithm to find the pair (x, y) in an unsorted array S of integers such that the absolute difference between x and y is minimum. The algorithm runs in O(n) time by iterating through the array once to find the minimum difference. An algorithm to find the median of n items using a stack data structure. One approach pushes all items onto a stack then repeatedly pops and inserts items to sort the stack in O(n^2) time. A second approach uses two stacks to partition items below and above the median in O(n) time. An algorithm to identify elements repeated 1, 2, 3, etc. times in a set of n numbers. The algorithm uses a hash table

Uploaded by

vit1234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

S Such That (X, Y) and (Y, X) Are Treated As Same and - A - B - Is The Absolute

An algorithm to find the pair (x, y) in an unsorted array S of integers such that the absolute difference between x and y is minimum. The algorithm runs in O(n) time by iterating through the array once to find the minimum difference. An algorithm to find the median of n items using a stack data structure. One approach pushes all items onto a stack then repeatedly pops and inserts items to sort the stack in O(n^2) time. A second approach uses two stacks to partition items below and above the median in O(n) time. An algorithm to identify elements repeated 1, 2, 3, etc. times in a set of n numbers. The algorithm uses a hash table

Uploaded by

vit1234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

(1) Let S be an unsorted array of n integers.

Write two different


algorithms with different running times to find the pair (x, y)
where x, y ∈ S such that |x− y|=minimum {|a−b|: ∀ a , b ∈ S , a ≠ b }. Here
(x, y) and (y, x) are treated as same and | a - b | is the absolute
difference of two numbers a and b. For example, for S={22,
80, 18, 9, 90, 12, 24, 57, 86, 36 }, the required pair is ( 22,
24).
(2) The median of n numbers is defined as that item which is less
than (or equal to) half of the n numbers and which is larger than
(or equal to) half of the n numbers. Half of the n numbers are
defined as the ceiling of n/2. For example, the median of 16, 12,
99, 95, 18, 87, 10 is 18. Here, of the given seven numbers, a
ceiling of 7/2 is 4, 18 is less than or equal to the four numbers
such as 10,12,16 and 18 and is greater than or equal to the four
numbers such as 18, 87,95 and 99. One more example , the
median of 1,2,5,6,4,3,8,4,7 is 4. Design two different algorithms
(using two different techniques) to find the median of n items
with stack data structure. Analyze the running time of your
algorithms.
(3) Consider a set which contains n numbers. The ‘repeated-
elements’ problem is to identify the elements which are
repeated one time, two times, three times and so on (up to the
elements which are repeated n number of times). Write an
algorithm to solve the repeated-elements problem and also
compute the running time of your algorithm.

(4) Consider the problem ‘ 2D SEARCH’ which is described as


follows : M is an n × n matrix in which numbers in each row are
in an increasing order (reading the numbers from left to right)
and numbers in each column are in an increasing order (reading
the numbers from top to bottom) . The ‘ 2D SEARCH ’
problem is to find whether the given number ‘x’ occurs in the
matrix M or not. If x occurs in the matrix, your algorithm
should give the position of x in M . Otherwise, your algorithm
returns -1. Write an algorithm to solve the 2D SEARCH’
problem and compute the running time of your algorithm.

(5) [ a bd ] be a two dimensional matrix of size 2 ×2 and the


Let A= c
determinant of A is defined as ad-bc . Let S be a finite set of two
dimensional matrices of size 2 ×2. Write an algorithm to sort the
two dimensional matrices in non-decreasing order of their
determinants and compute the running time of your algorithm.

(6) Given ‘n’ circles, C1, C2, …, Cn with radii R1, R2,…, Rn
respectively ( Ri ≥ =1 ∀ i ). All circles can have any point as the
center. Without calculating the areas of the given circles, write
an algorithm to arrange the circles in increasing order of their
areas and compute the running time of your algorithm.

(7) Let S= { A1, A2, …, An } be a finite set , where each ‘Ai’ is a set
with ‘ n’ integers . Write an algorithm to compute the
intersection of all elements in S i.e., A1∩A2∩A3…∩An and
compute the running time of your algorithm.

(8) Let A be a one-dimensional array of n distinct numbers . A pair


(A[i], A[j]) is said to be an index-value inversion if these
numbers are such that A[i]=j and A[j]=i. Write an algorithm for
counting the number of index-value inversions and also
compute the running time of your algorithm.

(9)
A sequence an is defined as follows :
1
1+
1 1
1+ 1+
1 1 1
1+ 1+ 1+
1 1 1 1
1+ 1+ 1+ 1+
a1 = 1 , a2 = 2 a
, 3 = 3 , a4 = 4 ….

Design a recursive algorithm to calculate an and write a C language code for your algorithm.

You might also like