0

There is an exercise that asks for the biggest sub-array inside one specific array, and the sub-array should not have repeated elements.

I do not have to worry about complexity and efficiency right now

The array is [8,2,2,3,4,1,6,5,1,7]

As far as i understand, the biggest subarray without repeated elements is: [2,3,4,1,6,5,7]

But they say that my function should output the [2,3,4,1,6,5]

Why?

This is my code:

int existe(int chk, int v[], int tam){
    int i;

    for(i=0; i<tam; i++){
        if(v[i]==chk){
            return 1;
        }
    }

    return 0;
}

int removeb(int v[], int n){
    int i, max, j;
    max = 0; 

    for(i=0; i<n; i++){
        if(v[i]>max){
            max = v[i];
        }
    }
    for(i=0; i<n; i++){
        if(v[i]==max){
            j=i;
            while(j<n){
                v[j]=v[j+1];
                j++;
            }
            return j-1;
        }
    }
    return n;
}


int maxuniqueseq(int v[], int N){
    int i, j, newsize;
    int tmp[N];

    //newsize = removeb(v, N);
    printf("%d\n", N);

    for(i=0; i<N; i++){
        tmp[i] = v[i];
    }
    for(i=0, j=0; i<N; i++){
        if(existe(tmp[i],v, i)){
            ;
        }else{
            v[j]=tmp[i];
            j++;
        }
    }

    newsize = removeb(v, j);


    return newsize;
}

Output with the above array:

[2,3,4,1,6,5,7]

With another example:

[8,2,2,3,4,12,6,5,1,7]

outputs: [8,2,3,4,6,5,1,7]

5
  • 2
    If you think [2,3,4,1,6,5,7] is the biggest subarray then what happened to the 1 between 5 and 7 which is there in the original array ? Commented Jun 28, 2015 at 19:11
  • 2
    You would be needing biggest contiguous subarray. Else for biggest subarray it would be [8,2,3,4,1,6,5,7]. In either case your answer is wrong. Commented Jun 28, 2015 at 19:12
  • as i understand it, if i have [1,2,3,4,5], the biggest subarray is [1,2,3,4] Commented Jun 28, 2015 at 19:13
  • 1
    @skills why would it be so? why poor 5 is not good enough? Commented Jun 28, 2015 at 19:16
  • what about 5, it should be [1,2,3,4,5]. You need biggest contiguous subarray, it can be array itself. Commented Jun 28, 2015 at 19:17

1 Answer 1

2

I think the problem is with your interpretation of sub-array.

This is what a sub-array is

A subarray is commonly defined as a part or section of an array.

Hence the elements you are selecting need to be contiguous.

With an example,

suppose [10, 20, 50, 15, 10, 25, 45] is the array,

a sub-array for this would be [10, 20, 50]

where as [10, 50, 25] can not be called as a sub-array.

And the biggest sub-array without repeated elements would be [ 20, 50, 15, 10, 25, 45]

So change your code and give another try!

Sign up to request clarification or add additional context in comments.

6 Comments

What does contiguous mean? English is not my language. Can you give me an example of an array and a subarray and explain why is that a sub-array? Thank you so much .
@skills contiguous, means all adjacent and continuous elements.
@skills It would be useful if you write in ypur profile where you are from.:)
@RakholiyaJenish but they do not speak about contiguous in the exercise, the answer [2,3,4,1,6,5] has elements that are not adjacent and continuous, after 1, why 6?
@skills in the original array, 6 is right next to 1
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.