Maximise sum of absolute difference between adjacent elements in Array with sum K

Given two integers N and K, the task is to maximise 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:



Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to maximize the
// sum of absolute differences
// between adjacent elements
#include <bits/stdc++.h>
using namespace std;
  
// Function for maximising 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;
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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
chevron_right

Output:
22

Time Complexity: O(1).




Article Tags :
Practice Tags :