I'm currently doing an algorithm that given a sequence, checks if there is a subsequence which sum equals a given value. If I have:
3 8
10
5
1
7 5
1
2
3
4
5
6
7
0 0
where 3 8 and 7 5 are the (sequence size, value) and 0 0 tells us we reached the end. In this case it would print :
SUBSEQUENCE NOT FOUND
SUBSEQUENCE FOUND AT POSITION 2
My question is, why am I getting a Time Limit Exceeded when I submit it to Mooshak? Here goes the code:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
int main(){
int tamanho;
int valor;
int soma, numero;
int sequencia[100];
while(1){
scanf("%d %d", &tamanho, &valor);
if(tamanho != 0 && valor != 0){
for(int i = 1; i <= tamanho; i++){
scanf("%d", &numero);
printf("%d\n", i);
sequencia[i] = numero;
printf("%d\n", i);
}
for(int i = 1; i < tamanho; i++){
printf("i");
for(int j = i; j < tamanho; j++){
soma = 0;
printf("j");
for (int z = i; z < j; z++){
printf("z");
soma = soma + sequencia[z];
}
if(soma == valor){
printf("SUBSEQUENCIA NA POSICAO %d \n", i);
exit(0);
}
}
}
printf("SUBSEQUENCIA NAO ENCONTRADA\n");
}
}
return 0;
}