// C++ implementation to find the
// sum of Decomposition values of
// all suffixes of an array
#include
using namespace std;
#define int long long int
// Function to find the decomposition
// values of the array
int decompose(vector S)
{
// Stack
stack s;
int N = S.size();
int ans = 0;
// Variable to maintain
// min value in stack
int nix = INT_MAX;
// Loop to iterate over the array
for (int i = N - 1; i >= 0; i--) {
// Condition to check if the
// stack is empty
if (s.empty()) {
s.push(S[i]);
nix = S[i];
}
else {
// Condition to check if the
// top of the stack is greater
// than the current element
if (S[i] < s.top()) {
s.push(S[i]);
nix = min(nix, S[i]);
}
else {
int val = S[i];
// Loop to pop the element out
while (!s.empty() &&
val >= s.top()) {
s.pop();
}
nix = min(nix, S[i]);
s.push(nix);
}
}
// the size of the stack is the
// max no of subarrays for
// suffix till index i
// from the right
ans += s.size();
}
return ans;
}
// Driver Code
signed main()
{
vector S = { 9, 6, 9, 35 };
cout << decompose(S) << endl;
return 0;
}
Java
// Java implementation to find the
// sum of Decomposition values of
// all suffixes of an array
import java.util.*;
class GFG{
// Function to find the decomposition
// values of the array
static int decompose(Vector S)
{
// Stack
Stack s = new Stack();
int N = S.size();
int ans = 0;
// Variable to maintain
// min value in stack
int nix = Integer.MAX_VALUE;
// Loop to iterate over the array
for(int i = N - 1; i >= 0; i--)
{
// Condition to check if the
// stack is empty
if (s.isEmpty())
{
s.add(S.get(i));
nix = S.get(i);
}
else
{
// Condition to check if the
// top of the stack is greater
// than the current element
if (S.get(i) < s.peek())
{
s.add(S.get(i));
nix = Math.min(nix, S.get(i));
}
else
{
int val = S.get(i);
// Loop to pop the element out
while (!s.isEmpty() && val >= s.peek())
{
s.pop();
}
nix = Math.min(nix, S.get(i));
s.add(nix);
}
}
// The size of the stack is the
// max no of subarrays for
// suffix till index i
// from the right
ans += s.size();
}
return ans;
}
// Driver Code
public static void main(String args[])
{
Vector S = new Vector();
S.add(9);
S.add(6);
S.add(9);
S.add(35);
System.out.println(decompose(S));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 implementation to find the
# sum of Decomposition values of
# all suffixes of an array
import sys
# Function to find the decomposition
# values of the array
def decompose(S):
# Stack
s = []
N = len(S)
ans = 0
# Variable to maintain
# min value in stack
nix = sys.maxsize
# Loop to iterate over the array
for i in range(N - 1, -1, -1):
# Condition to check if the
# stack is empty
if (len(s) == 0):
s.append(S[i])
nix = S[i]
else:
# Condition to check if the
# top of the stack is greater
# than the current element
if (S[i] < s[-1]):
s.append(S[i])
nix = min(nix, S[i])
else:
val = S[i]
# Loop to pop the element out
while (len(s) != 0 and
val >= s[-1]):
s.pop()
nix = min(nix, S[i]);
s.append(nix)
# The size of the stack is the
# max no of subarrays for
# suffix till index i
# from the right
ans += len(s)
return ans
# Driver Code
if __name__ =="__main__":
S = [ 9, 6, 9, 35 ]
print(decompose(S))
# This code is contributed by chitranayal
C#
// C# implementation to find the
// sum of Decomposition values of
// all suffixes of an array
using System;
using System.Collections.Generic;
class GFG{
// Function to find the decomposition
// values of the array
static int decompose(List S)
{
// Stack
Stack s = new Stack();
int N = S.Count;
int ans = 0;
// Variable to maintain
// min value in stack
int nix = Int32.MaxValue;
// Loop to iterate over the array
for(int i = N - 1; i >= 0; i--)
{
// Condition to check if the
// stack is empty
if (s.Count == 0)
{
s.Push(S[i]);
nix = S[i];
}
else
{
// Condition to check if the
// top of the stack is greater
// than the current element
if (S[i] < s.Peek())
{
s.Push(S[i]);
nix = Math.Min(nix, S[i]);
}
else
{
int val = S[i];
// Loop to pop the element out
while (s.Count != 0 && val >= s.Peek())
{
s.Pop();
}
nix = Math.Min(nix, S[i]);
s.Push(nix);
}
}
// The size of the stack is the
// max no of subarrays for
// suffix till index i
// from the right
ans += s.Count;
}
return ans;
}
// Driver code
static void Main()
{
List S = new List();
S.Add(9);
S.Add(6);
S.Add(9);
S.Add(35);
Console.WriteLine(decompose(S));
}
}
// This code is contributed by divyeshrabadiya07