Created
March 24, 2024 01:42
-
-
Save megarubber/e2ab02576898d261b8c51f07198b0e1b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#define MAX 105 | |
void sort(int *array, int k) { | |
int* ord = (int*)malloc(k * sizeof(int)); | |
int i = 0; | |
int largest = array[0]; | |
for(i = 1; i < k; i++) { | |
if(array[i] > largest) | |
largest = array[i]; | |
} | |
int* aux = (int*)malloc(largest * sizeof(int)); | |
for(i = 0; i <= largest; i++) | |
aux[i] = 0; | |
for(i = 0; i < k; i++) | |
aux[array[i]]++; | |
for(i = 1; i <= largest; i++) | |
aux[i] += aux[i - 1]; | |
for(i = k - 1; i >= 0; i--) { | |
ord[aux[array[i]] - 1] = array[i]; | |
aux[array[i]]--; | |
} | |
for(i = 0; i < k; i++) | |
array[i] = ord[i]; | |
} | |
int longest_sequence(int* array, int k) { | |
sort(array, k); | |
int i = 0, sequence = 0, maxsequence = 0; | |
for(i = 0; i < k - 1; i++) { | |
int test_next = array[i] + 1; | |
if(array[i + 1] == test_next) | |
sequence++; | |
else | |
sequence = 0; | |
if(sequence > maxsequence) | |
maxsequence = sequence; | |
} | |
return maxsequence + 1; | |
} | |
int main() { | |
int* nums = (int*)malloc(sizeof(int) * MAX); | |
int n, i; | |
scanf("%d", &n); | |
if(n > MAX) return -1; | |
for(i = 0; i < n; i++) { | |
scanf("%d", &nums[i]); | |
if(nums[i] > 109 || nums[i] < -109) return -1; | |
} | |
printf("%d\n", longest_sequence(nums, n)); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment