Optimizing K for Maximum Sum in Array
Last Updated :
02 Dec, 2023
Given an array A[] of size N. You have to choose a number K between 1 and N (inclusive). Your score will be the sum of all the elements A[i] such that i is a multiple of K, the task is to find the minimum value of K with the maximum sum.
Examples:
Input: N = 2, A[] = {-3, 4}
Output: 2
Explanation: If we take K = 1, the score would be A1 + A2 = -3 + 4 = 1 but if we take K = 2 the score would be A2 = 4. So 2 is the correct choice.
Input: N = 3, A[] = {1, 2, 4 }
Output: 1
Explanation: Optimal to choose K = 1, such that sum becomes 1 + 2 + 4 = 7
Approach: This can be solved with the following idea:
Iterating in array A, for each i we have to calculate the sum of elements involved in each iteration. Store that sum in the maximum sum.
Below are the steps involved:
- Iterate in Array A, where i would be the value for K.
- For each i, run a loop for where starting would be j = i -1 and increment the value by i.
- Store the value of elements in sum. Update the value sum in max and update k accordingly.
Below is the implementation of the code:
C++
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
long long solve( int n, vector< long long > A)
{
long long ans = -1e18;
long long k = 0;
for ( int i = 1; i <= n; i++) {
long long sum = 0;
for ( int j = i - 1; j < n; j = j + i) {
sum = sum + A[j];
}
if (sum > ans) {
k = i;
ans = sum;
}
}
return k;
}
int main()
{
int N = 5;
vector< long long > A = { 1, 2, 3, 3, 1 };
cout << solve(N, A);
return 0;
}
|
Java
import java.util.*;
public class Main {
static long solve( int n, ArrayList<Long> A) {
long ans = Long.MIN_VALUE;
long k = 0 ;
for ( int i = 1 ; i <= n; i++) {
long sum = 0 ;
for ( int j = i - 1 ; j < n; j = j + i) {
sum = sum + A.get(j);
}
if (sum > ans) {
k = i;
ans = sum;
}
}
return k;
}
public static void main(String[] args) {
int N = 5 ;
ArrayList<Long> A = new ArrayList<>(Arrays.asList(1L, 2L, 3L, 3L, 1L));
System.out.println(solve(N, A));
}
}
|
Python3
def solve(n, A):
ans = - 1e18
k = 0
for i in range ( 1 , n + 1 ):
sum = 0
for j in range (i - 1 , n, i):
sum = sum + A[j]
if sum > ans:
k = i
ans = sum
return k
N = 5
A = [ 1 , 2 , 3 , 3 , 1 ]
print (solve(N, A))
|
C#
using System;
public class MaxSumKValue
{
public static int Solve( int n, int [] A)
{
long ans = long .MinValue;
int k = 0;
for ( int i = 1; i <= n; i++)
{
long sum = 0;
for ( int j = i - 1; j < n; j += i)
{
sum += A[j];
}
if (sum > ans)
{
k = i;
ans = sum;
}
}
return k;
}
public static void Main( string [] args)
{
const int N = 5;
int [] A = { 1, 2, 3, 3, 1 };
int result = Solve(N, A);
Console.WriteLine(result);
}
}
|
Javascript
function solve(n, A) {
let ans = -1e18;
let k = 0;
for (let i = 1; i <= n; i++) {
let sum = 0;
for (let j = i - 1; j < n; j += i) {
sum += A[j];
}
if (sum > ans) {
k = i;
ans = sum;
}
}
return k;
}
const N = 5;
const A = [1, 2, 3, 3, 1];
const result = solve(N, A);
console.log(result);
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...