Lagest sum consecutive increasing digits in a No - SSEC0015

Problem Link: SSEC0015

Problem:

You are provided with an input containing a number. Implement a solution to find the largest sum of consecutive increasing digits , and present the output with the largest sum and the positon of start and end of the consecutive digits.

Example :

Input :> 8789651

Output :> 24:2-4

where 24 is the largest sum and 2-4 is start and end of the consecutive increasing digits

Solution:

#include <bits/stdc++.h>
using namespace std;

pair<int,pair<int,int>> findMaxAndIndices(vector v){
int n = v.size();
int sii=0,ei=0;
int currMax = 0;
int i;
priority_queue pq;
pq.push(currMax);
int si=0;
bool flag=false;
for(i=0;i<n-1;i++){
if(v[i]<v[i+1]){
currMax+=v[i];
if(!flag){
si=i;
}
flag=true;
}else{
currMax+=v[i];
if(currMax>pq.top()){
pq.push(currMax);
ei = i;
sii = si;
if(!flag){
sii=i;
}else{
flag=false;
}
}
currMax=0;
}
}
if(i==n-1){
if(v[i]>v[i-1]){
currMax+=v[i];
if(currMax>pq.top()){
pq.push(currMax);
ei = i;
sii = si;
if(!flag){
sii=i;
}else{
flag=false;
}
}
}
}
pair<int,pair<int,int>> p;
p.first = pq.top();
p.second.first = sii;
p.second.second = ei;
return p;
}

int main() {
// your code goes here
long long int n;
cin>>n;
vector v;
while(n!=0){
int m = n%10;
v.push_back(m);
n/=10;
}
reverse(v.begin(),v.end());
pair<int,pair<int,int>> ans = findMaxAndIndices(v);
cout<<ans.first<<“:”<<ans.second.first+1<<“-”<<ans.second.second+1<<endl;
return 0;
}