how to adjust mod ?? so as correct ans on bigger overfllow values also
Extra ques......help
@hg11110000
Some changes to your code
- Do cnt%=mod; before using cnt in the actual calculation.
- Declare cnt as a long long variable so that it can be allowed to do computations outside the range of int as well. Final answer will be in range of int as modulo is done afterwards.
class Solution {
public:
int countHomogenous(string s) {
long long cnt=0; //Changed the declaration from int to LL
int ans=0;
int mod=1e9+7;
for(int i=0;i<s.size();i++)
{
// int c=s[i];
cnt=0;
cnt++;
while(s[i]==s[i+1] and i<s.size())
{
cnt++;
// cnt=(cnt%mod+1%mod)%mod;
i++;
}
cnt %= mod; //Added this statement
ans+=((cnt*(cnt+1))%mod)/2;
// ans+=(((cnt/2)%mod)*((cnt+1)%mod))%mod;
}
return ans;
}
};