CameraIcon
CameraIcon
SearchIcon
MyQuestionIcon
MyQuestionIcon
4
You visited us 4 times! Enjoying our articles? Unlock Full Access!
Question

Suppose you are given an array s[1...n] and a procedure reverse (s, i, j) which reverses the order of elements in between positions i and j (both inclusive). What does the following sequence do:

where 1kn:
reverse (s, 1, k);
reverse (s, k + 1, n);
reverse (s, 1, n);

A
Rotate s left by k position
Right on! Give the BNAT exam to get a 100% scholarship for BYJUS courses
B
None of the above
No worries! We‘ve got your back. Try BYJU‘S free classes today!
C
Leave s unchanged
No worries! We‘ve got your back. Try BYJU‘S free classes today!
D
Reverse all elements of s
No worries! We‘ve got your back. Try BYJU‘S free classes today!
Open in App
Solution

The correct option is A Rotate s left by k position
Effect of the given 3 reversals for any k is equivalent to left rotation of the array of size n by k.

Let S[ 1.......7]
1 2 3 4 5 6 7

n = 7, k = 2

reverse (S, 1, 2)
we get [ 2, 1, 3, 4, 5, 6, 7]

reverse (S, 3, 7)
we get [2, 1, 7, 6, 5, 4, 3 ]

reverse (S, 1, 7)
we get [3, 4, 5, 6, 7, 1, 2]

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