Skip to content
Related Articles

Related Articles

Sequence with sum K and minimum sum of absolute differences between consecutive elements
  • Last Updated : 15 Jul, 2019

Given two integers N and K, the task is to find a sequence of integers of length N such that the sum of all the elements of the sequence is K and the sum of absolute differences between all consecutive elements is minimum. Print this minimized sum.

Examples:

Input: N = 3, K = 56
Output: 1
The sequence is {19, 19, 18} and the sum of absolute
differences of all the consecutive elements is
|19 – 19| + |19 – 18| = 0 + 1 = 1 which is the minimum possible.

Input: N = 12, K = 48
Output: 0
The sequence is {4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4}.

Approach: There can be two cases:



  1. When K % N = 0 then the answer will be 0 as K can be evenly divided into N parts i.e. every element of the sequence will be equal.
  2. When K % N != 0 then the answer will be 1 because the sequence can be arranged in a way:
    • (K – (K % N)) is divisible by N so it can be divided evenly among all the N parts.
    • And the rest (K % N) value can be divided in such a way to minimize the consecutive absolute difference i.e. add 1 to the first or the last (K % N) elements and the sequence will be of the type {x, x, x, x, y, y, y, y, y} yielding the minimum sum as 1.

Below is the implementation of the above approach:

CPP




// C++ implementation of the approach
#include <iostream>
using namespace std;
  
// Function to return the minimized sum
int minimum_sum(int n, int k)
{
  
    // If k is divisible by n
    // then the answer will be 0
    if (k % n == 0)
        return 0;
  
    // Else the answer will be 1
    return 1;
}
  
// Driver code
int main()
{
    int n = 3, k = 56;
  
    cout << minimum_sum(n, k);
  
    return 0;
}

Java




// Java implementation of the approach
import java.io.*;
  
class GFG 
{
      
// Function to return the minimized sum
static int minimum_sum(int n, int k)
{
  
    // If k is divisible by n
    // then the answer will be 0
    if (k % n == 0)
        return 0;
  
    // Else the answer will be 1
    return 1;
}
  
// Driver code
public static void main (String[] args) 
{
  
    int n = 3, k = 56;
    System.out.println (minimum_sum(n, k));
}
}
  
// This code is contributed By @ajit_23 

Python3




# Python3 implementation of the approach
  
# Function to return the minimized sum
def minimum_sum(n, k):
  
    # If k is divisible by n
    # then the answer will be 0
    if (k % n == 0):
        return 0;
  
    # Else the answer will be 1
    return 1
  
# Driver code
  
n = 3
k = 56
  
print(minimum_sum(n, k))
  
# This code is contributed by mohit kumar 29

C#




// C# implementation of the approach
using System;
  
class GFG
{
          
// Function to return the minimized sum
static int minimum_sum(int n, int k)
{
  
    // If k is divisible by n
    // then the answer will be 0
    if (k % n == 0)
        return 0;
  
    // Else the answer will be 1
    return 1;
}
  
// Driver code
static public void Main ()
{
    int n = 3, k = 56;
    Console.Write(minimum_sum(n, k));
}
}
  
// This code is contributed By Tushil
Output:
1

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :