Menu Close

Find the number of sub-strings which start and end both in 1.

Fazil’s faculty gave him a string S consisting of only 1s and 0s and he need to find the number of sub-strings which start and end both in 1.
In this problem, a sub-string is defined as a sequence of continuous characters Si, Si+1, …, Sj where 1 ≤ i ≤ j ≤ N.

Can you help Fazil in completing the task?

Input:
T, the number of testcases. 

Each test-case consists of N(the length of string) in one line and string in second line.

Output:
For each testcase, print the required answer in one line.

Explanation:
Assume the String with 5 digits 10001 then it has 3 substrings such as S[1,1], S[5,5] and S[1,5]  that satisfy the condition.
#include <stdio.h>
int main()
{
    int t;
    int l;
    scanf("%d",&t);
    int sum;
    char string;
    int pair;
    while(t>0){
        pair=0;
        sum=0;
        scanf("%d",&l);
        int i;
        for(i=0;i<=l;i++){
            scanf("%c",&string);
            if(string=='1')
            pair++;
        }
        for(i=1;i<=pair;i++)
        sum+=i;
        printf("%d\n",sum);
        t--;  }
	return 0;
}

INPUT_1:
3
6
111011
8
01101111
5
10011

OUTPUT:
15
21
6


INPUT_2:
5
3
101
6
011101
8
11100101
4
0010
7
1000011

OUTPUT:
3
10
15
1
6


ILLUSTRATION

EXECUTED USING GCC

Morae Q!