Thursday, 25 November 2021

Kefa And First Steps Problem !!!

 PROBLEM 7: Kefa and First Steps


Problem Reference : Codeforces 

Kefa decided to make some money doing business on the Internet for exactly n days. He knows that on the i-th day (1 ≤ i ≤ n) he makes ai money. Kefa loves progress, that's why he wants to know the length of the maximum non-decreasing subsegment in sequence ai. Let us remind you that the subsegment of the sequence is its continuous fragment. A subsegment of numbers is called non-decreasing if all numbers in it follow in the non-decreasing order.

Help Kefa cope with this task!


Input:

The first line contains integer n (1 ≤ n ≤ 105).

The second line contains n integers a1,  a2,  ...,  an (1 ≤ ai ≤ 109).



Output:

Print a single integer — the length of the maximum non-decreasing subsegment of sequence a.


Note:

In the first test the maximum non-decreasing subsegment is the numbers from the third to the fifth one.

In the second test the maximum non-decreasing subsegment is the numbers from the first to the third one.


Solution:


  1. n = int(input())
  2. l = []
  3. a = [int(x) for x in input().split()]
  4. c = 0
  5. c1 = 0
  6. j = 1
  7. for i in range(1,n):
  8. if a[i] >= a[i-1]:
  9. c+=1
  10. c1 = max(c1,c)
  11. else:
  12. c = 0
  13. if c1 > 0 or j == 1:
  14. print(c1+1)
  15. else:
  16. print(c1)

This above solution is in python language. 


Examples
input
6
2 2 1 3 4 1
output
3
input
3
2 2 9
output
3

No comments:

Post a Comment

Rearrange an array with O(1) extra space

  PROBLEM 61:  Rearrange an array with O(1) extra space (For best view experience, view in windows version) Problem Reference : GeeksForGeek...