Last Updated on April 1, 2022 by Ria Pathak
CONCEPTS USED:
Suffix Sum Arrays
DIFFICULTY LEVEL:
Hard
PROBLEM STATEMENT(
SIMPLIFIED)
:
Given an array of
N
integers and an integerK
, the task is to find the maximum sum taking everyKth
element i.e.
sum
= arr[i] + arr[i + k] + arr[i + 2 *k] + arr[i+ 3 * k] + … arr[i+ q * k]
starting with any
i
value.
See original problem statement here
NOTE:
He can stop moving to (i+k)th index at any time he wishes.
Minimum value that sum can have is zero, it should never become negative throughout the calculation.
For Example:
Input : N = 5, K = 2
A[] = [1, 2, 3, 4, 5]
Output : 9
Explanation : Start with index = 0 and sum = 0
sum = sum + A[0] = 0 + 1 = 1
i = i + 2 = 0 + 2 = 2
sum = sum + A[2] = 2 + 3 = 5
i = i + 2 = 2 + 2 = 4
sum = sum + A[4] = 4 + 5 = 9
i = i + 2 = 4 + 2 = 6
Similarly starting from other indexes and finding sum values, we obtain that
9 is the maximum sum that can be obtained.
BRUTEFORCE METHOD:
Begin with choosing a starting
index
(i = 0), adding value at this
indexto the
sum, comparing
sumwith
max valueand updating
max value. Keep taking
Ksteps and updating
max valuetill
ibecomes equal to
N`.Follow
Step-1
for all suchindexes
and finally find themaximum sum
out of all such sum values.
Time Complexity
of this solution is O(N2) as we will be running two nested loops.
SOLUTIONS:
#includeint main() { int t; scanf("%d",&t); while(t--) { int n,k; scanf("%d %d",&n,&k); int arr[n]; for(int i=0;i max_val) max_val = sum; j += k; } } printf("%d\n",max_val); } return 0; }
#includeusing namespace std; int main() { int t;cin>>t; while(t--) { int n,k;cin>>n>>k; int arr[n]; for(int i=0;i >arr[i]; int max_val = 0; int sum = 0; for(int i=0;i
import java.util.*; import java.io.*; public class Main { public static void main(String args[]) throws IOException { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t!=0) { int n = sc.nextInt(); int k = sc.nextInt(); int arr[] = new int[n]; for(int i=0;imax_val) max_val = sum; j += k; } } System.out.println(max_val); t--; } } }
EFFICIENT METHOD:
It can be solved by using the concept of
Suffix Sum Arrays
where we start iterating the array from right side and keep storing the suffix sum for each (i+k)th element where(i+k)<n
.Finally we find the maximum sum from the
Suffix Sum Array
.
What are Suffix Sum Arrays?
Suffix Sum Arrays
are of same size of the normal array such that the ith index of this array stores the sum of values from ith index value to the last value of the original array i.e.
SuffixArray[i] = A[i] + A[i+1] + .... + A[N-1]
SOLUTIONS:
#includeint main() { int t; scanf("%d",&t); while(t--) { int n,k; scanf("%d %d",&n,&k); int arr[n]; for(int i=0;i =0;i--) { if(i+k < n) { if(arr[i]+sum[i+k]<0) sum[i] = 0; else sum[i] = arr[i]+sum[i+k]; } else { if(arr[i]<0) sum[i] = 0; else sum[i] = arr[i]; } if(sum[i]>max_val) max_val = sum[i]; } printf("%d\n",max_val); } return 0; }
#includeusing namespace std; int main() { int t;cin>>t; while(t--) { int n,k;cin>>n>>k; int arr[n]; for(int i=0;i >arr[i]; } int max_val = 0; int sum[n]; for(int i=0;i =0;i--) { if(i+k < n) { if(arr[i]+sum[i+k]<0) sum[i] = 0; else sum[i] = arr[i]+sum[i+k]; } else { if(arr[i]<0) sum[i] = 0; else sum[i] = arr[i]; } max_val = max(max_val,sum[i]); } cout<
import java.util.*; import java.io.*; public class Main { public static void main(String args[]) throws IOException { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t!=0) { int n = sc.nextInt(); int k = sc.nextInt(); int arr[] = new int[n]; for(int i=0;i=0;i--) { if(i+k < n) { if(arr[i]+sum[i+k]<0) sum[i] = 0; else sum[i] = arr[i]+sum[i+k]; } else { if(arr[i]<0) sum[i] = 0; else sum[i] = arr[i]; } if(sum[i]>max_val) max_val = sum[i]; } System.out.println(max_val); t--; } } }