-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlongest-subarray.c
More file actions
114 lines (99 loc) · 3.02 KB
/
longest-subarray.c
File metadata and controls
114 lines (99 loc) · 3.02 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <stdio.h>
#include <stdlib.h>
/*
* Complete the 'longestSubarray' function below.
*
* The function is expected to return an INTEGER.
* The function accepts INTEGER_ARRAY arr as parameter.
*/
/*
* Algorithm:
* 1. For each possible starting position:
* a. Try to form a subarray with at most 2 distinct values
* b. Additionally, the two values must differ by at most 1
* 2. Use a window expansion approach:
* a. Track distinct values seen so far (at most 2)
* b. When a new value is encountered, check if it can be added:
* i. If we have 0 values so far, add it
* ii. If we have 1 value so far, add it if |new_val - existing_val| ≤ 1
* iii. If we have 2 values already, check if the new value matches either
* c. If a value can't be added, the window can't be expanded further
* 3. Track the maximum valid window length
*
* Time Complexity: O(n²) where n is the array length
* Space Complexity: O(1) additional space
*/
int longestSubarray(int arr_count, int *arr)
{
int ans = 0;
// O(n^2) is okay because of constraints.
for (int i = 0; i < arr_count; i++)
{
int w[2] = {-1, -1}; // Stores the two distinct values (-1 means empty)
int w_count = 0; // Number of distinct values stored
int cnt = 0; // Length of current subarray
for (int j = i; j < arr_count; j++)
{
// Check if current element matches either of the two values
if (w_count > 0 && arr[j] == w[0])
{
cnt++;
continue;
}
if (w_count > 1 && arr[j] == w[1])
{
cnt++;
continue;
}
// If we don't have any value yet, add this as first
if (w_count == 0)
{
w[0] = arr[j];
w_count = 1;
cnt++;
}
// If we have only one value, check if new value can be added
else if (w_count == 1)
{
if (abs(w[0] - arr[j]) <= 1)
{
w[1] = arr[j];
w_count = 2;
cnt++;
}
else
{
// If difference > 1, we can't add this element
break;
}
}
// If we already have two values, and this is a third distinct value, break
else
{
break;
}
}
// Update answer with max subarray length found
if (cnt > ans)
{
ans = cnt;
}
}
return ans;
}
int main()
{
FILE *fptr = fopen(getenv("OUTPUT_PATH"), "w");
int arr_count;
scanf("%d", &arr_count);
int *arr = malloc(arr_count * sizeof(int));
for (int i = 0; i < arr_count; i++)
{
scanf("%d", &arr[i]);
}
int result = longestSubarray(arr_count, arr);
fprintf(fptr, "%d\n", result);
free(arr);
fclose(fptr);
return 0;
}