#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

typedef struct hash_table_node {
    int data;
    int index;
    struct hash_table_node* next;
} HashTableNode;

typedef struct hash_table {
    HashTableNode** buckets;
    int capacity;
    int size;
} HashTable;

HashTable* hash_create(int capacity) {
    HashTable* ht = (HashTable*)malloc(sizeof(HashTable));
    ht->capacity = capacity;
    ht->size = 0;
    ht->buckets = (HashTableNode*)calloc(capacity, sizeof(HashTableNode));
    return ht;
}

int hash_func(const HashTable* ht, int value) {
    return ((value % ht->capacity) + ht->capacity) % ht->capacity;
}

bool hash_insert(HashTable* ht, int value, int index) {
    int hash_value = hash_func(ht, value);
    HashTableNode* node = ht->buckets[hash_value];
    while (node != NULL) {
        if (node->data == value)
            return false;
        node = node->next;
    }

    HashTableNode* new_node = (HashTableNode*)malloc(sizeof(HashTableNode));
    new_node->data = value;
    new_node->index = index;
    new_node->next = ht->buckets[hash_value];
    ht->buckets[hash_value] = new_node;
    ht->size++;
    return true;
}

bool hash_remove(HashTable* ht, int value) {
    int hash_value = hash_func(ht, value);
    HashTableNode** node_ptr = &(ht->buckets[hash_value]);
    while (*node_ptr != NULL) {
        if ((*node_ptr)->data == value) {
            HashTableNode* temp = *node_ptr;
            *node_ptr = (*node_ptr)->next;
            free(temp);
            ht->size--;
            return true;
        }
        node_ptr = &((*node_ptr)->next);
    }

    return false;
}

bool hash_exists(const HashTable* ht, int value) {
    int hash_value = hash_func(ht, value);
    HashTableNode* node = ht->buckets[hash_value];
    while (node != NULL) {
        if (node->data == value)
            return true;
        node = node->next;
    }
    return false;
}

void hash_destroy(HashTable* ht) {
    for (int i = 0; i < ht->capacity; i++) {
        HashTableNode* node = ht->buckets[i];
        while (node != NULL) {
            HashTableNode* temp = node;
            node = node->next;
            free(temp);
        }
    }
    free(ht->buckets);
    free(ht);
}

void findLongestSubsequence(int n, int* book) {
    HashTable* ht = hash_create(n);
    int maxLength = 0;
    int maxIndex = -1;

    for (int i = 0; i < n; i++) {
        hash_insert(ht, book[i], i);
    }

    for (int i = 0; i < n; i++) {
        int currentLength = 0;
        int currentIndex = -1;
        int x = book[i];

        while (hash_exists(ht, x)) {
            currentLength++;
            if (currentIndex == -1)
                currentIndex = hash_func(ht, x);
            hash_remove(ht, x);
            x++;
        }

        if (currentLength > maxLength) {
            maxLength = currentLength;
            maxIndex = currentIndex;
        }
    }

    printf("Length of longest subsequence: %d\n", maxLength);
    printf("Index of occurrence: %d\n", maxIndex);

    hash_destroy(ht);
}

int main() {
    int n;
    scanf("%d", &n);
    int* book = (int*)malloc(n * sizeof(int));
    for (int i = 0; i < n; i++) {
        scanf("%d", &book[i]);
    }
    findLongestSubsequence(n, book);

    free(book);
    return 0;
}



its not working properly 
input 
9
1 9 8 7 2 3 6 4 5
output 5
Index 1