Sorting Problem from today's Coderbit round.

Revision en1, by 444iq, 2019-09-02 03:07:19

You are given a permutation of N numbers from 1 to N in an array

You want to sort the array using this operation

  1. Send the front-most element X places back in the array;
  2. Add x to the total cost.

N<=10^5

Input : A = { 1 , 3 , 2 }

output : 3.

Explanation :

=> Send 1 to index 1. (Cost +1)

A [3, 1, 2]

Send 3 to index 2. (Cost +2)

A = [1, 2, 3]

and the array is sorted.

hence total cost = 3.

Any approach for solving this problem>

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English 444iq 2019-09-02 03:07:19 523 Initial revision (published)