JohnnyCashh

Length of the largest subarray with contiguous elements

Sep 19th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.55 KB | None | 0 0
  1. def findLength(arr, n):
  2.     maxLen = 1
  3.     i = 0
  4.     while i < n - 1:
  5.         minn = arr[i]
  6.         maxx = arr[i]
  7.         j = i + 1
  8.         while j < n:
  9.             minn = min(minn, arr[j])
  10.             maxx = max(maxx, arr[j])
  11.             if maxx - minn == j - i:
  12.                 maxLen = max(maxLen, maxx - minn + 1)
  13.             j = j + 1
  14.         i = i + 1
  15.     return maxLen
  16.  
  17.  
  18. n = int(input("Number of elements: "))
  19. arr = list(map(int, input().split(' ')))[:n]
  20.  
  21. print("Length of the longest contiguous subarray is %s" % findLength(arr, n))
Advertisement
Add Comment
Please, Sign In to add comment