Reduce sum of max sized Subsequence such that no two parts are adjoining


View Dialogue

Enhance Article

Save Article

Like Article

View Dialogue

Enhance Article

Save Article

Like Article

Given an array arr[] of optimistic numbers, the duty is to seek out the minimal sum of a subsequence of most attainable parts with the constraint that no two numbers within the sequence are adjoining.

Examples:

Enter: arr[]={1, 7, 3, 2}
Output: 3
Rationalization: Choose the subsequence {1, 2}. 
The sum is 3 and no two parts are adjoining. 
That is the least attainable sum.

Enter: arr[]={1, 8, 4, 10, 6}
Output: 11
Rationalization: Choose the subsequence {1, 4, 6}. 
The sum is 11 and no two parts are adjoining. 
That is the least attainable sum.

Enter: arr[]={2, 0, 11, 2, 0, 2}
Output: 4
Rationalization: Choose the subsequence {0, 2, 2}. 
The sum is 4 and no two parts are adjoining. 
That is the least attainable sum.

Method: To resolve the issue observe the under observations:

There are two completely different circumstances:

  • Array dimension is Odd – the one alternative is to choose the even listed parts(Zero – Primarily based Indexing). 
  • Array dimension is Even – The subsequence dimension shall be N/2. It may be in three attainable methods
    • All of the even listed parts.
    • All of the odd listed parts.
    • Even listed parts until i and odd listed parts ranging from i+3
    • The minimal amongst these 3 would be the reply.

Comply with the steps to unravel the issue:

  • If the scale of arr[] is Odd, the sum of all of the even listed parts is the reply.
  • For Dimension of arr to Even, retailer the sum of all of the even indices right into a variable.
    • Iterating from the tip by including the odd index parts and eradicating the even listed parts from the tip.
    • Replace the minimal reply in every index.
  • After the iteration is over, return the minimal worth as the reply.

 Under is the implementation of the above strategy.

C++

  

#embody <bits/stdc++.h>

utilizing namespace std;

  

int FindMinSum(vector<int>& vec, int n)

{

    int sum = 0;

    for (int i = 0; i < n; i += 2) {

  

        

        sum += vec[i];

    }

    if (n % 2 != 0)

        return sum;

    int ans = sum;

    for (int i = n - 1; i > 0; i -= 2) {

  

        

        

        sum = sum - vec[i - 1] + vec[i];

  

        

        ans = min(sum, ans);

    }

  

    

    return ans;

}

  

int most important()

{

  

    

    vector<int> arr = { 2, 0, 11, 2, 0, 2 };

    int N = vec.dimension();

  

    

    cout << FindMinSum(arr, N) << endl;

  

    return 0;

}

C

#embody <stdio.h>

  

int FindMinSum(int arr[], int n)

{

    int sum = 0;

    for (int i = 0; i < n; i += 2) {

        

        sum += arr[i];

    }

    if (n % 2 == 1)

        return sum;

    int ans = sum;

    for (int i = n - 1; i > 0; i -= 2) {

        

        sum = sum - arr[i - 1] + arr[i];

        

        if (sum < ans)

            ans = sum;

    }

  

    

    return ans;

}

int most important()

{

    

    int arr[] = { 2, 0, 11, 2, 0, 2 };

    int n = *(&arr + 1) - arr;

  

    

    int reply = FindMinSum(arr, n);

    printf("%d", reply);

  

    return 0;

}

Java

import java.io.*;

import java.util.*;

  

public class GFG {

  

    static int remedy(int vec[], int n)

    {

        int sum = 0;

        if (n % 2 != 0) {

            for (int i = 0; i < n; i = i + 2) {

                

                sum = sum + vec[i];

            }

            return sum;

        }

        else {

            int ans = sum;

            for (int i = n - 1; i > 0; i -= 2) {

                

                

                ans = ans - vec[i - 1] + vec[i];

                

                ans = Math.min(ans, sum);

            }

            

            return ans;

        }

    }

    public static void most important(String args[])

        throws IOException

    {

        

        int vec[] = { 2, 1, 2 };

  

        int n = vec.size;

  

        

        System.out.println(remedy(vec, n));

    }

}

Python

def FindMinSum(record, n):

    sum = 0

    for i in vary(0, n, 2):

        

        sum = sum + record[i]

    if n % 2 == 1:

        return sum

    ans = sum

    for i in vary(n-1, 0, -2):

        

        sum = sum - record[i-1] + record[i]

        

        ans = min(sum, ans)

     

    return ans

  

  

record = [2, 0, 11, 2, 0, 2]

n = 6

okay = FindMinSum(record, n)

print(okay)

Time Complexity: O(N)
Auxiliary House: O(1)

Leave a Reply