• edited by
2,238 views
0 0 votes

We are given a sequence of $n$ positive numbers $a_{1}, a_{2}, \ldots, a_{n}$ and a fixed number $k>0$. We want to find a pair of numbers $a_{i}$ and $a_{j}$ such that $j>i$ and $a_{j}-a_{i}=k$. If there is no such pair we want to output "false". This can be solved easily in big-theta( $\mathrm{n}^{2}$ ) time by examining all pairs of numbers. If want to devise an algorithm of $O(n \log n)$ then which method you will follow to solve this problem?
(A) Dynamic programming
(C) Either ' a ' or ' b '
(B) Divide-and-conquer
(D) None of the these

2 Answers

0 0 votes
what i think the answer should be B as i am using this method in the sorting technique and also in the search approach.. well someone can do it using A so it can be the answer. what i tried is this.

sort elements in nlogn time

now pic the middle element and subtract from the last one. ( as the difference should be positive , if negative pic the starting element)

** further i m describing for positive difference. the algorithm can be ,modified for negative as well .

now subtract the mid and last element . if the diffrence we get is more than the difference we need. then we will set mid as the lower and again calculate the mid , as by moving toward more big numbers only the difference will decrease.
like here

take array

6  4 9  2  1

sort .

1 2  4  6  9  .

suppose i want 3 as a difference, then

picking middle element i.e =4. that is 9-4 = 5. . so as the answer is greter than what we ned then we have to move toward 9 because then only we can decrease our answer, if i move toward 1 the difference will increase. so in this way i can use the binary search type method to get . make such n iterations. each time leaving the highest element that has been examined.

time complexity . = (nlogn)+ n*logn. = (nlogn) .

i don't think repeating subproblems are there , and divide and conquer , as used . may be the answer
• edited by
Position:
Show:

Related questions

9 9 votes
2 answers 2 answers
23.5k
23.5k views
0 0 votes
1 answers 1 answer
1.1k
1.1k views
0 0 votes
1 1 answer
887
887 views
dhruba asked Jun 5, 2023
887 views
Consider performing QuickSort on an array of n distinct elements. What is the probability that no comparisons will be made between the smallest and largest element?a. 1/n...
2 2 votes
1 1 answer
2.2k
2.2k views
yes asked Oct 6, 2015
2,219 views
for example array contain a[1 2 3 3 3 3 3 4 5] then retun(1)