Given three integers **x, y** and **z** (can be negative). The task is to find the length of the smallest array that can be made such that absolute difference between adjacent elements is less than or equal to 1, the first element of the array is x, having one integer y and last element z.

Examples:

Input : x = 5, y = 7, z = 11

Output : 7

The smallest starts with 5, having 7, ends

with 11 and having absolute difference 1

is { 5, 6, 7, 8, 9, 10, 11 }.Input : x = 3, y = 1, z = 2

Output : 4

The array would become { 3, 2, 1, 2 }

The idea is to consider the number line since the difference between adjacent elements is 1 so to move from X to Y all numbers between x and y have to be covered and to end the array with integer z, so move from element y to element z.

So, the length of the smallest array that can be formed will be the number of points that will be covered from x to z i.e.

1 + abs(x - y) + abs(y - z) (1 is added to count point x).

## C++

`// C++ program to find the length of ` `// smallest array begin with x, having y, ` `// ends with z and having absolute difference ` `// between adjacent elements <= 1. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Return the size of smallest ` `// array with given constraint. ` `int` `minimumLength(` `int` `x, ` `int` `y, ` `int` `z) ` `{ ` ` ` `return` `1 + ` `abs` `(x - y) + ` `abs` `(y - z); ` `} ` ` ` `// Drivers code ` `int` `main() ` `{ ` ` ` `int` `x = 3, y = 1, z = 2; ` ` ` `cout << minimumLength(x, y, z); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find the length ` `// of smallest array begin with x, ` `// having y, ends with z and having ` `// absolute difference between ` `// adjacent elements <= 1. ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` ` ` `// Return the size of smallest ` ` ` `// array with given constraint. ` ` ` `static` `int` `minimumLength(` `int` `x, ` ` ` `int` `y, ` `int` `z) ` ` ` `{ ` ` ` `return` `1` `+ Math.abs(x - y) ` ` ` `+ Math.abs(y - z); ` ` ` `} ` ` ` ` ` `// Drivers code ` ` ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `x = ` `3` `, y = ` `1` `, z = ` `2` `; ` ` ` `System.out.println( ` ` ` `minimumLength(x, y, z)); ` ` ` `} ` `} ` ` ` `// This code is contributed by anuj_67. ` |

*chevron_right*

*filter_none*

## Python 3

`# Python 3 program to find ` `# the length of smallest ` `# array begin with x, having ` `# y, ends with z and having ` `# absolute difference between ` `# adjacent elements <= 1. ` ` ` `# Return the size of smallest ` `# array with given constraint. ` `def` `minimumLength(x, y, z): ` ` ` ` ` `return` `(` `1` `+` `abs` `(x ` `-` `y) ` ` ` `+` `abs` `(y ` `-` `z)) ` ` ` `# Drivers code ` `x ` `=` `3` `y ` `=` `1` `z ` `=` `2` `print` `(minimumLength(x, y, z)) ` ` ` `# This code is contributed ` `# by Smitha ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find the length ` `// of smallest array begin with x, ` `// having y, ends with z and having ` `// absolute difference between ` `// adjacent elements <= 1. ` `using` `System; ` `class` `GFG { ` ` ` ` ` `// Return the size of smallest ` ` ` `// array with given constraint. ` ` ` `static` `int` `minimumLength(` `int` `x, ` ` ` `int` `y, ` ` ` `int` `z) ` ` ` `{ ` ` ` `return` `1 + Math.Abs(x - y) ` ` ` `+ Math.Abs(y - z); ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `x = 3, y = 1, z = 2; ` ` ` `Console.WriteLine(minimumLength(x, y, z)); ` ` ` `} ` `} ` ` ` `// This code is contributed by anuj_67. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find the length of ` `// smallest array begin with x, having y, ` `// ends with z and having absolute difference ` `// between adjacent elements <= 1. ` ` ` `// Return the size of smallest ` `// array with given constraint. ` `function` `minimumLength(` `$x` `, ` `$y` `,` `$z` `) ` `{ ` ` ` `return` `1 + ` `abs` `(` `$x` `- ` `$y` `) + ` ` ` `abs` `(` `$y` `- ` `$z` `); ` `} ` ` ` ` ` `// Driver Code ` ` ` `$x` `= 3; ` ` ` `$y` `= 1; ` ` ` `$z` `= 2; ` ` ` `echo` `minimumLength(` `$x` `, ` `$y` `, ` `$z` `); ` ` ` `// This code is contributed by anuj_67. ` `?> ` |

*chevron_right*

*filter_none*

**Output**

4

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:

- Find smallest number with given number of digits and sum of digits under given constraints
- Possible pairs forming a Pythagorean Triple with a given value
- Forming triangles using points on a square
- Minimum colors required such that edges forming cycle do not have same color
- Count of ways to split N into Triplets forming a Triangle
- Count of subarrays forming an Arithmetic Progression (AP)
- Longest subarray forming an Arithmetic Progression (AP)
- Longest subarray forming a Geometic Progression (GP)
- Longest subsequence forming an Arithmetic Progression (AP)
- Add elements of given arrays with given constraints
- Check loop in array according to given constraints
- Count of elements which form a loop in an Array according to given constraints
- Find duplicates under given constraints
- Maximum number of ones in a N*N matrix with given constraints
- Minimum time required to transport all the boxes from source to the destination under the given constraints
- Remove minimum characters from string to split it into three substrings under given constraints
- Check if the Matrix follows the given constraints or not
- Minimum steps to reach end of array under constraints
- Find the smallest and second smallest elements in an array
- Maximum sum of smallest and second smallest in an array

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.