Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

fix commented code; add clarifying comment
Source Link
c--
  • 2.6k
  • 1
  • 7
  • 15

C (clang), 76 bytes

Takes a list of digits and a number as input and returns \$[start, end)\$.

i;j;f(*l,s,n){for(i=j=0;n<0|s/~j*n;)n-=n>0?l[j++]:-l[i++];l[1]=j;*l=n?-1:i;}

How it works

i;j; // start and end of the sliding window, respectively.
f(*s*l,l s,n) { // input a list of integers, its size and the number.
 for( // main loop
 i = j = 0; // start with i = 0 and j = 0 (empty window)
 n < 0 | // the sum of elements in the window exceeds n, we will remove elements in the next iteration
 s / ~j * n;) // j < l and the sum of elements does not match n
 n -= // subtract from n:
 n > 0 ? // if n > 0 ...
 l[j++] // increase the window size.
 : // else (n < 0) ...
 -l[i++] // decrease the window size.
 ; // end main loop
 l[1] = j; // l doubles as return placeholder. l[1] is the end of the window.
 *l = // l[0] is:
 n ? // if n != 0 ...
 -1 // there was no solution, set it to -1.
 : // else (n == 0) ...
 i; // we found the solution, set it to the start of the window.
}

Try it online!

C (clang), 76 bytes

Takes a list of digits and a number as input and returns \$[start, end)\$.

i;j;f(*l,s,n){for(i=j=0;n<0|s/~j*n;)n-=n>0?l[j++]:-l[i++];l[1]=j;*l=n?-1:i;}

How it works

i;j; // start and end of the sliding window, respectively.
f(*s,l,n) { // input a list of integers, its size and the number.
 for( // main loop
 i = j = 0; // start with i = 0 and j = 0
 n < 0 | // the sum of elements in the window exceeds n, we will remove elements in the next iteration
 s / ~j * n;) // j < l and the sum of elements does not match n
 n -= // subtract from n:
 n > 0 ? // if n > 0 ...
 l[j++] // increase the window size.
 : // else (n < 0) ...
 -l[i++] // decrease the window size.
 ; // end main loop
 l[1] = j; // l doubles as return placeholder. l[1] is the end of the window.
 *l = // l[0] is:
 n ? // if n != 0 ...
 -1 // there was no solution, set it to -1.
 : // else (n == 0) ...
 i; // we found the solution, set it to the start of the window.
}

Try it online!

C (clang), 76 bytes

Takes a list of digits and a number as input and returns \$[start, end)\$.

i;j;f(*l,s,n){for(i=j=0;n<0|s/~j*n;)n-=n>0?l[j++]:-l[i++];l[1]=j;*l=n?-1:i;}

How it works

i;j; // start and end of the sliding window, respectively.
f(*l, s,n) { // input a list of integers, its size and the number.
 for( // main loop
 i = j = 0; // start with i = 0 and j = 0 (empty window)
 n < 0 | // the sum of elements in the window exceeds n, we will remove elements in the next iteration
 s / ~j * n;) // j < l and the sum of elements does not match n
 n -= // subtract from n:
 n > 0 ? // if n > 0 ...
 l[j++] // increase the window size.
 : // else (n < 0) ...
 -l[i++] // decrease the window size.
 ; // end main loop
 l[1] = j; // l doubles as return placeholder. l[1] is the end of the window.
 *l = // l[0] is:
 n ? // if n != 0 ...
 -1 // there was no solution, set it to -1.
 : // else (n == 0) ...
 i; // we found the solution, set it to the start of the window.
}

Try it online!

add explanation
Source Link
c--
  • 2.6k
  • 1
  • 7
  • 15

C (clang), 76 bytes

ReturnsTakes a list of digits and a number as input and returns \$[start, end)\$.

i;j;f(*s*l,ls,n){for(i=j=0;n<0|li=j=0;n<0|s/~j*n;)n-=n>0?s[j++]l[j++]:-s[i++];s[1]=j;*s=nl[i++];l[1]=j;*l=n?-1:i;}

How it works

i;j; // start and end of the sliding window, respectively.
f(*s,l,n) { // input a list of integers, its size and the number.
 for( // main loop
 i = j = 0; // start with i = 0 and j = 0
 n < 0 | // the sum of elements in the window exceeds n, we will remove elements in the next iteration
 s / ~j * n;) // j < l and the sum of elements does not match n
 n -= // subtract from n:
 n > 0 ? // if n > 0 ...
 l[j++] // increase the window size.
 : // else (n < 0) ...
 -l[i++] // decrease the window size.
 ; // end main loop
 l[1] = j; // l doubles as return placeholder. l[1] is the end of the window.
 *l = // l[0] is:
 n ? // if n != 0 ...
 -1 // there was no solution, set it to -1.
 : // else (n == 0) ...
 i; // we found the solution, set it to the start of the window.
}

Try it online! Try it online!

C (clang), 76 bytes

Returns \$[start, end)\$.

i;j;f(*s,l,n){for(i=j=0;n<0|l/~j*n;)n-=n>0?s[j++]:-s[i++];s[1]=j;*s=n?-1:i;}

Try it online!

C (clang), 76 bytes

Takes a list of digits and a number as input and returns \$[start, end)\$.

i;j;f(*l,s,n){for(i=j=0;n<0|s/~j*n;)n-=n>0?l[j++]:-l[i++];l[1]=j;*l=n?-1:i;}

How it works

i;j; // start and end of the sliding window, respectively.
f(*s,l,n) { // input a list of integers, its size and the number.
 for( // main loop
 i = j = 0; // start with i = 0 and j = 0
 n < 0 | // the sum of elements in the window exceeds n, we will remove elements in the next iteration
 s / ~j * n;) // j < l and the sum of elements does not match n
 n -= // subtract from n:
 n > 0 ? // if n > 0 ...
 l[j++] // increase the window size.
 : // else (n < 0) ...
 -l[i++] // decrease the window size.
 ; // end main loop
 l[1] = j; // l doubles as return placeholder. l[1] is the end of the window.
 *l = // l[0] is:
 n ? // if n != 0 ...
 -1 // there was no solution, set it to -1.
 : // else (n == 0) ...
 i; // we found the solution, set it to the start of the window.
}

Try it online!

golf
Source Link
c--
  • 2.6k
  • 1
  • 7
  • 15

C (clang), 7976 bytes

Returns \$[start, end)\$.

i;j;f(*s,l,n){for(i=j=0;n<0|n*s[j];i=j=0;n<0|l/~j*n;)n-=n>0?s[j++]-48:48-s[i++];s[1]=j;*s=n?-1:i;}

Try it online! Try it online!

C (clang), 79 bytes

Returns \$[start, end)\$.

i;j;f(*s,n){for(i=j=0;n<0|n*s[j];)n-=n>0?s[j++]-48:48-s[i++];s[1]=j;*s=n?-1:i;}

Try it online!

C (clang), 76 bytes

Returns \$[start, end)\$.

i;j;f(*s,l,n){for(i=j=0;n<0|l/~j*n;)n-=n>0?s[j++]:-s[i++];s[1]=j;*s=n?-1:i;}

Try it online!

edit test case
Source Link
c--
  • 2.6k
  • 1
  • 7
  • 15
Loading
Source Link
c--
  • 2.6k
  • 1
  • 7
  • 15
Loading

AltStyle によって変換されたページ (->オリジナル) /