Shortest Sum Contiguous Subarray

Subarray is basically a part of an array or it is an array within another array. Subarray contains contiguous elements whereas the supersequence does not contain the contiguous elements. Here, we will find the sum of the contiguous subarray which is minimum.

Suppose we have given an array containing 'n' integers. The main aim is to find the sum of the elements of the contiguous array which is minimum.

Let's understand through some examples.

Example 1:

Example 2:

There are multiple techniques to solve this problem. The first solution to this problem is brute force approach. In this approach, we find all the possible arrays and then search for the subarray which has the minimum sum.

Algorithm of brute force

The above algorithm generates the time complexity of O(n2). The time complexity is high so we will use another approach to solve the problem. We will use Kaden's algorithm to solve the problem.

Algorithm for the efficient approach

The time complexity of the above algorithm is O(n). It is the best possible time complexity because we need to go the elements only once.

Implementation in C

Output

Shortest Sum Contiguous Subarray




Latest Courses