Longest Subarray - Problem Solving (Basic) certification | HackerRank


Solution in Python:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/bin/python3

import math
import os
import random
import re
import sys


#
# Complete the 'longestSubarray' function below.
#
# The function is expected to return an INTEGER.
# The function accepts INTEGER_ARRAY arr as parameter.
#

def longestSubarray(arr):
    n = len(arr)
    ans = 0
    # O(n^2) is okay because of constraints.
    for i in range(n):
        w = []
        cnt = 0
        for j in range(i, n):
            if arr[j] in w:
                cnt += 1
                continue
            if len(w) == 0:
                w.append(arr[j])
            elif len(w) == 1:
                if abs(w[0] - arr[j]) > 1:
                    break
                else:
                    w.append(arr[j])
            else:
                break
            cnt += 1
        ans = max(ans, cnt)
    return ans
    
if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    arr_count = int(input().strip())

    arr = []

    for _ in range(arr_count):
        arr_item = int(input().strip())
        arr.append(arr_item)

    result = longestSubarray(arr)

    fptr.write(str(result) + '\n')

    fptr.close()
Labels : #hackerrank ,#hackerrank certification ,#problem solving ,#problem solving (Basic) ,#python ,

Post a Comment