Sequence with sum K and minimum sum of absolute differences between consecutive elements Last Updated : 12 Jul, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report 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: C++ // 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 JavaScript <script> // Javascript implementation of the approach // Function to return the minimized sum function 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 let n = 3, k = 56; document.write(minimum_sum(n, k)); // This code is contributed by rishavmahato348 </script> Output: 1 Time Complexity: O(1) Auxiliary Space: O(1), since no extra space has been taken. Comment More infoAdvertise with us Next Article Sequence with sum K and minimum sum of absolute differences between consecutive elements G gp6 Follow Improve Article Tags : Misc Mathematical DSA Practice Tags : MathematicalMisc Similar Reads Sum of minimum difference between consecutive elements of an array Given an array of pairs where each pair represents a range, the task is to find the sum of the minimum difference between the consecutive elements of an array where the array is filled in the below manner: Each element of an array lies in the range given at its corresponding index in the range array 10 min read 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 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 Maximize sum of absolute difference between adjacent elements in Array with sum K 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 adj 4 min read Cost of creating smallest subsequence with sum of difference between adjacent elements maximum Given two array of N integers arr[] and costArray[], representing removal cost associated with each element. The task is to find the subsequence from the given array of minimum cost such that the sum of the difference between adjacent elements is maximum. On removing each element, cost is incurred.E 10 min read Like