int main(){ scanf("%d", &N); for (int i = 0; i < N; i++){ scanf("%d", &arr[i]); } kadane(); printf("%d\n", max_n); return 0; }
int main() { int p, i, v[10001], teste = 1, max, a, b, x, y; for(;;) { scanf("%d", &p); if (p == 0) break; else { for (i = 0; i < p; i++) { scanf("%d %d", &x, &y); v[i] = x - y; } kadane(v, p, &a, &b, &max); if (max > 0) printf("Teste %d\n%d %d\n", teste, a+1, b+1); else printf("Teste %d\nnenhum\n", teste); printf("\n"); teste++; } } return 0; }
int main() { /* * The Kadane algorithm is used to find the sub-array * with the biggest partial sum of elements */ double arr[] = {4.2, -1.2, -10, 10.25, -2.25, 20.2, 0.1}; int beg = 0; int end = 0; double sum = 0; int * begPtr = &beg; int * endPtr = &end; double * sumPtr = ∑ int sz = sizeof(arr) / sizeof(arr[0]); kadane(arr, sz, begPtr, endPtr, sumPtr); std::cout << "array starting at " << *begPtr << " and ending at " << *endPtr << " sums to " << *sumPtr << std::endl; return 0; }
static int step(int k) { int j, max_kadane, sum; sum = 0; for(j=0; j<N; j++) { aux[j] += mat[k][j]; sum += aux[j]; aux2[j] = -aux[j]; } max_kadane = kadane(aux); int k2 = kadane(aux2); sum += k2; if(sum > max_kadane) return sum; return max_kadane; }
int kadane2d(int a[N][N], int n) { int i, j, k, maxsum = 0; for(i = 0; i < n; i++) { int p[N]; for(j = 0; j < n; j++) p[j] = 0; for(j = i; j < n; j++) { int sum; for(k = 0; k < n; k++) p[k] += a[j][k]; /* p[k] = soma da coluna k, da linha i ate a linha j */ sum = kadane(p, n); if(sum > maxsum) maxsum = sum; } } return maxsum; }
// The main function that finds maximum sum rectangle in M[][] void findMaxSum(int M[][COL]) { // Variables to store the final output int maxSum = INT_MIN, finalLeft, finalRight, finalTop, finalBottom; int left, right, i; int temp[ROW], sum, start, finish; // Set the left column for (left = 0; left < COL; ++left) { // Initialize all elements of temp as 0 memset(temp, 0, sizeof(temp)); // Set the right column for the left column set by outer loop for (right = left; right < COL; ++right) { // Calculate sum between current left and right for every row 'i' for (i = 0; i < ROW; ++i) temp[i] += M[i][right]; // Find the maximum sum subarray in temp[]. The kadane() function // also sets values of start and finish. So 'sum' is sum of // rectangle between (start, left) and (finish, right) which is the // maximum sum with boundary columns strictly as left and right. sum = kadane(temp, &start, &finish, ROW); // Compare sum with maximum sum so far. If sum is more, then update // maxSum and other output values if (sum > maxSum) { maxSum = sum; finalLeft = left; finalRight = right; finalTop = start; finalBottom = finish; } } } // Print final values printf("(Top, Left) (%d, %d)\n", finalTop, finalLeft); printf("(Bottom, Right) (%d, %d)\n", finalBottom, finalRight); printf("Max sum is: %d\n", maxSum); }
int main(int argc, char const *argv[]) { int n; while(true){ scanf("%d",&n); if(n==0) break; memset(seq,0,sizeof(seq)); for (int i = 0; i < n; ++i) { scanf("%d",&seq[i]); } int s = kadane(n); if(s>0) printf("The maximum winning streak is %d.\n",s); else printf("%s\n","Losing streak." ); } return 0; }
int findMaxSum(int matrix[numRows][numCols]) { int maxSum = 0; for (int left = 0; left < numCols; left++) { int temp[numRows] = {0}; for (int right = left; right < numCols; right++) { // Find sum of every mini-row between left and right columns and save it into temp[] for (int i = 0; i < numRows; ++i) temp[i] += matrix[i][right]; // Find the maximum sum subarray in temp[]. int sum = kadane(temp, numRows); if (sum > maxSum) maxSum = sum; } } return maxSum; }
void findMaxSum(int M[][COL]) { int maxSum = INT_MIN; int finalLeft, finalRight, finalTop, finalBottom; int left, right, i; int temp[ROW], sum ,start, end; for (left=0; left<COL; ++left) { memset(temp, 0, sizeof(temp)); for (right=left; right<COL; ++right) { for (i=0; i<ROW; ++i) { temp[i] += M[i][right]; } sum = kadane(temp, &start, &end, ROW); if (sum > maxSum) { maxSum = sum; finalLeft = left; finalRight = right; finalTop = start; finalBottom = end; } } } printf("(Top, Left) (%d, %d)\n", finalTop, finalLeft); printf("(Bottom, Right) (%d, %d)\n", finalBottom, finalRight); printf("max sum is %d\n", maxSum); }
void findMaxSum() { // Variables to store the final output int maxSum = INT_MIN, finalLeft, finalRight, finalTop, finalBottom; int left, right, i; int sum; for (left = 0; left < COL; ++left) { memset(arr, 0, sizeof(arr)); for (right = left; right < COL; ++right) { for (i = 0; i < ROW; ++i) arr[i] += M[i][right]; sum = kadane(); if (sum > maxSum) { maxSum = sum; finalLeft = left; finalRight = right; finalTop = start; finalBottom = finish; } } } printf("(Top, Left) (%d, %d)\n", finalTop, finalLeft); printf("(Bottom, Right) (%d, %d)\n", finalBottom, finalRight); printf("Max sum is: %d\n", maxSum); }
int main() { typedef int InputType ; // Input array, output range, output sum. typedef std::tuple<std::vector<InputType>, std::pair<std::iterator_traits<std::vector<InputType>::iterator>::difference_type, std::iterator_traits<std::vector<InputType>::iterator>::difference_type>, InputType> InOut; InOut in_outs[]{ // All positive: easy, take all. InOut{ {1, 3, 2}, {0, 3}, 6 }, InOut{ {0, -1, 4, -2, 1, 2, -5, 3}, {2, 6}, 5 }, // All negative. Edge case. Take no elements and return an empty range [0, 0[. InOut{ {-1, -3, -2}, {0, 0}, 0 }, // Multiple solutions. Take the one that starts first and is shortest. // In this case, the shortest solution is the empty solution. InOut{ {0, 0, 0}, {0, 0}, 0 }, // Coming first has priority over being the shortest. InOut{ {1, 2, -100, 3}, {0, 2}, 3 }, // The shortest possible solution is taken in case of multiple solutions. InOut{ {1, 2, 0, -2, 2}, {0, 2}, 3 }, // Edge case: single element in input. InOut{ {1}, {0, 1}, 1 }, }; std::pair<std::vector<InputType>::iterator, std::vector<InputType>::iterator> output_range; InputType output_value; for (auto& in_out : in_outs) { auto& input = std::get<0>(in_out); auto& expected_output_range = std::get<1>(in_out); auto& expected_output_value = std::get<2>(in_out); kadane(input.begin(), input.end(), output_range, output_value); assert(output_range.first - input.begin() == expected_output_range.first); assert(output_range.second - input.begin() == expected_output_range.second); assert(output_value == expected_output_value); } }
int main() { int n,i,arr[]= {1,-1,-1,1,1,-1,1,1,1,1,-1,-1,-1,-1,-1,-1,1}; n = sizeof(arr)/sizeof(int); printf("max sum is : %d\n",kadane(arr,n)); }