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]
[2,3,4,1,6,5,7]is the biggest subarray then what happened to the1between5and7which is there in the original array ?