Here's a C program that implements the solution for finding the longest incrementing subsequence. The C language doesn't have built-in hashtable structures, so we'll need to create our own. Since we'll be dealing with potentially large keys (up to 10^9), a straightforward array won't work. We could use a balanced tree for that, or in our particular case, where the input domain is limited, we can work around it by using a dynamic array or linked list. However, for this approach, we will create a simplified hashtable to keep track of sequences.
#include <stdio.h>
#include <stdlib.h>
#define MAX_BOOKS 200000
typedef struct LinkedListNode {
int index;
struct LinkedListNode *next;
} LinkedListNode;
// The hashtable will use open addressing for conflict resolution
typedef struct {
int number;
int index;
int sequenceLen;
struct LinkedListNode *head;
} HashTableItem;
typedef struct {
HashTableItem *table;
int size;
} HashTable;
HashTable* createHashTable(int size) {
HashTable *hashtable = (HashTable *)malloc(sizeof(HashTable));
hashtable->table = (HashTableItem *)malloc(sizeof(HashTableItem) * size);
hashtable->size = size;
for (int i = 0; i < size; i++) {
hashtable->table[i].number = -1; // Indicates an empty slot
}
return hashtable;
}
int hashFunction(int number, int size) {
// Simple modulus-based hash function
return abs(number) % size;
}
void insertHashTable(HashTable *hashtable, int number, int index) {
int hashIndex = hashFunction(number, hashtable->size);
while (hashtable->table[hashIndex].number != -1 && hashtable->table[hashIndex].number != number) {
hashIndex = (hashIndex + 1) % hashtable->size;
}
if (hashtable->table[hashIndex].number == -1) {
// New number, insert here
hashtable->table[hashIndex].number = number;
hashtable->table[hashIndex].sequenceLen = 1;
hashtable->table[hashIndex].index = index;
hashtable->table[hashIndex].head = (LinkedListNode *)malloc(sizeof(LinkedListNode));
hashtable->table[hashIndex].head->index = index;
hashtable->table[hashIndex].head->next = NULL;
} else {
// Number already in the hash table, need to update
hashtable->table[hashIndex].sequenceLen += 1;
LinkedListNode *temp = hashtable->table[hashIndex].head;
LinkedListNode *newNode = (LinkedListNode *)malloc(sizeof(LinkedListNode));
newNode->index = index;
newNode->next = NULL;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
}
HashTableItem* searchHashTable(HashTable *hashtable, int number) {
int hashIndex = hashFunction(number, hashtable->size);
while (hashtable->table[hashIndex].number != -1 && hashtable->table[hashIndex].number != number) {
hashIndex = (hashIndex + 1) % hashtable->size;
}
if (hashtable->table[hashIndex].number == number) {
return &(hashtable->table[hashIndex]);
} else {
return NULL;
}
}
void freeHashTable(HashTable *hashtable) {
for (int i = 0; i < hashtable->size; ++i) {
LinkedListNode *node = hashtable->table[i].head;
while (node != NULL) {
LinkedListNode *temp = node;
node = node->next;
free(temp);
}
}
free(hashtable->table);
free(hashtable);
}
void printLongestSubsequence(HashTable *hashtable, int maxLength, int startIndex) {
printf("Length of Longest Subsequence: %d\n", maxLength);
// Retrieve and print the start index and the subsequence
int number = hashtable->table[startIndex].number;
LinkedListNode *node = hashtable->table[startIndex].head;
while (maxLength-- && node != NULL) {
printf("Number: %d, Index: %d\n", number, node->index);
++number;
node = node->next;
}
}
void findLongestSubsequence(int *book, int n) {
int maxLength = 0;
int startIndex = -1;
HashTable *hashtable = createHashTable(n * 2);
for (int i = 0; i < n; ++i) {
insertHashTable(hashtable, book[i], i);
HashTableItem* prevItem = searchHashTable(hashtable, book[i] - 1);
// Check if the current number can be a continuation of a sequence
if (prevItem != NULL) {
HashTableItem* currItem = searchHashTable(hashtable, book[i]);
currItem->sequenceLen = prevItem->sequenceLen + 1;
currItem->index = prevItem->index; // Update index to start of subsequence
insertHashTable(hashtable, book[i], currItem->index); // Update other members
if (currItem->sequenceLen > maxLength) {
maxLength = currItem->sequenceLen;
startIndex = currItem->index;
}
} else if (maxLength == 0) {
// This is the first number in the sequence
maxLength = 1;
startIndex = i;
}
}
printLongestSubsequence(hashtable, maxLength, startIndex);
freeHashTable(hashtable);
}
int main() {
int book[MAX_BOOKS];
int n;
// Read the number of books
scanf("%d", &n);
// Read the book numbers
for (int i = 0; i < n; ++i) {
scanf("%d", &book[i]);
}
// Find and print the longest subsequence
findLongestSubsequence(book, n);
return 0;
}
Be aware that the above C code creates a hash table to track the starting indices of potential subsequences and lengths. The hash function is very simple and is not intended to handle large amounts of data or be highly efficient with load factors, as it's just for the purpose of illustration. It's also worth noting that due to the simplistic nature of the hash function and the linear probing collision resolution strategy, this implementation may perform poorly for large data sets with many hash collisions. In a real-world scenario, more sophisticated data structures and algorithms, such as AVL trees or Red-Black trees, might be used for better performance.