With every increase in length of array, number of subarray increases by n
example arr = {3,2,5} total Possible subarray is 1 + 2 + 3 = 6;
example arr = {1,2,3,2} total Possible subarray is 1 + 2 + 3 + 4 = 10;
so in this question find the count of all consecutive elements that doesnot containg maxNumber, and return the total number of subarrays.
Probably this is it, let me know if logic is wrong
#include<bits/stdc++.h>
using namespace std;
int32_t main()
{
vector<int> TC1 = {1,2,3,9}; // output = 6 {1,2,3, 12 , 23, 123}
vector<int> TC2 = {1,9,3,9}; // output = 2 {1,3}
vector<int> TC3 = {1,9,3,8}; // output = 4 {1,3,8,38}
vector<int> TC4 = {9}; // output = 0 {}
// CODE BEGINS
vector<int> a = TC1;
int maxElement = *max_element(a.begin() , a.end());
int ans = 0,
ct = 0;
for(auto i : a)
{
if(i != maxElement)
++ct,
ans += ct;
else
ct = 0;
}
cout<<"ans is "<<ans;
}