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:

- 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. - 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; ` `} ` |

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

**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.

## Recommended Posts:

- Maximize the sum of differences of consecutive elements after removing exactly K elements
- Array formed using sum of absolute differences of that element with all other elements
- Smallest subsequence with sum of absolute difference of consecutive elements maximized
- Absolute Difference of all pairwise consecutive elements in an array
- Sum of all differences between Maximum and Minimum of increasing Subarrays
- Minimum elements to be inserted in Array to make adjacent differences equal
- Maximise sum of absolute difference between adjacent elements in Array with sum K
- Count of elements whose absolute difference with the sum of all the other elements is greater than k
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Generate a Binary String without any consecutive 0's and at most K consecutive 1's
- Count possible binary strings of length N without P consecutive 0s and Q consecutive 1s
- Minimum absolute difference between N and a power of 2
- Minimum absolute difference between N and any power of 2
- Generate an alternate odd-even sequence having sum of all consecutive pairs as a perfect square
- Number of possible permutations when absolute difference between number of elements to the right and left are given
- Pair of prime numbers with a given sum and minimum absolute difference
- Length of second longest sequence of consecutive 1s in a binary array
- Construct a sequence from given frequencies of N consecutive integers with unit adjacent difference
- Maximum distance between two elements whose absolute difference is K
- Count array elements that can be represented as sum of at least two consecutive array elements

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.