给定三个整数 x、 y 和 Z (可以是负数)。任务是找到最小数组的长度,使相邻元素之间的绝对差小于或等于1,数组的第一个元素是x,有一个整数y和最后一个元素z。 例如:
null
输入:x=5,y=7,z=11 产出:7 最小的以5开头,7结尾 11,绝对差为1 是{5,6,7,8,9,10,11}。 输入:x=3,y=1,z=2 产出:4 数组将变成{3,2,1,2}
这个想法是考虑数字线,因为相邻元素之间的差值是1,所以从x到y移动必须覆盖x和y之间的所有数,并用整数z来结束数组,所以从元素y移到元素z。 所以,可以形成的最小阵列的长度将是从x到z覆盖的点的数量。
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; } |
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. |
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 |
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. |
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. ?> |
Javascript
<script> // Javascript 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 + Math.abs(x - y) + Math.abs(y - z); } // Drivers code var x = 3, y = 1, z = 2; document.write( minimumLength(x, y, z)); // This code is contributed by noob2000. </script> |
输出
4
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END