The task is to find a subsequence with maximum sum such that there should be no adjacent elements from the array in the subsequence

限于喜欢 提交于 2021-02-08 12:09:57

问题


It's showing the wrong answer. Can anybody please tell me which test case I am missing ?

Without Adjacent

Given an array arr[] of N positive integers. The task is to find a subsequence with maximum sum such that there should be no adjacent elements from the array in the subsequence.

Input: First line of input contains number of testcases T. For each testcase, first line of input contains size of array N. Next line contains N elements of the array space seperated.

Output: For each testcase, print the maximum sum of the subsequence.

Constraints:

1 <= T <= 100

1 <= N <= 10^6

1 <= arr[i] <= 10^6

Example:

Input:

2
3
1 2 3
3
1 20 3

Output:

4
20

Explanation:

Testcase 1: Elements 1 and 3 form a subsequence with maximum sum and no elements in the subsequence are adjacent in the array.

Testcase 2: Element 20 from the array forms a subsequence with maximum sum.

I tried using below test cases also

Input:

3
9
1 2 9 4 5 0 4 11 6
1
0
1
1

Output:

26
0
1

It worked fine but while submitting it was giving "wrong answer" I don't know for which test case it was talking about

Here is my solution:

#include<iostream>

using namespace std;

int main()

{
    
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        cin>>n;
        
        int arr[n];
        for(int i=0;i<n;i++)
            cin>>arr[i];
       
        int sum1,sum2,sum_even=0,sum_odd=0;
        
        for(int i=0;i<n;i+=2)
            sum_even+=arr[i];
        for(int i=1;i<n;i+=2)
            sum_odd+=arr[i];
        
        if(n>=1)
            sum1 = arr[0];
        else
            sum1 = -1;
        
        if(n>=2)
            sum2 = arr[1];
        else
            sum2 = -1;   
            
        int new_sum,i;
        
        for(i=2; i<n; i+=2)
        {
            if((i+1)!=n && arr[i+1]>arr[i])
            {
                i++;
                sum1+=arr[i];
            }
            else if(i+1==n)
            {
                sum1+=arr[i];
            }
            else
            {
                sum1+=arr[i];
            }
        }
        
        for(i=3; i<n; i+=2)
        {
            if((i+1)!=n && arr[i+1]>arr[i])
            {
                i++;
                sum2+=arr[i];
            }
            else if(i+1 ==n)
            {
                sum2+=arr[i];
            }
            else
            {
                sum2+=arr[i];
            }
        }
        
        int sum = sum1>sum2 ? sum1 : sum2;
        sum = sum>sum_odd ? sum : sum_odd;
        sum = sum>sum_even ? sum : sum_even;
      
        cout<<sum<<endl;
    }
    return 0;
}

回答1:


The issue is that you seem to made some guesses on the structure on any solution.
Your code is rather complex and it is difficult effectively to find a counter example by hand.
I made a random generation of arrays and compare your result with the optimal one.
I finally obtained this counter example : [14 18 8 19 22 1 20 23]. Your code gives a result of 64, while the optimum sum is equal to 67.

A simple optimum solution is to iteratively calculate two sums, both corresponding to a maximum up to the current index i, the first sum (sum0) assuming current value arr[i] is not used, the second sum (sum1) assuming the current value arr[i] is used.

#include <iostream>
#include <vector>
#include <algorithm>

int max_sum (const std::vector<int>& arr) {
    int sum0 = 0; 
    int sum1 = arr[0];
    int n = arr.size();
    
    for (int i = 1; i < n; ++i) {
        int temp = sum0;
        sum0 = std::max (sum0, sum1);
        sum1 = temp + arr[i];
    }
    return std::max (sum0, sum1);
}

int main() {
    int t;
    std::cin >> t;
    while(t--) {
        int n;
        std::cin >> n;
        std::vector<int> arr(n);
        for(int i = 0; i < n; i++)
           std::cin >> arr[i];
        int sum = max_sum (arr);
        std::cout << sum << '\n';
    }
}


来源:https://stackoverflow.com/questions/63813228/the-task-is-to-find-a-subsequence-with-maximum-sum-such-that-there-should-be-no

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!