Rely variety of Subsequences in Array by which X and Y are min and max parts

Rely variety of Subsequences in Array by which X and Y are min and max parts

Given an array arr[] consisting of N distinctive parts, the duty is to return the depend of the subsequences in an array by which the minimal component is X and the utmost component is Y.

Examples:

Enter: arr[] = {2, 4, 6, 7, 5}, X = 2, Y = 5
Output: 2
Rationalization: Subsequences by which the minimal component is X and the utmost component is Y are {2, 5}, {2, 4, 5}.

Enter: arr[] = {2, 4, 6, 7, 5, 1, 9, 10, 11}, X = 2, Y = 7
Output: 8
Rationalization: subsequences by which the minimal component is X and the utmost component is Y are {2, 4, 6, 7, 5}, {2, 4, 7, 5}, {2, 4, 6, 7}, {2, 4, 7}, {2, 6, 7, 5}, {2, 7, 5}, {2, 6, 7}, {2, 7}

Naive Strategy: The fundamental method to remedy the issue is as follows:

The given downside could be solved by producing and counting all attainable subsequences of the given array utilizing recursion and checking if every subsequence has the minimal component as X and the utmost component as Y.

Beneath is the code for the above method:

C++

#embody <bits/stdc++.h>

utilizing namespace std;

  

int Subsequences(vector<int>& arr, int index,

                 vector<int>& subarr, int n, int x, int y)

{

  

    if (index == n) {

  

        

        

        if (subarr.dimension() != 0 && subarr[0] == x

            && subarr[subarr.size() - 1] == y) {

  

            

            

            return 1;

        }

        else

            return 0;

    }

    else {

  

        

        

        subarr.push_back(arr[index]);

  

        

        

        

        int pic

            = Subsequences(arr, index + 1, subarr, n, x, y);

  

        

        

        subarr.pop_back();

  

        

        

        

        int notpic

            = Subsequences(arr, index + 1, subarr, n, x, y);

  

        

        

        return pic + notpic;

    }

}

  

int important()

{

    vector<int> arr = { 2, 4, 6, 7, 5, 1, 9, 10, 11 };

    int x = 2, y = 7;

    vector<int> subarr;

  

    

    type(arr.start(), arr.finish());

  

    

    

    cout << Subsequences(arr, 0, subarr, arr.dimension(), x, y);

  

    return 0;

}

Time Complexity: O(2n)
Auxiliary Area: O(n)

Environment friendly Strategy: To resolve the issue comply with the beneath concept:

We all know that the subsequence can be shaped can be within the vary (X, Y), and the system that can come for all of the subsequence within the vary (X, Y) excluding X and Y can be 2n the place n is the variety of parts within the vary X and Y. Subsequently will return the twon as the reply.

Beneath are the steps for the above method:

  • Initialize a counter variable say, cnt = 0 to maintain monitor of the variety of parts within the vary [X, Y].
  • Run a loop from i = 0 to i < n and verify if the component lies throughout the vary (x, y),
    • if (arr[i] > X && arr[i] < Y), increment the cnt variable by 1.
  • Return 2cnt, which supplies us the variety of subsequences, and return 1 << cnt.

Beneath is the code for the above method:

C++

#embody <bits/stdc++.h>

utilizing namespace std;

  

int countSubsequences(int arr[], int n, int x, int y)

{

  

    

    

    

    int cnt = 0;

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

  

        

        

        if (arr[i] > x && arr[i] < y) {

  

            

            cnt++;

        }

    }

  

    

    

    

    return (1 << cnt);

}

  

int important()

{

    int arr[] = { 2, 4, 6, 7, 5 };

  

    

    int n = sizeof(arr) / sizeof(arr[0]);

    int x = 2, y = 5;

  

    

    

    cout << countSubsequences(arr, n, x, y) << endl;

    return 0;

}

Time Complexity: O(n)
Auxiliary Area: O(1)