Given a sequence of n positive integers, find a subsequence of length k such that the sum over the absolute values of the differences is maximal

d125q

We are given a sequence of n positive integers, which I will denote as a0, a1, …, an-1. We are also given an integer k, and our task is to:

  1. find a subsequence of length exactly k (denoted as b0, b1, …, bk-1), such that abs(b1 - b0) + abs(b2 - b1) + … + abs(bk-1 - bk-2) is maximal; and

  2. output the sum (no need to output the entire subsequence).

I have been trying to solve this using a dynamic programming approach but all my efforts have been futile.

EDIT: k <= n. The elements in the sequence b must appear in the same order as they appear in a (otherwise, this would be solved by simply finding max, min, ... or min, max, ...).

Example input:

n = 10
k = 3
1 9 2 3 6 1 3 2 1 3

Output:

16 (the subsequence is 1 9 1, and abs(9 - 1) + abs(1 - 9) = 8 + 8 = 16)

Any help / hints would be greatly appreciated.

d125q

I managed to solve this problem. Here's the full code:

#include <stdio.h>
#include <stdlib.h>

int abs(int a)
{
    return (a < 0) ? -a : a;
}

int solve(int *numbers, int N, int K)
{
    int **storage = malloc(sizeof(int *) * N);
    int i, j, k;
    int result = 0;

    for (i = 0; i < N; ++i)
        *(storage + i) = calloc(K, sizeof(int));

    // storage[i][j] keeps the optimal result where j + 1 elements are taken (K = j + 1) with numbers[i] appearing as the last element.

    for (i = 1; i < N; ++i) {
        for (j = 1; j < K; ++j) {
            for (k = j - 1; k < i; ++k) {
                if (storage[i][j] < storage[k][j - 1] + abs(numbers[k] - numbers[i]))
                    storage[i][j] = storage[k][j - 1] + abs(numbers[k] - numbers[i]);

                if (j == K - 1 && result < storage[i][j])
                    result = storage[i][j];
            }
        }
    }

    for (i = 0; i < N; ++i)
        free(*(storage + i));

    free(storage);
    return result;
}

int main()
{
    int N, K;
    scanf("%d %d", &N, &K);
    int *numbers = malloc(sizeof(int) * N);
    int i;

    for (i = 0; i < N; ++i)
        scanf("%d", numbers + i);

    printf("%d\n", solve(numbers, N, K));

    return 0;
}

The idea is simple (thanks to a friend of mine for hinting me at it). As mentioned in the comment, storage[i][j] keeps the optimal result where j + 1 elements are taken (K = j + 1) with numbers[i] appearing as the last element. Then, we simply try out each element appearing as the last one, taking each possible number of 1, 2, ..., K elements out of all. This solution works in O(k * n^2).

I first tried a 0-1 Knapsack approach where I kept the last element I had taken in each [i][j] index. This solution did not give a correct result in just a single test case, but it worked in O(k * n). I think I can see where it would yield a suboptimal solution, but if anyone is interested, I can post that code, too (it is rather messy though).

The code posted here passed on all test cases (if you can detect some possible errors, feel free to state them).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Given a sequence of n positive integers, find a subsequence of length k such that the sum over the absolute values of the differences is maximal

From Dev

Find all M-length sets of positive integers that sum to N

From Dev

Given a set of positive integers <=k and its n subsets , find union of which pairs of subsets give the original set

From Dev

Find all k-size subsets with sum s of an n-size bag of duplicate unsorted positive integers

From Dev

Find k out of n subset with maximal area

From Dev

Can a given number be written as a sum of two or more consecutive positive integers?

From Dev

Given a 2D matrix of size MxN with positive integer values, find the closed loop that has the maximum sum

From Dev

Find the subsequence which, once repeated, generate a given sequence

From Dev

Find Maximal values that can be acquired from N

From Dev

Length of the longest subsequence in a list of integers

From Java

Fastest Algorithm to Find the Minimum Sum of Absolute Differences through List Rotation

From Dev

How to find SAD(sum-of-absolute-differences) in list haskell

From Dev

Find Maximum sum in a path in a 2D matrix with positive integers

From Dev

Find sum of 5 positive integers (one input) without looping

From Dev

How to iterate over array of integers to find a sequence based on an O(N) solution?

From Dev

How to find the maximal (longest) sequence of increasing elements in an array arr[n]?

From Dev

Find the smallest positive integer that does not occur in a given sequence

From Dev

Given a list of n integers , find the minimum subset sum greater than X

From Dev

Given n and k, return the kth permutation sequence

From Dev

computes the number of ways to partition n into the sum of positive integers c++

From Dev

Generate all possible 3 positive integers that sum to N

From Dev

Given a set of n integers, list all possible subsets with k1<= sum <=k2 , k1 and k2 floating numbers

From Dev

For a given sequence of n numbers, find the substring with a maximum total sum with the lowest computational complexity as possible

From Dev

Sum a sequence of integers in a vector

From Dev

Sum a sequence of integers in a vector

From Dev

sum of absolute differences of a number in an array

From Dev

Find the minimum set of integers whose sum is greater than a given integer

From Dev

Find Pair Of Integers in Array whose Sum is Given Number in PHP

From Java

find the subset of maximal sum in which the distance between consecutive elements is at most 6 from a given array

Related Related

  1. 1

    Given a sequence of n positive integers, find a subsequence of length k such that the sum over the absolute values of the differences is maximal

  2. 2

    Find all M-length sets of positive integers that sum to N

  3. 3

    Given a set of positive integers <=k and its n subsets , find union of which pairs of subsets give the original set

  4. 4

    Find all k-size subsets with sum s of an n-size bag of duplicate unsorted positive integers

  5. 5

    Find k out of n subset with maximal area

  6. 6

    Can a given number be written as a sum of two or more consecutive positive integers?

  7. 7

    Given a 2D matrix of size MxN with positive integer values, find the closed loop that has the maximum sum

  8. 8

    Find the subsequence which, once repeated, generate a given sequence

  9. 9

    Find Maximal values that can be acquired from N

  10. 10

    Length of the longest subsequence in a list of integers

  11. 11

    Fastest Algorithm to Find the Minimum Sum of Absolute Differences through List Rotation

  12. 12

    How to find SAD(sum-of-absolute-differences) in list haskell

  13. 13

    Find Maximum sum in a path in a 2D matrix with positive integers

  14. 14

    Find sum of 5 positive integers (one input) without looping

  15. 15

    How to iterate over array of integers to find a sequence based on an O(N) solution?

  16. 16

    How to find the maximal (longest) sequence of increasing elements in an array arr[n]?

  17. 17

    Find the smallest positive integer that does not occur in a given sequence

  18. 18

    Given a list of n integers , find the minimum subset sum greater than X

  19. 19

    Given n and k, return the kth permutation sequence

  20. 20

    computes the number of ways to partition n into the sum of positive integers c++

  21. 21

    Generate all possible 3 positive integers that sum to N

  22. 22

    Given a set of n integers, list all possible subsets with k1<= sum <=k2 , k1 and k2 floating numbers

  23. 23

    For a given sequence of n numbers, find the substring with a maximum total sum with the lowest computational complexity as possible

  24. 24

    Sum a sequence of integers in a vector

  25. 25

    Sum a sequence of integers in a vector

  26. 26

    sum of absolute differences of a number in an array

  27. 27

    Find the minimum set of integers whose sum is greater than a given integer

  28. 28

    Find Pair Of Integers in Array whose Sum is Given Number in PHP

  29. 29

    find the subset of maximal sum in which the distance between consecutive elements is at most 6 from a given array

HotTag

Archive