Get best answers to any doubt/query/question related to programming , jobs, gate, internships and tech-companies. Feel free to ask a question and you will receive the best advice/suggestion related to anything you ask about software-engineering , development and programming problems .

0 like 0 dislike
1,655 views
in Online Assessments by Expert (144,420 points)

1 Answer

0 like 0 dislike

Q : 

Given an integer array nums, do the following operations:

 

  • If the first element is the smallest element in the array, remove it
  • Otherwise, puts it to the end of the array

 

Return number of operations it takes for nums to be empty.

 

Example
Input
nums:

 

[2,1,3]

 

Output

 

5

 

Explanation

 

After 1st operation, nums becomes [1, 3, 2] since 2 is not the smallest value in [2,1,3], puts 2 to the end of nums
After 2nd operation, nums becomes [3, 2] since 1 is the smallest value in [1,3,2]
After 3rd operation, nums becomes [2,3] since 3 is not the smallest value in [3,2], puts 3 to the end of nums
After 4th operation, nums becomes [3] since 2 is the smallest value in [2,3]
After 5th operation, nums becomes [] since 5 is the smallest value in [5]

 

After the 5th operation, nums becomes [].

 

So return 5.

 

Constraints
1 <= nums.length <= 2*10^5
1 <= nums[i] <= 10^5 where 0 <= i < nums.length

by Expert (144,420 points)
...