## Oracle Interview Question

Backend Developers**Country:**India

Since the order of given numbers is not guaranteed, i don't think we have another option to achieve this less than O(n*n)

nLogn solution is possible.

Create a map ( a balanced BST for log n lookup ).

insert last item in the map.

[1] Start from the second last element in the list and go backwards .

[2] for every element find next bigger element in map ( something like upper_bound in C++ ). And use that result in the result array. insert current element in original array into map ( so its available for next lookup).

```
# Replace each number with the next bigger number from right side of current index.
# program is in python version 3...
arr_input = [2,5,9,6,3,4,8,15,12];#input array......
arr_result = arr_input;#result or output of our input....
print(arr_input)
for i in range(len(arr_input)):
temp = arr_input[i]
remainingelements = sorted(arr_input[i:])#make a sorted array of remaining elements on right side
for j in range(len(remainingelements)):
if(remainingelements[j]>temp):#check for greatest element on the right side....
arr_result[i] = remainingelements[j]#insert to the output
break
# print the output
print(arr_result)
```

```
# Replace each number with the next bigger number from right side of current index.
# program is in python version 3...
arr_input = [2,5,9,6,3,4,8,15,12];#input array......
arr_result = arr_input;#result or output of our input....
print(arr_input)
for i in range(len(arr_input)):
temp = arr_input[i]
remainingelements = sorted(arr_input[i:])#make a sorted array of remaining elements on right side
for j in range(len(remainingelements)):
if(remainingelements[j]>temp):#check for greatest element on the right side....
arr_result[i] = remainingelements[j]#insert to the output
break
# print the output
print(arr_result)
```

~ replace each number in a list by its next bigger number.

~ No replacing if the number is the biggest or at the end of the list.

~ code in Phyton 3

```
def fun (lst):
work = lst
for i in range(0,len(work)):
if work[i] == max(work) or i == len(work)-1:
continue
else:
temp = [x for x in work if x > work[i]]
work[i] = min(temp)
return work
```

Mayn't be well optimized but should work.

- SC December 19, 2018