Milly and special subarrays

QUESTION

Milly is playing with an array A of size N. She is trying to find those non-empty subarrays (sequence of consecutive elements) whose maximum and minimum values are identical. Milly got confused while counting. Your task is to help her in this problem.\n\nInput\n\nFirst line of the input will contain T denoting the number of test-cases.\nFor every test case, first line will contain N. Next line will contain N space separated integers denoting Ai.\nOutput\n\nFor every test case, print the required answer in a separate line.\nConstraints\n\n1 T 10\n1 N 10^5.

ANSWER

 #include <stdio.h>
#include <iostream>
using namespace std;
int a[100];
int main()
{
    int t;
    cin >> t;
    while(t--) {
        long long int n;
        cin >> n;
        for(int i = 0; i < n; i++) scanf("%d",&a[i]);
        long long k, ans = 0;
        long long int prev = 0;
        for(long long int i = 0; i <= n; i++) {
            if(a[i] == a[prev] && i!=n);
            else {
                k = i - prev;
                ans += k*(k+1)/2;
                prev = i;
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.