Ejemplo n.º 1
0
void getMaxSubArray(int *arr, int n)
{
    int i, hash[100] = {[0 ... 99] = -10}; // initializing all elements with -10 because we will store the indices of sum_so_far obtained.
    int sum = 0;
    int lower_bound = 0, upper_bound = 0, size = 0;
    for (i = 0; i < n; ++i)
    {
        sum = sum + arr[i];
        // check if hash contains
        if (hash[sum] != -10)// sum is present in hash
        {
            int current_size = i - hash[sum];
            if (size < current_size)
            {
                size = current_size;
                lower_bound = hash[sum];
                upper_bound = i;
            }
        }
        else// sum is not present in hash, so insert it
        {
            hash[sum] = i; //initializing with the index
        }
    }
    replaceNegOnes(arr, n);
    printSubArray(arr, lower_bound+1, upper_bound);
}
Ejemplo n.º 2
0
void putsSubArray(int *A, int first, int last) 
{
    printSubArray(A, first, last);
    puts("\n");
}