#Longest subsequence of maximum 3 consecutive distinct elements in an array

9 messages · Page 1 of 1 (latest)

lapis pendantBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

fading moon
#

There is not enough information here. Where does the 6 7 come from? Fix what? There is no algorithm to fix here. I feel like you wrote an attempted solution that gives the wrong answer in some cases but forgot to post it.

summer grove
lapis pendantBOT
#
How to Format Code on Discord
Markup

```cpp
int main() {}
```

Result
int main() {} ```
summer grove
#
#include <iostream>

using namespace std;

bool all_elements_are_distinct(int arr[], int poz_i, int poz_f){
    for (int i = poz_i; i < poz_f; i++)
    for (int j = i+1; j < poz_f+1; j++) {
            if (arr[i]==arr[j]) return false;}
    return true;
}
void get_longest_seq2(int arr[], int n){
    int currLen = 1; int currIdx = 0; int maxLen = 1; int maxIdx = 0;
    for (int i = 0; i < n; i++)
    for (int j = i+1; j < n; j++) {
        if (all_elements_are_distinct(arr, i, j)==true)
        {
            currLen = j-i+1;
            currIdx = i;
        }
        else if ((currLen>maxLen) && (currLen <=3)){
            maxLen = currLen;
            maxIdx = currIdx;
            currLen = 1;
        }
    }
        if (maxLen > 1){
    for (int i = 0; i < maxLen; i++)
        {
        cout << arr[maxIdx] << " ";
        maxIdx++;
        }
cout << "\n";
}
    else
        cout << "There in no subsequence with this property." << endl;
}
int main()
{
    int arr[] = {3,4,5,6,6,7};
    get_longest_seq2(arr,6);
    return 0;
}

fading moon
#

It's not easy to follow this and figure out what all the variables are for. I'd make a helper function that calculates the distinct subsequence sum of 3 elements and then iterate over the range.

#

I don't think all_elements_are_distinct helps as much as you think. I'm fairly sure you can make an example where duplicates are required. 1, 1, 1 and 3,4,5,0,0,1000,0,0,6,6,7 should be added to your tests.

#

And maybe also negative numbers if they are allowed.

lapis pendantBOT
#

@summer grove

Please Do Not Delete Posts!

Please don't delete forum posts. They can be helpful to refer to later and other members can learn from them. In the future you can use !solved to close a post and mark a post as solved.