Sequence with sum K and minimum sum of absolute differences between consecutive elements

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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 Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : mohit kumar 29, jit_t

Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.