Mehta Task

QUESTION

This time your task is simple.\n\nTask:\nMehta has to find the longest subarray whose sum is greater than equal to zero in a given array of length** N**. After he finds out the length of longest subarray, he has to report how many subarrays of such maximum length exist that satisfy the mentioned property.\n\nNote:\nSubarray is defined as an array of contiguous numbers in a particular array. Subarray(A,B) has length (B-A+1) and consists of all numbers from index A to index B.\n\nInput:\nFirst Line of the input consists of a single integer N as described above. Next Line consists of N space separated integers.\n\nOutput:\nIn a single line, output the maximum length and number of such subarrays having maximum length separated by a single space.In case, there is no such subarray having sum greater than equal to zero, simply print \”-1\” without quotes.

“TESTCASE_1”: “4\n-1 -2 3 -1\n###—###SEPERATOR—###—\n3 2”, “TESTCASE_2”: “6\n8 3 -2 5 -1 6\n###—###SEPERATOR—###—\n6 1”, “TESTCASE_3”: “10\n4 3 15 -1 -5 21 99 -9 34 67\n###—###SEPERATOR—###—\n10 1”, “TESTCASE_4”: “0\n###—###SEPERATOR—###—\n0”, “TESTCASE_5”: “0\n###—###SEPERATOR—###—\n0

ANSWER

#include <stdio.h>
long array[10000001];
int main()
{
    int i, j, n, l1 = 0, l2 = 0;
    scanf("%d", &n);
    for (i = 1; i <= n; i++) {
    	scanf("%ld", &array[i]);
    	array[i] += array[i-1];
    	if (array[i] >= array[i - l1])
    		l2++;
    	for (j = l1 + 1; j <= i && array[i] >= array[i - j]; j++) {
    		l1 = j;
    		l2 = 1;
    	}
    }
    if (l1 == 0)
    	printf("-1\n");
    else
    	printf("%d %d\n", l1, l2);
    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.

Powered By
CHP Adblock Detector Plugin | Codehelppro