Example #1
0
unsigned long int max_sum(int row,int col)
{
//  if(row==0) printf("\nrow %d col %d\n,%d",row,col,triangle[row][col]);
  if (row==ROWS-1) return triangle[row][col]; //base case, bottom of triangle
  if (paths[row][col]) return paths[row][col]; //dynamic programming aspect, avoids caculating things it has already calculating.
  int t = triangle[row][col]; //don't access twice
  unsigned long int a = t + max_sum(row+1,col);
  unsigned long int b = t + max_sum(row+1,col+1);
  unsigned long int temp = (a>b) ? a : b;
  paths[row][col] = temp;
  return temp;
}
Example #2
0
int main(int argc, char *argv[])
{
    int a[] = { 1, 2, -3, 4, -9, -20, -50, 78, 90, -100 };

    printf("MAX: %d\n", max_sum(a));

    return 0;
}
Example #3
0
int  max_sum(int x[], int left, int right)
{
     int middle;              /* the middle element       */
     int max_to_left;         /* max found in [left,mid]  */
     int max_to_right;        /* max found in [mid+1,rig] */
     int max_crossing;        /* max found cross middle   */
     int sum;                 /* working variable         */
     int left_max;            /* left max from recursion  */
     int right_max;           /* right max from recursion */
     int result, i;

     if (left > right)        /* no element remains?      */
          result = 0;         /* result must be zero      */
     else if (left == right)  /* exactly one element?     */
          result = (x[left] > 0) ? x[left] : 0; /* ret max*/
     else {                   /* now we have many element */
          middle    = (left + right) / 2; /* get the mid. */
          left_max  = max_sum(x, left, middle); /* left   */
          right_max = max_sum(x, middle+1, right); /* rig.*/

          sum = 0;            /* compute max from mid down*/
          max_to_left = 0;
          for (i = middle; i >= left; i--) {
               sum += x[i];
               if (max_to_left < sum)
                    max_to_left = sum;
          }

          sum = 0;            /* compute max from mid+1 up*/
          max_to_right = 0;
          for (i = middle+1; i <= right; i++) {
               sum += x[i];
               if (max_to_right < sum)
                    max_to_right = sum;
          }

          max_crossing = max_to_left+max_to_right; /*cross*/
          if (max_crossing >= max_to_left) /* get the max */
               result = max(max_crossing, max_to_right);
          else
               result = max(max_to_left, max_to_right);
     }
     return result;
}
Example #4
0
int main()
{
    int i,n;
    while(~scanf("%d",&n)){
        for(i=0;i<n;i++)
            scanf("%d",d+i);
        printf("%d\n",max_sum(d,n));
    }
    return 0;
}
Example #5
0
void main(void)
{
     int  x[] = { 2, -3, 1, -1, 3, -2, -3, 3};
     int  n   = sizeof(x)/sizeof(int);
     int  i;

     printf("\nMaximum Consecutive Elements Sum Program");
     printf("\n========================================");
     printf("\n\nGiven Array :");

     for (i = 0; i < n; i++)
          printf("%4d", x[i]);

     printf("\n\nMaximum Sum is %d", max_sum(x, 0, n-1));
}
Example #6
0
int max_sum(int array[][SIZE], int index)
{
    int i;

    for(i = 0; i < index+1; i++) {
        if(array[index + 1][i] > array[index + 1][i + 1])
            array[index][i] += array[index + 1][i];
        else
            array[index][i] += array[index + 1][i + 1];
    }

    if(index != 0)
        return max_sum(array, index-1);
    else
        return array[0][0];
}
Example #7
0
int main(int argc, char* argv[])
{
	int n;
	MaxSum ret;

	int array1[] = {0, -1, 2, -1, 3, -1, 0};
	n = sizeof(array1)/sizeof(int);
	ret = max_sum(array1, n);
	printf("sum:%d [%d:%d]\n", ret.sum, ret.start+1 , ret.end+1); // 4 (2 + -1 + 3), start 3, end 5

	int array2[] = {0, 2, -2, -1, -3, 1, 0, 4, 1};
	n = sizeof(array2)/sizeof(int);
	ret = max_sum(array2, n);
	printf("sum:%d [%d:%d]\n", ret.sum, ret.start+1 , ret.end+1); // 6 (1 + 0 + 4 + 1) start 6, end 9

	int array3[] = {0, -2, 2, -4, 3, -3, -2};
	n = sizeof(array3)/sizeof(int);
	ret = max_sum(array3, n);
	printf("sum:%d [%d:%d]\n", ret.sum, ret.start+1 , ret.end+1); // 1 (2 + -4 + 3) start 3, end 5

	int array4[] = {2, -1, 2, -1, 3};
	n = sizeof(array4)/sizeof(int);
	ret = max_sum(array4, n);
	printf("sum:%d [%d:%d]\n", ret.sum, ret.start+1 , ret.end+1); // 5 (2 + -1 + 2 + -1 + 3) start 1, end 5

	int array5[] = {0, -1, 2, -4, 3, -3, 2};
	n = sizeof(array5)/sizeof(int);
	ret = max_sum(array5, n);
	printf("sum:%d [%d:%d]\n", ret.sum, ret.start+1 , ret.end+1); // 1 (0 + -1 + 2) start 1, end 3
	                                                           // or 1 (2 + -4 + 3) start 3, end 5
	                                                           // or 1 (3 + -3 + 2) start 5, end 7

	/* ok, corner cases I do not have yet solutions for. */

	//FIXME: failing - one of "peak" numbers involved is zero
	int array6[] = {0, -1, 2, -4, 2, -3, -2};
	n = sizeof(array6)/sizeof(int);
	ret = max_sum(array6, n);
	printf("sum:%d [%d:%d]\n", ret.sum, ret.start+1 , ret.end+1); // 1 (0 + -1 + 2) start 1, end 3
	return 0;
	//FIXME: not even thought of - all numbers involved are negative
	int array7[] = {0, -1, -2, -4, -2, -3, -2};
	n = sizeof(array7)/sizeof(int);
	ret = max_sum(array7, n);
	printf("sum:%d [%d:%d]\n", ret.sum, ret.start+1 , ret.end+1); // OMG!
	return 0;
}
Example #8
0
int main()
{
  FILE* f = fopen(FILENAME,"r");
  memset(triangle,0,sizeof(triangle));
  memset(paths,0,sizeof(paths));
  char c;
  int col = 0;
  int row = 0;
  int d = 0;
  while ((c=fgetc(f)) != EOF)
  {
     if (c==' ') 
     {
       triangle[row][col] = d;
       d = 0;
       col++;
       continue;
     }

     if (c=='\n') 
     {
       triangle[row][col] = d;
       row++;
       col = 0;
       d = 0;
       continue;
     }

     d = 10*d + c - '0';     
  }
  if (d) triangle[row][col]=d; 

  /*for (int i=0;i<ROWS;i++)
  {
    for (int j=0;j<ROWS;j++)
    {
      printf("%d ",triangle[i][j]);
    }
    printf("\n");
  }*/

  printf("Max path sum: %lu\n",max_sum(0,0));
}
Example #9
0
int main()
{
    FILE *fp;
    int array[SIZE][SIZE];

    int i;
    int j;

    if((fp = fopen("067.txt", "r")) == NULL) {
        return 0;
    }

    for(i = 0; !feof(fp); i++)
        for(j = 0; j <= i; j++)
            fscanf(fp, "%d", &array[i][j]);

    printf("%d\n", max_sum(array, 98));

    fclose(fp);

    return 0;
}
Example #10
0
int main(){
  /** Variable declaration and initialization 
  We will use a double pointer to hold the 2D matrix
  for dynamic allocation of space.
  */
  int **matrix;
  int *array;
  int rows,cols,i,j,temp,k;

  int row1,row2,col1,col2,start,end,max_so_far;
  int sum = 0;  
  int flag = 0;

  row1 = -9999;
  row2 = row1;
  col1 = row1;
  col2 = row1; 
  start = row1;
  end = row1;
  max_so_far = -1;
  matrix = NULL;
  /**
  We first ask the user about the desired Matrix size, 
  making sure that they input the right values.
  */
  printf("\nBuilding a MxN matrix. (M can be == N)\n");
  printf("\nEnter the number of rows :");
  if(scanf("%d",&rows) != 1){
  	printf("\nInvalid Input! Exiting..");
	exit(-1);
  }
  printf("\nEnter the number of columns:");
  if(scanf("%d",&cols) != 1){
	printf("\nInvalid Input! Exiting..");
	exit(-1);
  }
  /**
  Now that we know the size, we dynamically allocate memory of that size
  to hold the matrix elements.
  */
 
  /**
  The way we do this is to consider a matrix as an array of arrays.
  Every array will hold elements equal to Column size and there would be
  'Row' number of such arrays.
  */

  //Allocating memory for 'Row' arrays
  matrix = (int**)malloc(rows*sizeof(int*));
  //Now allocating memory for each array of 'Column' elements itself
  for(i=0;i<rows;i++){
      		*(matrix+i) = (int*)malloc(cols*sizeof(int));
  }

  //building the matrix by taking user input
  
  for(i=0;i<rows;i++){//Complexity O(n^2)
	for(j=0;j<cols;j++){
		printf("\nEnter Matrix[%d][%d] :",i,j);
		if(scanf("%d",&temp) != 1){
		 	printf("\nInvalid Input! Exiting..");
		}
		if(temp > 0)
			flag = 1;
		*(*(matrix+i)+j) = temp;
        }         
  }

  if(flag == 0){
    	printf("\nWe require at least 1 positive value for this algorithm to work! Please try again.\n");
	exit(-1);
  }

  //Print Matrix
  printf("\nYour Matrix is:\n");
  for(i=0;i<rows;i++){
	printf("\n");
        for(j=0;j<cols;j++){
                printf("\t%d",*(*(matrix+i)+j));
        }
  }
  printf("\n");
  
  /**
  Algorithm:
  1. We first traverse the matrix row by row - 
	 while doing this, we consider a temporary array (of size 'Column' 
		this array holds the sum of elements in each column of the row below.
     e.g. If our Matrix looks like: 1 2 3 
                                    4 5 6 
                                    7 8 9 
          when we are at row 0, the temp array will hold: [1+4+7,2+5+8,3+6+8] and so on.
  2. With this array in place, we then find out the maximum sum of contiguous elements (start and end)
     and record it. If on further iteration, we find a sum greater than earlier, we update the value
     and the start and end coordinates. 
  3. The max sum recorded at the end of all iterations and the i,j (See loop below) start,end values 
     give us the matrix that has the highest sum of elements. 
  Note: This Algorithm only works if at least one matrix element is positive.
  Complexity : O(n^3)
  */
 
  //Create the temporary array
  array = (int*)malloc(cols*sizeof(int));
  
  //Now we find the greatest sum

  for(i=0;i<rows;i++){
  
	for(k=0;k<cols;k++){
		//everything is 0 to start with
		*(array+k) = 0;
	}
        for(j=i;j<rows;j++){
		for(k=0;k<cols;k++)
			*(array+k) = *(array+k)+ *(*(matrix+j)+k);
  		//Now that we have the Array, find the maximum sum and the contiguous elements.
		max_sum(array,cols,&start,&end,&sum);
		if(sum > max_so_far){
 			col1 = start;
			col2 = end;
			row1 = i;
 			row2 = j;
			max_so_far = sum;
  		}
        }
  }

  //What we have now is the solution. 

  printf("Max sum is: %d, start_row = %d, start_col = %d, end_row = %d, end_col = %d\n",max_so_far,row1,col1,row2,col2);
  return 0;
}