Question: Let n be a nonnegative integer. In this problem, we are given an array of integers A[ 1,..., n] and an integer x. We wish

Let n be a nonnegative integer. In this problem, we are given an array of integers A[ 1,..., n] and an integer x. We wish to compute the successor of x in A, which we define as the smallest element in A which is greater than x. For example, if A = [8,4,2, -7, -5,6,2] and x = 2, then the successor of x in A is 4. Similarly, the successor of -6 in A is -5. We define the successor of x in A to be infinity if there is no integer in A which is greater than x. Here is a recursive algorithm which takes as input A[ 1,... ,n] and an integer x, and returns the successor of x in A, as defined above. procedure Successor(A[1,..., n], x) if n = 0 then return infinity s := Successor (A [1,...,n - 1],x) if (A[n] > x and A[n]
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
