in ,

Count of subarrays for each Array element in which arr[i] is first and least

Given an array arr[], the task is to find the count of subarrays starting from the current element that has a minimum element as the current element itself.

Examples: 

Input: arr[] = {2, 4, 2, 1, 3} 
Output: {3, 1, 1, 2, 1}
Explanation: For the first element we can form 3 valid subarrays with the given condition 

  • 2 -> {2} , {2,4} , {2,4,2} = 3 subarrays and so on
  • 4 -> {4}                   = 1 subarray
  • 2 -> {2}                   = 1 subarray
  • 1 -> {1} , {1, 3}          = 2 subarrays
  • 3 -> {3}                   = 1 subarray

Input: arr[] = {1, 4, 2, 5, 3}
Output: {5, 1, 3, 1, 1}

 

Naive Solution: The naive approach revolves around the idea that:

  • If smaller element is not found then count of subarrays = length of array – current index
  • If element is found then count of subarrays = index of smaller element – current index

The task can be solved using 2 loops. The outer loop picks all the elements one by one. The inner loop looks for the first smaller element for the element picked by the outer loop. If a smaller element is found then the index of that element  – current index is stored as next, otherwise, the lengthcurrent index is stored.

Below is the implementation of the above approach:

C++

#include

using namespace std;

  

vector<int> countOfSubArray(vector<int> arr)

{

    int next, i, j;

    int n = arr.size();

    vector<int> ans;

  

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

        bool flag = false;

  

        

        

        

        

        next = n - i;

        for (j = i + 1; j < n; j++) {

            if (arr[i] > arr[j]) {

  

                

                

                

                

                next = j - i;

                ans.push_back(next);

                flag = true;

                break;

            }

        }

  

        if (flag == false) {

            ans.push_back(next);

        }

    }

    return ans;

}

  

int main()

{

    vector<int> arr{ 1, 4, 2, 5, 3 };

  

    vector<int> ans = countOfSubArray(arr);

  

    for (int i = 0; i < ans.size(); i++) {

        cout << ans[i] << " ";

    }

  

    return 0;

}

Time Complexity: O(N2
Auxiliary Space: O(1)

Efficient Solution: This problem basically asks to find how far is the current index from the index of the next smaller number to the number at the current index. The most optimal way to solve this problem is by making use of a stack.
Follow the below steps to solve the problem:

  • Iterate over each number of the given array arr[] using the current index.
  • If the stack is empty, push the current index to the stack.
  • If the stack is not empty then do the following:
    • If the number at the current index is lesser than the number of the index at top of the stack, push the current index.
    • If the number at the current index is greater than the number of the index at top of the stack, then update the count of subarrays as the index at top of the stack – current index
  • Pop the stack once the number of days has been updated in the output list.
  • Repeat the above steps for all the indices in the stack that are greater than the number at the current index.

Below is the implementation of the above approach:

C++

#include

using namespace std;

  

vector<int> countOfSubArray(vector<int> arr)

{

  

    stack<int> st;

    

    vector<int> v;

    int n = arr.size();

  

    

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

  

        

        

        

        while (st.size() > 0 

               && arr[st.top()] >= arr[i]) {

            

            st.pop();

        }

  

        if (st.size() == 0) {

            v.push_back(n - i);

        }

        else {

            v.push_back(st.top() - i);

        }

  

        

        st.push(i);

    }

    

    reverse(v.begin(), v.end());

    return v;

}

  

int main()

{

    

    vector<int> arr{ 1, 4, 2, 5, 3 };

  

    

    vector<int> ans = countOfSubArray(arr);

  

    

    for (int i = 0; i < ans.size(); i++) {

        cout << ans[i] << " ";

    }

  

    return 0;

}

Time Complexity: O(N)  
Auxiliary Space: O(N) 

What do you think?

Silver 1

Written by yulica

Leave a Reply

Your email address will not be published.

      Android vs iOS Development: Pros and Cons of Each Platform_61d8c364a6197.jpeg

      Android vs iOS Development: Pros and Cons of Each Platform

      Mobile App Development Trends for 2020_61d8c2bb8874c.jpeg

      Mobile App Development Trends for 2020