disk space analysis hackerrank solution
14-03-2023
Hit : 141
Here's a Python solution for the "Disk Space Analysis" problem on Hackerrank:
pythondef maxSubstring(n, k, s):
max_len = 0
curr_len = 0
for i in range(n):
if s[i] >= k:
curr_len += 1
else:
max_len = max(max_len, curr_len)
curr_len = 0
return max(max_len, curr_len)
n = int(input())
k = int(input())
s = list(map(int, input().split()))
print(maxSubstring(n, k, s))
The maxSubstring
function takes in the length of the array s
, the threshold value k
, and the array s
itself. It then loops through the array, keeping track of the current substring length (curr_len
) and the maximum substring length seen so far (max_len
). If the current element of the array is greater than or equal to k
, we increment curr_len
. Otherwise, we update max_len
to be the maximum of max_len
and curr_len
, and reset curr_len
to 0.
Finally, we print out the maximum substring length.
Note that we first convert the input string into a list of integers using map(int, input().split())
.