Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

C function to find the longest increasing subsequence without arrays

scaryguy7

New Coder
Hello
I kinda stuck right now
I need to make a program in C with those instructions: the problem is its kinda easy to do with arrays but without them,I cant think anything else.
we need to write a function that takes int n and then takes the sequences of numbers, the function need to return us the sum of the biggest sequence. for n = 8 and the sequence = 2,4,6,8,11,14,17,9' (there is 2 long sequences of 4, (when 8 starts the new sequence and closes the current sequence) here we will get the print 50.`
for n = 2 and the sequence 7,19 the function will print 26. for n = 8 and the sequence 8,6,4,2,11,14,15,16 the longest length is 4 so the function will print 20.
can't find any solution here without arrays


here what i did so far but its not working

C:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int
main(void)
{
    int d1, d2;
    // get count of sequence numbers
    int n;
    scanf("%d", &n);

    // length of current sequence
    int curlen = 0;

    // sum of current sequence
    int cursum = 0;

    // value of current number
    int curval;

    // value of previous number
    int oldval;

    // get the first number
    scanf("%d", &oldval);

    // add it to the current sequence
    curlen += 1;
    cursum += oldval;

    // initialize the best sequence from the current one
    int bestsum = cursum;
    int bestlen = curlen;

    // get all remaining numbers in the sequence
    for (int i = 1; i < n; ++i, oldval = curval) {
        // get new current number
        if (scanf("%d", &curval) != 1) {
            fprintf(stderr, "too short\n");
            break;
        }
        d1 = curval - oldval;
        scanf("%d", &oldval);
        d2 = oldval - curval;

        // reset the current sequence if we're not increasing
        if (d1 != d2)
        {
            cursum = 0;
            curlen = 0;
        }

        // add to the current sequence
        cursum += curval;
        curlen += 1;

        // save off the longest sequence we've seen
        if (curlen > bestlen) {
            bestlen = curlen;
            bestsum = cursum;
            continue;
        }

        // save off a sequence that is the same length as the best but has a
        // larger sum
        if ((curlen == bestlen) && (cursum > bestsum)) {
            bestsum = cursum;
            continue;
        }
    }

    // print the final/best sum
    printf("%d\n", bestsum);

    return 0;
}


can you suggest me something?
 
You need to define the exact meaning of sequence. That term is way too generic and your explanations don't help. For example why is 8,6,4,2 a sequence but 11,14,15,16 isn't ? And what is meant with "the biggest sequence" ? Is that the one with the most numbers, or the one with the largest sum of digits ?
And if you say your code "isn't working" what exactly do you mean ?

You see how important is is to define your problem concisely...

If you say it's easy to to with arrays, why don't you use them ?
 

New Threads

Buy us a coffee!

Back
Top Bottom