This is a C Program to find the sum of contiguous subarray within a 1 – D array of numbers which has the largest sum.

We have to write a program in C such that the program will find the sum of contiguous subarray within a 1 – D array of numbers (one-dimensional array of numbers) which has the largest sum.

Suppose, we have an array of 8 elements with values: -1,-5,5,3,-2,5,4 and 1, then here is a sample of various possible contiguous subarrays:

-1 -1,-5 -1,-5,5 -1,-5,5,3 -1,-5,5,3,-2 -1,-5,5,3,-2,5 -1,-5,5,3,-2,5,4 -1,-5,5,3,-2,5,4,1 -5 -5,5 -5,5,3 .... .... ....

For each subarray, we have to do the sum of the elements of the subarray and then find the subarray which has the largest sum.

If we are entering 8 elements (N = 8), with array element values as -1,-5,5,3,-2,5,4 and 1 then,

**The largest contiguous subarray is**: 5 3 -2 5 4 1

**The sum of the largest contiguous subarray is**: 16

In this program, we will print the contiguous subarray within one dimensional array of numbers which has the largest sum.

We will do this by iterating over every possible contiguous combination of the array using 2 **for** loops.

Then we will compare them to a variable **largest** which is initialized with a value of first element of the array, say **array[0]**. For every contiguous subarray, we will add the elements of that subarray and then compare it with the variable **largest** to find the largest sum and also store the address of the **starting and ending index**.

In the end, we will print the Largest sum and the corresponding subarray.

The sequence of steps for the solution will be as follows:

1. Create an array of user-defined size.

2. Run a **for** loop to read the elements of the array.

3. Considering the first element of the array to be the largest, compare all the contiguous subarray sums, and change the largest value if the largest element is smaller than the current subarray sum.

4. At last, the largest element will hold the actual largest contiguous subarray sum and then print it.

Here is the source code of the C Program to find the sum of contiguous subarray within a 1 – D array of numbers which has the largest sum. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

`/*`

`* C Program to Find the Sum of Contiguous Subarray within a`

`* 1 - D Array of Numbers which has the Largest Sum`

`*/`

`#include<stdio.h>`

int main()

`{`

int size,m=0,l=0;

printf("Type the length of the array\n");

scanf("%d",&size);

int array[size];

printf("type the elements of the array\n");

for(int i=0;i<size;i++)

`{`

scanf("%d",&array[i]);

`}`

int largest=array[0];

for(int i=0;i<size;i++)

`{`

int sum=0;

for(int j=i;j<size;j++)

`{`

sum=sum+array[j];

if(sum>largest)

`{`

m=i;l=j;

largest=sum;

`}`

`}`

`}`

printf("\n The largest contigous subarray is");

for(int z=m;z<=l;z++)

`{`

printf(" %d ",array[z]);

`}`

printf("\n The sum of the largest contigous subarray is");

printf(" %d",largest);

return 0;

`}`

1. Take the size of the array as input from users.

2. Then, Initialize an array of size given by the user.

3. Using for loop, take array element as input from users and insert them into the array.

4. After inserting all the elements of the array, consider the very first element of array to be the largest.

5. Run a for loop, from 1 to arraySize-1, extracting array element one by one.

6. Run another loop inside this loop and sum every possible contiguous subarray.

6. If the largest element is smaller than the sum of the current contiguous subarray, then the largest element is updated to the current sum.

7. In the end, the largest element will hold the actual largest sum.

Here is the runtime output of the C program where the user is reading an array of 8 elements with values as -1,-5,5,3,-2,5,4 and 1 and then it displays the largest contigous subarray with its sum.

Type the length of the array 8 type the elements of the array -1 -5 5 3 -2 5 4 1 The largest contiguous subarray is 5 3 -2 5 4 1 The sum of the largest contiguous subarray is 16

**Sanfoundry Global Education & Learning Series – 1000 C Programs.**

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

**Related Posts:**

- Check C Books
- Practice Computer Science MCQs
- Apply for C Internship
- Check Computer Science Books
- Watch Advanced C Programming Videos