Minimize the moves
1 Answer
Minimize the moves
| Flag | PURGE
This is an online written test.
Please solve it.
You have a sequence of d[0] , d[1], d[2] , d[3] ,..,d[n]. In each move you are allowed to increase any d[i] by 1 or 2 or 5 i:0 to n .What is the minimum number of moves required to transform the sequence to permutation of [1,2,3,..,n] if it's possible else return -1.
Constraint: 1<=n<=1000
nput:
First Line will have no of elemnts
Second Line on. elements will be present
Ex: 5
1
1
3
2
1
Output:
As no of elements are 5, all permutation can be {1, 2, 3, 4, 5} .
To convert input to get all possible permutation i.e {1, 2, 5, 4, 3},
no of moves will be 4.
Hence output would be 4.
Email me when people comment.
Email me when people comment.
Loading...
An error occurred in subscribing you.
Email me when people comment.
Email me when people comment.
Loading...
An error occurred in subscribing you.
CareerCup is the world's biggest and best source for software engineering interview preparation. See all our resources.
This can be done using greedy method. Have an index of numbers that we need to get once got a number mark it up and while making every move consider if maximum amount that is compatible.
- satya August 31, 2014