Find minimum value of K to maximise sum of elements on indices that are multiples of K
Last Updated :
04 Dec, 2023
Given an array arr[] of N integers, the task is to find the minimum value of K such that the sum of elements on indices that are multiples of K is the maximum possible.
Example:
Input: arr[] = {-3, 4}
Output: 2
Explanation: For the given array, it the value of K = 1, then the multiples of K are {1, 2} which sums up to arr[1] + arr[2] = -3 + 4 = 1. For K = 2, the valid multiple of K is 2 and hence the sum is arr[2] = 4, which is the maximum possible. Hence, K = 2 is a valid answer.
Input: arr[] = {-1, -2, -3}
Output: 2
Approach: The given problem can be solved by a similar to that of the Sieve of Eratosthenes. The idea is to calculate the sum for all possible values of K in the range [1, N] by iterating over each multiple of K as done while marking the non-prime elements in the Sieve. The value of K giving the maximum sum is the required answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxSum( int arr[], int N)
{
int maxSum = INT_MIN, ans = -1;
for ( int K = 1; K <= N; K++) {
int sum = 0;
for ( int i = K; i <= N; i += K) {
sum += arr[i - 1];
}
if (sum > maxSum) {
maxSum = sum;
ans = K;
}
}
return ans;
}
int main()
{
int arr[] = { -1, -2, -3 };
int N = sizeof (arr) / sizeof ( int );
cout << maxSum(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int maxSum( int arr[], int N)
{
int maxSum = Integer.MIN_VALUE, ans = - 1 ;
for ( int K = 1 ; K <= N; K++)
{
int sum = 0 ;
for ( int i = K; i <= N; i += K)
{
sum += arr[i - 1 ];
}
if (sum > maxSum)
{
maxSum = sum;
ans = K;
}
}
return ans;
}
public static void main(String args[])
{
int arr[] = { - 1 , - 2 , - 3 };
int N = arr.length;
System.out.println(maxSum(arr, N));
}
}
|
Python3
import sys
def maxSum(arr, N):
maxSum = - sys.maxsize;
ans = - 1 ;
for K in range ( 1 ,N + 1 ):
sum = 0 ;
for i in range (K, N + 1 ,K):
sum + = arr[i - 1 ];
if ( sum > maxSum):
maxSum = sum ;
ans = K;
return ans;
if __name__ = = '__main__' :
arr = [ - 1 , - 2 , - 3 ];
N = len (arr);
print (maxSum(arr, N));
|
C#
using System;
public class GFG{
static int maxSum( int []arr, int N)
{
int maxSum = int .MinValue, ans = -1;
for ( int K = 1; K <= N; K++)
{
int sum = 0;
for ( int i = K; i <= N; i += K)
{
sum += arr[i - 1];
}
if (sum > maxSum)
{
maxSum = sum;
ans = K;
}
}
return ans;
}
public static void Main(String []args)
{
int []arr = { -1, -2, -3 };
int N = arr.Length;
Console.WriteLine(maxSum(arr, N));
}
}
|
Javascript
<script>
function maxSum(arr, N) {
let maxSum = -999999;
let ans = -1;
for (let K = 1; K <= N; K++) {
let sum = 0;
for (let i = K; i <= N; i += K) {
sum = sum + arr[i - 1];
}
if (sum > maxSum) {
maxSum = sum;
ans = K;
}
}
return ans;
}
let arr = [-1, -2, -3];
let N = arr.length;
document.write(maxSum(arr, N));
</script>
|
Time Complexity: O(N*log N)
Auxiliary space: O(1)
Another Approach:
- First, we calculate the prefix sum of the given array. Let’s say prefixSum[i] stores the sum of the first i elements of the array.
- Then, we iterate over all possible values of K in the range [1, N] and calculate the sum of elements on indices that are multiples of K. This can be done in constant time by using prefixSum array. Let’s say this sum is sumK.
- We update the maximum sum and the corresponding value of K whenever we find a new maximum sum.
- Finally, we return the value of K that gave us the maximum sum.
Below is the implementation of the above approach:
C++
#include <iostream>
#include <vector>
using namespace std;
int maxSum(vector< int >& arr) {
int n = arr.size();
vector< int > prefixSum(n+1, 0);
for ( int i=1; i<=n; i++) {
prefixSum[i] = prefixSum[i-1] + arr[i-1];
}
int maxSum = -999999999, ans = -1;
for ( int K=1; K<=n; K++) {
int sumK = 0;
for ( int i=K; i<=n; i+=K) {
sumK += prefixSum[i] - prefixSum[i-K];
}
if (sumK > maxSum) {
maxSum = sumK;
ans = K;
}
}
return ans;
}
int main() {
vector< int > arr = {-1, -2, -3};
cout << maxSum(arr) << endl;
return 0;
}
|
Java
import java.util.ArrayList;
import java.util.List;
public class MaxSumFinder {
public static int maxSum(List<Integer> arr) {
int n = arr.size();
List<Integer> prefixSum = new ArrayList<>(n + 1 );
for ( int i = 0 ; i <= n; i++) {
prefixSum.add( 0 );
}
for ( int i = 1 ; i <= n; i++) {
prefixSum.set(i, prefixSum.get(i - 1 ) + arr.get(i - 1 ));
}
int maxSum = Integer.MIN_VALUE;
int ans = - 1 ;
for ( int K = 1 ; K <= n; K++) {
int sumK = 0 ;
for ( int i = K; i <= n; i += K) {
sumK += prefixSum.get(i) - prefixSum.get(i - K);
}
if (sumK > maxSum) {
maxSum = sumK;
ans = K;
}
}
return ans;
}
public static void main(String[] args) {
List<Integer> arr = List.of(- 1 , - 2 , - 3 );
System.out.println(maxSum(arr));
}
}
|
Python3
def maxSum(arr):
n = len (arr)
prefixSum = [ 0 ] * (n + 1 )
for i in range ( 1 , n + 1 ):
prefixSum[i] = prefixSum[i - 1 ] + arr[i - 1 ]
maxSum = - 999999999
ans = - 1
for K in range ( 1 , n + 1 ):
sumK = 0
for i in range (K, n + 1 , K):
sumK + = prefixSum[i] - prefixSum[i - K]
if sumK > maxSum:
maxSum = sumK
ans = K
return ans
if __name__ = = "__main__" :
arr = [ - 1 , - 2 , - 3 ]
result = maxSum(arr)
print ( result)
|
C#
using System;
using System.Collections.Generic;
class Program
{
static int MaxSum(List< int > arr)
{
int n = arr.Count;
List< int > prefixSum = new List< int >(n + 1);
for ( int i = 0; i <= n; i++)
{
prefixSum.Add(0);
}
for ( int i = 1; i <= n; i++)
{
prefixSum[i] = prefixSum[i - 1] + arr[i - 1];
}
int maxSum = int .MinValue;
int ans = -1;
for ( int K = 1; K <= n; K++)
{
int sumK = 0;
for ( int i = K; i <= n; i += K)
{
sumK += prefixSum[i] - prefixSum[i - K];
}
if (sumK > maxSum)
{
maxSum = sumK;
ans = K;
}
}
return ans;
}
static void Main()
{
List< int > arr = new List< int > { -1, -2, -3 };
Console.WriteLine(MaxSum(arr));
}
}
|
Javascript
function maxSum(arr) {
const n = arr.length;
const prefixSum = new Array(n + 1).fill(0);
for (let i = 1; i <= n; i++) {
prefixSum[i] = prefixSum[i - 1] + arr[i - 1];
}
let maxSum = Number.MIN_SAFE_INTEGER;
let ans = -1;
for (let K = 1; K <= n; K++) {
let sumK = 0;
for (let i = 0; i < n; i++) {
if ((i + 1) % K === 0) {
sumK += arr[i];
}
}
if (sumK > maxSum) {
maxSum = sumK;
ans = K;
}
}
return ans;
}
const arr = [-1, -2, -3];
console.log(maxSum(arr));
|
Time Complexity: O(n*logn)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...