- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have a set of elements nums. We have to sort them in non-decreasing sequence. But the sorting technique is randomized. We will check whether the array is sorted or not, if not then random shuffle it and check again. Until all the elements are sorted continue this process. In this case we have to find expected number of shuffles required to sort them. Show the answer up to 6 decimal places.

So, if the input is like nums = [5,2,7], then the output will be 6 because there are 3 permutations are possible, so the probability is 1/3

- If we get sorted array at i = 1 number of iteration, then it will take 1/3
- If we get sorted array at i = 2 number of iterations, then it will take (2/3)*(1/3)

If we get sorted array at i-th number of iterations, then it will take (2/3)^(i-1) * (1/3)

To solve this, we will follow these steps −

- if nums is sorted, then
- return 0

- otherwise,
- m:= a new dictionary initially empty
- for each i in nums, do
- if i is present in m, then
- m[i] := m[i] + 1

- otherwise,
- m[i]:= 1

- if i is present in m, then
- num:= 1
- for each key i in m, do
- num := num * factorial(m[i])

- den:= factorial(size of nums)
- return (den/num) and rounding off up to 6 decimal places

Let us see the following implementation to get better understanding −

from math import factorial def solve(nums): if nums == sorted(nums): return 0 else: m={} for i in nums: if i in m: m[i]+=1 else: m[i]=1 num=1 for i in m: num *= factorial(m[i]) den=factorial(len(nums)) return round((den/num),6) nums = [5,2,7] print(solve(nums))

[5,2,7]

6.0

- Related Questions & Answers
- Program to find out the number of shifts required to sort an array using insertion sort in python
- Program to find number of swaps required to sort the sequence in python
- Program to find number of expected moves required to win Lotus and Caterpillar game in Python
- Minimum number of swaps required to sort an array in C++
- Python program to sort the elements of an array in ascending order
- Python program to sort the elements of an array in descending order
- Number of operations required to make all array elements Equal in Python
- Java program to find the sum of elements of an array
- C++ Program to Sort an Array of 10 Elements Using Heap Sort Algorithm
- Program to sort array by increasing frequency of elements in Python
- C program to sort an array of ten elements in an ascending order
- Python program to left rotate the elements of an array
- Python program to print the duplicate elements of an array
- Python program to right rotate the elements of an array
- Shuffle an Array in Python

Advertisements