(C programming) Given a sequence of numbers a1, a2, a3, ..., an, find the maximum sum of a contiguous subsequence of those numbers. Note that, a subsequence of one element is also a contiquous subsequence.
Input
The input consists of multiple datasets. Each data set consists of:
n a1 a2 . . an
You can assume that 1 ≤ n ≤ 5000 and -100000 ≤ ai ≤ 100000.
The input end with a line consisting of a single 0.
Output
For each dataset, print the maximum sum in a line.
Sample Input
7 -5 -1 6 4 9 -6 -7 13 1 2 3 2 -2 -1 1 2 3 2 1 -2 1 3 1000 -200 201 0
Output for the Sample Input
19 14 1001
Kadane's algorithm has been used here to find the maximum sum of contiguous array
Editable Code:
# include <stdio.h>
//Implementation of Kadane's algorithm
int maxContiguousSum(int a[], int size) {
int max_ending_here = 0, max_so_far = 0,i;
for ( i = 0; i < size; i++){
max_ending_here = max_ending_here + a[i];
if (max_ending_here < 0)
max_ending_here = 0;
else if (max_so_far < max_ending_here)
max_so_far = max_ending_here;
}
return max_so_far;
}
int main(){
int n;
scanf("%d", &n);
if(n>0){
do{
int
a[5000];
int i;
for(i =
0;i<n;i++){ //Taking array input
scanf("%d", &a[i]);
}
printf("%d\n",maxContiguousSum(a,n)); //Calling the fuction to
print max sum
scanf("%d",
&n); //Size of next array
}while(n>0); //Loop runs if size
of next array is greater than 0
}
return 0;
}
Hope this helps
If you have any doubt feel free to comment
Thank You!!
(C programming) Given a sequence of numbers a1, a2, a3, ..., an, find the maximum sum...
The input consists of n numbers a1, a2, . . . , an and a target value t. The goal is to determine in how many possible ways can we add up two of these numbers to get t. Formally, your program needs to find the number of pairs of indices i, j, i < j such that ai+aj = t. For example, for 2, 7, 3, 1, 5, 6 and t = 7, we can get t in two...
Sequence a1,a2……an consists of nonnegative integers. We want to determine the largest sum of such subsequences that do not contain two consecutive elements of the original sequence (For example if a3 is a member of the subsequence, then a2 and a4 cannot be contained in the subsequence ). Give an O(n) running time algorithms for this problem.
13. Consider the sequence of numbers ao, ai, a2, a3, given by ao-2, ai-3, and for any positive integer k 2, a3ak 2ak-1. (a) Evaluate a2,a3, a4,as. Show your work. (b) Prove that for all positive integers n, an 2 +1
ALGORITHM PROBLEM: A) Significant Inversions: We are given a sequence of n arbitrary but distinct real numbers <a1 , a2 ,..., an>. We define a significant inversion to be a pair i < j such that ai > 2 aj . Design and analyze an O(n log n) time algorithm to count the number of significant inversions in the given sequence. [Hint: Use divide-&-conquer. Do the “combine” step carefully] B) The Maximum-Sum Monotone Sub-Array Problem: Input: An array A[1..n] of...
2 Double summation Let a1, A2, A3, ... be a sequence of real numbers, and let n > 1 be an integer. Which of the following are always equal? пі пп nn nn « ££« Žia. E£« L« i=1 j=1 i=1 j=1 i= 1 i=1 j=1 j=1 i=j
: Let a1, a2, a3, . . . be the sequence of integers defined by a1 = 1 and defined for n ≥ 2 by the recurrence relation an = 3an−1 + 1. Using the Principle of Mathematical Induction, prove for all integers n ≥ 1 that an = (3 n − 1) /2 .
1. Write a program that reads in two arrays (a1 and a2) of numbers and creates a new array (a3) of numbers such that the new array contains all the unique numbers of a1 and a2. Assume the input array elements are unique elements. In the main function, ask the user to enter the length of each array, declare and reads in the numbers for each array, and calculate and display the output array. Example input/output #1: Enter the length...
Given the following algorithm: Algorithnm Input: a1, a2,...,an, a sequence of numbers n, the length of the sequence x, a number Output: ?? i:- 1 While (x2 # a, and i < n) i+1 End-while If (x- - a) Return(i) Return(-1) 3, -1, 2,9, 36,-7, 6,4 a) What is the correct output of the Algorithm with the following input: a1, a2,..an b) What is the asymptotic worst-case time complexity of the Algorithm? Algorithnm Input: a1, a2,...,an, a sequence of numbers...
You are given a set of integer numbers A = {a1, a2, ..., an}, where 1 ≤ ai ≤ m for all 1 ≤ i ≤ n and for a given positive integer m. Give an algorithm which determines whether you can represent a given positive integer k ≤ nm as a sum of some numbers from A, if each number from A can be used at most once. Your algorithm must have O(nk) time complexity.
3. (6 pts) A sequence A = [a1, a2, . . . , anjis called a valley sequence if there is an index i with 1 < t < n such that al > a2 > . . . > ai and ai < ai+1 < . . . < an. A valley sequence must contain at least three elements. (a) (2 pts) Given a valley sequence A of length n, describe an algorithm that finds the element a, as...