Maximize sum of absolute difference between adjacent elements in Array with sum K
Last Updated :
30 Sep, 2022
Given two integers N and K, the task is to maximize the sum of absolute differences between adjacent elements of an array of length N and sum K.
Examples:
Input: N = 5, K = 10
Output: 20
Explanation:
The array arr[] with sum 10 can be {0, 5, 0, 5, 0}, maximizing the sum of absolute difference of adjacent elements ( 5 + 5 + 5 + 5 = 20)
Input: N = 2, K = 10
Output: 10
Approach:
To maximize the sum of adjacent elements, follow the steps below:
- If N is 2, the maximum sum possible is K by placing K in 1 index and 0 on the other.
- If N is 1, the maximum sum possible will always be 0.
- For all other values of N, the answer will be 2 * K.
Illustration:
For N = 3, the arrangement {0, K, 0} maximizes the sum of absolute difference between adjacent elements to 2 * K.
For N = 4, the arrangement {0, K/2, 0, K/2} or {0, K, 0, 0} maximizes the required sum of absolute difference between adjacent elements to 2 * K.
Below is the implementation of the above approach:
C++
// C++ program to maximize the
// sum of absolute differences
// between adjacent elements
#include <bits/stdc++.h>
using namespace std;
// Function for maximizing the sum
int maxAdjacentDifference(int N, int K)
{
// Difference is 0 when only
// one element is present
// in array
if (N == 1) {
return 0;
}
// Difference is K when
// two elements are
// present in array
if (N == 2) {
return K;
}
// Otherwise
return 2 * K;
}
// Driver code
int main()
{
int N = 6;
int K = 11;
cout << maxAdjacentDifference(N, K);
return 0;
}
Java
// Java program to maximize the
// sum of absolute differences
// between adjacent elements
import java.util.*;
class GFG{
// Function for maximising the sum
static int maxAdjacentDifference(int N, int K)
{
// Difference is 0 when only
// one element is present
// in array
if (N == 1)
{
return 0;
}
// Difference is K when
// two elements are
// present in array
if (N == 2)
{
return K;
}
// Otherwise
return 2 * K;
}
// Driver code
public static void main(String[] args)
{
int N = 6;
int K = 11;
System.out.print(maxAdjacentDifference(N, K));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program to maximize the
# sum of absolute differences
# between adjacent elements
# Function for maximising the sum
def maxAdjacentDifference(N, K):
# Difference is 0 when only
# one element is present
# in array
if (N == 1):
return 0;
# Difference is K when
# two elements are
# present in array
if (N == 2):
return K;
# Otherwise
return 2 * K;
# Driver code
N = 6;
K = 11;
print(maxAdjacentDifference(N, K));
# This code is contributed by Code_Mech
C#
// C# program to maximize the
// sum of absolute differences
// between adjacent elements
using System;
class GFG{
// Function for maximising the sum
static int maxAdjacentDifference(int N, int K)
{
// Difference is 0 when only
// one element is present
// in array
if (N == 1)
{
return 0;
}
// Difference is K when
// two elements are
// present in array
if (N == 2)
{
return K;
}
// Otherwise
return 2 * K;
}
// Driver code
public static void Main(String[] args)
{
int N = 6;
int K = 11;
Console.Write(maxAdjacentDifference(N, K));
}
}
// This code is contributed by 29AjayKumar
JavaScript
<script>
// JavaScript program to maximize the
// sum of absolute differences
// between adjacent elements
// Function for maximising the sum
function maxAdjacentDifference(N, K)
{
// Difference is 0 when only
// one element is present
// in array
if (N == 1)
{
return 0;
}
// Difference is K when
// two elements are
// present in array
if (N == 2)
{
return K;
}
// Otherwise
return 2 * K;
}
// Driver Code
let N = 6;
let K = 11;
document.write(maxAdjacentDifference(N, K));
// This code is contributed by susmitakundugoaldanga.
</script>
Time Complexity: O(1).
Auxiliary Space: O(1)
Similar Reads
Split array into K subarrays with minimum sum of absolute difference between adjacent elements Given an array, arr[] of size N and an integer K, the task is to split the array into K subarrays minimizing the sum of absolute difference between adjacent elements of each subarray. Examples: Input: arr[] = {1, 3, -2, 5, -1}, K = 2Output: 13Explanation: Split the array into following 2 subarrays:
8 min read
Maximum absolute difference between distinct elements in an Array Given an array arr[] of N integers, the task is to find the maximum absolute difference between distinct elements of the array.Examples: Input: arr[] = {12, 10, 9, 45, 2, 10, 10, 45, 10} Output: 10 Explanation: Distinct elements of given array are 12, 9, 2. Therefore, the maximum absolute difference
6 min read
Maximize element at index K in an array with at most sum M and difference between adjacent elements at most 1 Given a positive integer N, the task is to construct an array of length N and find the maximum value at index K such that the sum of all the array elements is at most M and the absolute difference between any two consecutive array elements is at most 1.Examples:Input: N = 3, M = 7, K = 1Output: 3Exp
6 min read
Array element with minimum sum of absolute differences | Set 2 Given an array arr[] consisting of N positive integers, the task is to find an array element X such that sum of its absolute differences with every array element is minimum. Examples: Input: arr[] = {1, 2, 3, 4, 5}Output: 3Explanation: For element arr[0](= 1): |(1 - 1)| + |(2 - 1)| + |(3 - 1)| + |(4
7 min read
Difference between sum of K maximum even and odd array elements Given an array arr[] and a number K, the task is to find the absolute difference of the sum of K maximum even and odd array elements.Note: At least K even and odd elements are present in the array respectively. Examples: Input arr[] = {1, 2, 3, 4, 5, 6}, K = 2Output: 2Explanation:The 2 maximum even
8 min read