Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>So you only need to print the length of the list, not the list itself? That helps, because it simplifies memory management (which is an issue for c).</p> <p>This is really just a glorified search. At each point you need to know (1) the current value (if any); (2) the current length (if any); (3) the position in the list; (4) the largest length known. Using a length of zero helps track when you have no current value (so you don't need a "magic" initial value). The return value should be the largest length.</p> <p>On each recursion you can either skip the current number or include it in the list (but only if it is less than the current value).</p> <p>so the code is just:</p> <pre> <code>#include &lt;stdio.h&gt; int max(int a, int b) { return a > b ? a : b; } int find(int* data, int total_length, int offset, int previous, int run_length, int max_length) { // update max length if it has improved if (run_length &gt; max_length) max_length = run_length; // if we are at the end, return max if (offset == total_length) return max_length; // if the current value is too small, we cannot include it if (run_length && data[offset] &lt;= previous) return find(data, total_length, offset+1, previous, run_length, max_length); // otherwise, we want the best of either including it or not return max( // try including it find(data, total_length, offset+1, data[offset], run_length+1, max_length), // try excluding it find(data, total_length, offset+1, previous, run_length, max_length)); } // test int main(int argc, char *argv) { int data[] = { 88, 1, 22, 3, 34, 6, 54, 9, 19 }; printf("%d\n", find(data, 9, 0, 0, 0, 0)); return 0; } </code> </pre> <p>obviously this is terrible c code (not because I didn't use array initialization, which someone "kindly" fixed (although they didn't bother to vote this answer - I guess they think I am motivated to post here by learning from their deep knowledge of C syntax) but because it uses the call stack where it would be more efficient to use a separate memory structure).</p> <p>(also, I have to say, writing something like this is much easier as a recursive function than as doing it in a loop, because you just write down what you want to happen - if you had a loop then you would need to worry about changing values and resetting them and all that mess. The problem is that it abuses the stack horribly).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload