Ejemplo n.º 1
0
int maxsubarray(int low,int high)
{
    if(low==high&&a[low]%p==0)
        return a[low];
    else
    {
        mid=(low+high)/2;

        leftsum  =maxsubarray(low,mid);
        rightsum =maxsubarray(mid+1,high);
        crosssum =maxcrossarray(low,mid,high);

        if(leftsum>rightsum&&leftsum>crosssum)
            {
                sum=leftsum;
                a1=low;
                b=mid;

            }
        else if(rightsum>leftsum&&rightsum>crosssum)
            {
                sum=rightsum;
                a1= mid+1;
                b=high;
            }
        else
        {
            sum=crosssum;
            a1=low1;
            b=high1;
        }
            return sum;
    }
}
int main()
{
	int *ans;
	int j;
	for (j = 0; j < 8; j++)
	{
		ans = maxsubarray(test[j].arr, &test[j].size);
		if (check(ans, test[j].ans, test[j].size))
		{
			printf("Passed\n");
		}
	}
	getchar();
}
Ejemplo n.º 3
0
int main()
{
    int i,n;

    scanf("%d",&n);

        for(i=0;i<n;i++)
            scanf("%d",&a[i]);

        maxsubarray(0,n-1);
        printf("maximum sub array sum is :%d \n low =%d ,high=%d",sum,a1,b);

return 0;
}
Ejemplo n.º 4
0
int main(int argc, const char * argv[])
{
    int arr[10] = {2, -3,6,5,4,7,3,2,1,2};
    //    printf("Number of inversions %d\n", count_inversion(arr, 8));
    //    for (int i = 0; i < 7; i++) {
    //        printf("%d \n", arr[i]);
    //    }
    
    MaxsubarrayResult result = maxsubarray(arr, 8);
    printf("left: %d, right: %d, sum: %d\n", result.lo, result.hi, result.sum);
    
    result = maxsubarray_linear(arr, 8);
    printf("left: %d, right: %d, sum: %d\n", result.lo, result.hi, result.sum);
    
    printf("Minimum of the array: %d\n", stat_min(arr, 10));
    printf("Maximum of the array: %d\n", stat_max(arr, 10));
    printf("%d minimum of array: %d\n", 7, stat_ith_min(arr, 10, 7));
    
    int out[10] = {};
    sort_counting(arr, 10, -3, 7, out);
    for (int i = 0; i < 10; i++) {
        printf("%d \n", out[i]);
    }
    
    IntegerStack stack;
    stack_init(&stack, 10);
    stack_push(&stack, 4);
    stack_push(&stack, 8);
    stack_push(&stack, -3);
    printf("Stack top: %d\n", stack.top);
    int pop = 1;
    stack_pop(&stack, &pop);
    stack_pop(&stack, &pop);
    printf("Stack pop: %d\n", pop);
    stack_destroy(&stack);
    
    IntegerQueue q;
    queue_init(&q, 10);
    
    queue_enqueue(&q, 10);
    printf("Dequeue result: %d, Key: %d\n", queue_dequeue(&q, &pop), pop);
    printf("Dequeue result: %d, Key: %d\n", queue_dequeue(&q, &pop), pop);
    
    queue_destroy(&q);
    
    return 0;
}