- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to find out the greatest subarray of a given length in python
Suppose we have an array containing various integer values and a given length k. We have to find out the greatest subarray from the array of the given length. A subarray is said to be greater than another subarray, if subarray1[i] ≠ subarry2[i] and subarray1[i] > subarry2[i].
So, if the input is like nums = [5, 3, 7, 9], k = 2, then the output will be [7, 9].
To solve this, we will follow these steps −
- start := size of nums - k
- max_element := nums[start]
- max_index := start
- while start >= 0, do
- if nums[start] > max_element is non-zero, then
- max_element := nums[start]
- max_index := start
- return nums[from index max_index to max_index + k]
- if nums[start] > max_element is non-zero, then
- return nums[from index max_index to max_index + k]
Let us see the following implementation to get better understanding −
Example
def solve(nums, k): start = len(nums) - k max_element = nums[start] max_index = start while start >= 0: if nums[start] > max_element: max_element = nums[start] max_index = start start -= 1 return nums[max_index:max_index + k] print(solve([5, 3, 7, 9], 2))
Input
[5, 3, 7, 9], 2
Output
[7, 9]
- Related Articles
- Program to find out the sum of the maximum subarray after a operation in Python
- Program to find maximum length of subarray with positive product in Python
- Program to find out the value of a given equation in Python
- Python Program to find out the determinant of a given special matrix
- Program to find out the length of longest palindromic subsequence using Python
- Program to Find Out the Occurrence of a Digit from a Given Range in Python
- Program to find out the number of special numbers in a given range in Python
- Program to find sum of the 2 power sum of all subarray sums of a given array in Python
- Program to find out the length between two cities in shortcuts in Python
- Write a program in C++ to find the length of the largest subarray with zero sum
- Program to find out number of distinct substrings in a given string in python
- Program to find out special types of subgraphs in a given graph in Python
- Python Program to find out the number of sets greater than a given value
- Program to find maximum score of a good subarray in Python
- Program to find length of longest arithmetic subsequence of a given list in Python

Advertisements