# Program to count number of elements are placed at correct position in Python

PythonServer Side ProgrammingProgramming

#### Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

#### Practical Machine Learning using Python

Best Seller

91 Lectures 23.5 hours

#### Practical Data Science using Python

22 Lectures 6 hours

Suppose we have a list of numbers called nums, we have to find the number of elements that are present in the correct indices, when the list was to be sorted.

So, if the input is like [2, 8, 4, 5, 11], then the output will be 2, as the elements 2 and 11 are in their correct positions. The sorted sequence will be [2, 4, 5, 8, 11]

To solve this, we will follow these steps −

• s := sort the list nums
• count := 0
• for i in range 0 to size of nums, do
• if s[i] is same as nums[i], then
• count := count + 1
• return count

Let us see the following implementation to get better understanding −

## Example

Live Demo

class Solution:
def solve(self, nums):
s = sorted(nums)
count = 0
for i in range(len(nums)):
if s[i] == nums[i]:
count += 1
return count
ob = Solution()
print(ob.solve([2, 8, 4, 5, 11]))

## Input

[2, 8, 4, 5, 11]

## Output

2