
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to find minimum length of first split of an array with smaller elements than other list in Python
Suppose we have a list of numbers nums, we want to split the list into two parts part1 and part2 such that every element in part1 is less than or equal to every element in part1. We have to find the smallest length of part1 that is possible (not 0 length).
So, if the input is like nums = [3, 1, 2, 5, 4], then the output will be 3, because we can split the list like part1 = [3, 1, 2] and part2 = [5, 4].
To solve this, we will follow these steps −
- p := minimum of nums
- s := 0
- for i in range 0 to size of nums - 1, do
- if nums[i] is same as p, then
- s := i
- come out from the loop
- if nums[i] is same as p, then
- p := maximum of the sub-list of nums[from index 0 to s]
- ans := s
- for i in range s + 1 to size of nums - 1, do
- if nums[i] < p, then
- ans := i
- if nums[i] < p, then
- return ans + 1
Example
Let us see the following implementation to get better understanding −
def solve(nums): p = min(nums) s = 0 for i in range(len(nums)): if nums[i] == p: s = i break p = max(nums[: s + 1]) ans = s for i in range(s + 1, len(nums)): if nums[i] < p: ans = i return ans + 1 nums = [3, 1, 2, 5, 4] print(solve(nums))
Input
[3, 1, 2, 5, 4]
Output
3
- Related Articles
- Program to split a set into equal sum sets where elements in first set are smaller than second in Python
- Find the first, second and third minimum elements in an array in C++ program
- Constructing an array of smaller elements than the corresponding elements based on input array in JavaScript
- Find Number of Array Elements Smaller than a Given Number in Java
- Program to find array of length k from given array whose unfairness is minimum in python
- Program to find minimum length of lossy Run-Length Encoding in Python
- Program to find minimum total cost for equalizing list elements in Python
- Program to find length of longest sublist where difference between min and max smaller than k in Python
- Program to return number of smaller elements at right of the given list in Python
- Python program to find Tuples with positive elements in List of tuples
- Find the first, second and third minimum elements in an array in C++
- Python – Combine list with other list elements
- Java Program to Get First and Last Elements from an Array List
- Program to find length of longest consecutive sublist with unique elements in Python
- Python program to find sum of elements in list

Advertisements