- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions

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

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

- if s[i] is same as nums[i], then
- return count

Let us see the following implementation to get better understanding −

## Example

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

- Related Articles
- Program to count number of intervals which are intersecting at given point in Python
- Program to count number of sublists with exactly k unique elements in Python
- Program to count number of elements present in a set of elements with recursive indexing in Python
- 8085 program to count number of elements which are less than 0A
- Program to find number of elements in A are strictly less than at least k elements in B in Python
- Program to count number of elements in a list that contains odd number of digits in Python
- Program to count number of ways to win at most k consecutive games in Python
- Program to return number of smaller elements at right of the given list in Python
- Program to count number of palindromic substrings in Python
- Program to count number of unhappy friends in Python
- Program to count number of homogenous substrings in Python
- Program to count number of nice subarrays in Python
- Golang program to add elements at first and last position of linked list
- Program to count index pairs for which array elements are same in Python
- Program to count number of word concatenations are there in the list in python

Advertisements