Welcome back, dear reader. So, how is it going? In this article, we solve a very interesting problem called K CONCATENATION. So, what does this problem say?
Important Links : Problem Link, Solution Video
K Concatenation
You should name a variable using the same care with which you name a first-born child.
Welcome back, dear reader. So, how is it going? In this article, we solve a very interesting problem called K CONCATENATION. So, what does this problem say?
Important Links : Problem Link, Solution Video
We are given an array arr1. We are also given an integer k. The array, arr2, is formed by concatenating K copies of arr1. For instance if arr1 is {1,2} and k=3, then arr2={1,2,1,2,1,2}. We have to find the largest subarray sum in this arr2. We recommend you refer to the K CONCATENATION VIDEO once to understand the question completely.
We recommend you try to solve this problem on your own first and then refer to the solution to build your problem-solving skills and concepts.
Time Complexity: O(n)
Space Complexity: O(1)
Dear reader, we recommend you refer to the KADANE'S ALGORITHM VIDEO first as it forms the basis for this problem and we are not going to discuss it here.
We hope that you have understood the question well. Let us start by taking an abstract example i.e. let us not consider any values for the arr1 now. Let us say that arr1 is {a,b,c,d,e,f} and it will be concatenated K times.
If k=1, this means that we only have the array, arr1, as the array in which we have to find the largest/maximum subarray sum. So, this can be done very easily using Kadane's Algorithm that we have already studied. So, this is a very simple case.
If k>1, this means that the array in which we have to find the largest subarray sum is a larger array than the arr1 we took. Two possible situations may arise here. Let us discuss both of them.
We have the given array arr1 = {a,b,c,d,e,f}. This is just an abstract example. In reality, we will have an array of integer values. So, if the sum of this array is negative then the largest subarray sum of K such concatenated arrays can never be of the form:
What do we mean by this? If the sum of elements of the given array is negative, then the largest subarray sum of the same array concatenated K times can never be of such a subarray that starts from the first subarray and reaches the last subarray.
Let us prove this by the method of contradiction. Let us say that the largest subarray sum of the given K concatenated arrays is the sum as shown above.
Now, this sum has elements d, e, and f from the first array and the elements a, b from the last array. All the remaining elements of the rest k-2 arrays are also a part of this subarray sum.
So, the total sum will be :
Now, look at the first term of this sum. This can be generated if the subarray is taken from the first two arrays also as shown in the figure below:
Also, we can say that :
So, we have contradicted our own statement that the largest subarray sum will be (d+ e + f + a + b) + (k-2) x sum. Hence, by the method of contradiction, we can say that the largest subarray sum of K concatenated arrays (when the sum of the array is negative) will never be of the form as shown in img-1.
You may try to prove this with real integer values and some value of K as well. The conclusion that we can come up with is simple.
If the sum of the array is negative, find the largest subarray sum in the first two arrays only using Kadane's Algorithm.
The largest subarray sum will be in the first two arrays, this is certain, but, it can also be of two forms as shown in the examples below:
So, if the sum of the array is negative, the largest subarray sum will either be in the first array only or in between the first two arrays and not more than that.
So, we can say that if k>=2, and the sum of the array is negative, the largest subarray sum can be found by applying Kadane's Algorithm in 2 concatenated arrays.
Now, let us see what happens if the sum is positive.
If the sum of the array is positive, the largest subarray sum will always be of the form as shown in img-1 i.e. starting from the first array and reaching the last array.
We can prove this too by the method of contradiction.
Let us say we assume the following marked elements to be the largest subarray sum.
So, the largest subarray sum according to us right now is (d + e + f + a + b). To contradict ourselves, let us consider another case as shown below:
So, the sum of the above subarray that we have considered will be :
If we think carefully, this sum will always be greater than the subarray sum taken by considering just 2 arrays. This is because the sum of the complete array is positive.
Therefore, we have contradicted our own statement. Hence, the largest subarray sum will not be just among the first two arrays. But, how do we prove that it will be the sum of a subarray starting from the first array and reaching the last array?
See, the differentiating factor between these two subarray sums (that we have considered above) is that the second sum has the sum of arrays added to it. Now, this will be maximum if all the arrays in between are added to the sum.
So, the only way to maximize the subarray sum is that we should include all the arrays in between.
So, the largest subarray sum of K concatenated arrays when the sum of the array is greater than 0 is always of the form starting from the first subarray and reaching the last subarray.
Now, if we consider traversing all the arrays in between, that will be a lot of work to do. Rather, we have already generated the formula to do so (img-8).
So, if the sum of an array is greater than zero, the largest subarray sum will be the largest subarray sum of 2 concatenated arrays + (k-2) multiplied by the sum of one array. Why? Think about this!!!!
We have finally reached the following conclusions:
So, dear reader, we hope that you have understood the complete analysis that we have done above. We recommend you refer to the solution video to understand the complete analysis that we have discussed above and clear your doubts regarding it if any.
Now that we have understood the process completely, let us write the code for the same:
java; true";
The above code is written and explained in the solution video. You may refer to it to clear all your doubts regarding the code if any. Let us now analyze the time and space complexity of this code.
The time complexity of this code is O(n) as we are applying Kadane's Algorithm to a maximum of 2n elements i.e. 2 concatenated arrays. We know that Kadane's Algorithm finds the largest subarray sum in O(n) time. So, the time complexity becomes O(n).
We have not used any extra space. Hence the space complexity is O(1).
So, dear reader, we hope that you have understood the above solution. We recommend you refer to the complete solution video once as it will build your concepts really strong. With this, we have completed this problem.
Here are a few suggestions from our side that you don't want to miss: