Apologies for the confusion. The provided solution had some issues. Here is a revised version using a hash table for efficient lookups, which will handle positive as well as negative integers properly and print the indices of the longest sequence of continuous numbers:
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int value; // The value in the original array
int index; // The original index in the array
} HashItem;
int hash(int value) {
return value + 100000; // Offset for potential negative values
}
void findLongestSubsequence(int *arr, int n) {
HashItem hashTable[200001] = {{0,0}}; // Assuming the numbers are in the range [-100000, 100000]
int startValue = 0, maxLength = 1, currentLength, currentStart;
int *indices = (int *)malloc(n * sizeof(int));
int idxLen = 0;
// Build the hash table
for (int i = 0; i < n; ++i) {
int h = hash(arr[i]);
hashTable[h].value = arr[i];
hashTable[h].index = i + 1;
}
// Find the longest sequence
for (int i = 0; i < n; ++i) {
int number = arr[i];
int h = hash(number);
if (hashTable[h].index) { // We haven't seen this number yet
// We will look for the start of the sequence
currentLength = 1;
currentStart = number;
// Go backwards
while (hashTable[hash(currentStart - 1)].index) {
--currentStart;
++currentLength;
}
// Check sequence length
if (currentLength >= maxLength) {
maxLength = currentLength;
startValue = currentStart;
idxLen = 0; // Reset the indices counter
}
// Mark all sequence numbers as visited
for (int j = 0; j < currentLength; ++j) {
hashTable[hash(currentStart + j)].index = 0;
}
}
}
// Collect the indices of the longest sequence from the array, reorder them in a new array
for (int i = 0, count = 0; i < n && count < maxLength; ++i) {
if (arr[i] >= startValue && arr[i] < startValue + maxLength) {
indices[idxLen++] = i; // Save index (+1 because of 0-based indexing)
count++;
}
}
// Output the results
printf("%d\n", maxLength);
for (int i = 0; i < idxLen; ++i) {
printf("%d ", indices[i]); // The indices are 0-based
}
free(indices);
}
int main() {
int n;
scanf("%d", &n);
int *arr = (int *)malloc(n * sizeof(int));
for (int i = 0; i < n; ++i) {
scanf("%d", arr + i);
}
findLongestSubsequence(arr, n);
free(arr);
return 0;
}
This program starts by building a hash table to remember the positions of each number in the original array. It then scans the array, constructing subsequences from unvisited numbers, updating the starting value, and marking elements as visited along the way.
The hash function simple adds a large number (e.g., 100000) to the input value to ensure a non-negative hash result, given the assumption that input values could range from -100000 to 100000.
Once the longest subsequence is found as per the maxLength and its starting number startValue, we go through the original array to gather the indices of the elements that are part of this subsequence, and we print all these indices out.
Do note that the output indices are 0-based as per the C programming convention. If you expect the output indices to be 1-based (to start counting from index 1), you should increase the indices by 1 during the output.