wiz-icon
MyQuestionIcon
MyQuestionIcon
1
You visited us 1 times! Enjoying our articles? Unlock Full Access!
Question

Let 'a' be an array containing n integers in increasing order. The following algorithm determines whether there are two distinct numbers in the array whose difference is a specified number S > 0

i = 0; j = 1;
while (j < n)
{
if (E)
j++;
else if (a[j] — a[i] = = S)
break;
else
i++;
}
if (j < n)
printf("yes");
else
printf ("no");

Choose the correct expression for E.

A
a[j] - a[i] < S
Right on! Give the BNAT exam to get a 100% scholarship for BYJUS courses
B
a[j] - a[i] > S
No worries! We‘ve got your back. Try BYJU‘S free classes today!
C
a[i] - a[j] < S
No worries! We‘ve got your back. Try BYJU‘S free classes today!
D
a[i] - a [j] > S
No worries! We‘ve got your back. Try BYJU‘S free classes today!
Open in App
Solution

The correct option is A a[j] - a[i] < S
x y
i j

So, if conditions will check a[j] - a[i] < S then increment j so that difference between two elements increase and when a[j] - a[i] > S then increment i for making difference between two element indexed at i and j respectively is decrease.

So, a[j] - a[i] < S is correct condition.

flag
Suggest Corrections
thumbs-up
0
similar_icon
Similar questions
View More
Join BYJU'S Learning Program
similar_icon
Related Videos
thumbnail
lock
Functions
QUANTITATIVE APTITUDE
Watch in App
Join BYJU'S Learning Program
CrossIcon