# Largest sum contiguous subarray

If you want to practice data structure and algorithm programs, you can go through 100+ java coding interview questions.

### Problem:

From Wikipedia :
In computer science, the Largest sum contiguous subarray is the task of finding the contiguous subarray within a one-dimensional array of numbers which has the largest sum. For example, for the sequence of values −2, 1, −3, 4, −1, 2, 1, −5, 4; the contiguous subarray with the largest sum is 4, −1, 2, 1, with sum 6.

### Solution :

There are multiple solution to solve this problem:

#### Solution 1:

Use two loops and try each combination of array elements to find maximum sum.
Time complexity : O(N^2)

#### Solution 2:

I have discussed Kadane ‘s algorithm in previous post. You can refer it.
Time complexity : O(N)

#### Solution 3:

Dynamic Programming:
You can use dynamic programming to solve this problem.
Lets say array be arr[] and maximum sum upto index i is maxSum(i)
Logic which can be used for dynamic programming:

So it can be define as
Max sum at index i is maximum of (max sum upto i-1 + current element , current element)
Java code  :

Time complexity : O(N)

### Java Program to find largest sum contiguous subarray:

When you run above program, you will get below output:

import_contacts

## Related Posts

• 03 May

### Make requirements.txt in python

Table of ContentsWhat is the requirements.txt file in Python?Ways to make requirements.txt file in PythonUsing the pip to make requirements.txt in PythonUsing the conda command to make requirements.txt file in PythonUsing the pipreqs package to make requirements.txt file in PythonConclusion What is the requirements.txt file in Python? Every package is also updated regularly and features […]

• 03 May

### Create an Array of 1 to 10 in Python

Table of ContentsIntroductionHow to create an array of 1 to 10 in Python?Using the range() function to create an array of 1 to 10 in Python.Using list comprehension along with the range() function to create an array of 1 to 10 in Python.Using a user-defined function to create an array of 1 to 10 in […]

• 03 May

### Remove Urls from Text in Python

Table of ContentsIntroductionWays to remove URLs from Text in PythonUsing the re.sub() function to remove URLs from Text in PythonUsing the re.findall() function to remove URLs from Text in PythonUsing the re.search() function to remove URLs from Text in PythonUsing the urllib.urlparse class to remove URLs from Text in PythonConclusion In this post, we will […]

• 03 May

### Create a List from 1 to 100 in Python

Table of ContentsWays to create a list from 1 to 100 in PythonUsing the range() function to create a list from 1 to 100 in PythonUsing the numpy.arange() function to create a list from 1 to 100 in PythonUsing the for loop with range() to create a list from 1 to 100 in PythonConclusion In […]

• 23 April

### How to initialize an Array in Constructor in C++

Table of ContentsArraysConstructors of a ClassWays to initialize an array in Constructor in C++Initialize an array in Constructor With std::fill()Initialize an array in Constructor Using for LoopInitialize an array in Constructor Using Member Initializer ListConclusion This article discusses methods of initializing an array in constructor in C++. Let us first discuss arrays and constructors in […]

• 23 April

### Set an Array Equal to Another Array in Java

Table of ContentsSetting an Array Variable Equal to Another Array VariableSet an Array Equal to Another Array in Java Using the clone() MethodSet an Array Equal to Another Array in Java Using the arraycopy() MethodSet an Array Equal to Another Array in Java Using the copyOf() MethodSet an Array Equal to Another Array in Java […]

## Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.