找出是否可以使用一个外部数字使数组元素相同 |设置 2
给定一个由N个整数组成的数组arr[] ,以下是可以使用任何外部数X执行的三个操作:
- 将X添加到数组元素一次。
- 从数组元素中减去X一次。
- 不对数组元素执行任何操作。
任务是检查是否存在一个数字X ,这样如果对数字X执行上述操作,则数组元素变得相等。如果数字存在,则打印“是”和X的值。否则,打印“否” 。
例子:
Input: arr[] = {2, 3, 3, 4, 2}
Output: Yes 1
Explanation:
Consider the value of X as 1 and increment the array element arr[0](= 2) and arr[4](= 2) by 1, and decrement arr[3](= 4) by 1 modifies the array to {3, 3, 3, 3, 3}.
Therefore, print Yes with the value X as 1.
Input: arr[] = {4, 3, 2, 1}
Output: No
方法:可以根据以下观察解决给定的问题:
- 如果所有数字都相等,则答案为“是”。
- 如果数组中有两个不同的数字,答案是“是”,因为每个不同的数字都可以在运算后转换为另一个整数。
- 如果数组中至少有四个不同的数字,则答案是“否”,因为加法的性质。
- 在其他情况下,如果存在三个不同的数字A < B < C ,则可以通过将所有A增加B - A并将所有C减少C - A来使所有数组元素相等。因此,只有当2 * B等于(C + A) / 2时,答案才会是“是”。
请按照以下步骤解决问题:
- 将3 个变量(例如X、Y和Z )初始化为-1 ,以存储数组arr[]的所有3 个不同整数。
- 使用变量i遍历给定的数组arr[]并执行以下步骤:
- 如果X、Y和Z中的任何一个为-1 ,则将arr[i]分配给该变量。
- 否则,如果X、Y和Z都不等于arr[i] ,则打印“ NO ”并返回。
- 如果X、Y和Z中的任何一个等于-1 ,则打印“ YES ”。
- 将最小元素存储在X中,将次大元素存储在Y中,将最大元素存储在Z中。
- 现在,如果ZY等于(Y – X) ,则打印“ YES ”。否则,打印“ NO ”。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find whether it is possible
// to make array elements equal using
// one external number
void isPossMakeThemEqual(int arr[],
int N)
{
int X = -1;
int Y = -1;
int Z = -1;
// Traverse the array arr[]
for (int i = 0; i < N; i++) {
// If X is equal to -1
if (X == -1) {
// Update the value of X
X = arr[i];
}
// Otherwise, if X is not
// equal to arr[i]
else if (X != arr[i]) {
// If Y is equal to -1
if (Y == -1) {
// Update Y
Y = arr[i];
}
// Otherwise, if Y is not
// equal to arr[i]
else if (Y != arr[i]) {
// If Z is equal to -1
if (Z == -1) {
// Update the value
// of Z
Z = arr[i];
}
// Otherwise If Z is not
// equal to arr[i], then
// there are at least four
// distinct numbers in array
else if (Z != arr[i]) {
cout << "NO";
return;
}
}
}
}
// If Y is equal to -1, then all
// the array elements are equal
if (Y == -1) {
cout << "YES 0";
return;
}
// If Z is equal to -1, then there
// are only two distinct elements
if (Z == -1) {
cout << "YES " << abs(X - Y);
return;
}
int a = X, b = Y, c = Z;
X = min(a, min(b, c));
Z = max(a, max(b, c));
Y = a + b + c - X - Z;
// If Y - X is not equal to Z - Y
if (Y - X != Z - Y) {
cout << "NO";
return;
}
// Finally print "Yes"
cout << "Yes " << (Y - X);
}
// Driver Code
int main()
{
int arr[] = { 2, 3, 3, 4, 2 };
int N = sizeof(arr) / sizeof(arr[0]);
isPossMakeThemEqual(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG {
// Function to find whether it is possible
// to make array elements equal using
// one external number
static void isPossMakeThemEqual(int arr[],
int N)
{
int X = -1;
int Y = -1;
int Z = -1;
// Traverse the array arr[]
for (int i = 0; i < N; i++) {
// If X is equal to -1
if (X == -1) {
// Update the value of X
X = arr[i];
}
// Otherwise, if X is not
// equal to arr[i]
else if (X != arr[i]) {
// If Y is equal to -1
if (Y == -1)
{
// Update Y
Y = arr[i];
}
// Otherwise, if Y is not
// equal to arr[i]
else if (Y != arr[i]) {
// If Z is equal to -1
if (Z == -1) {
// Update the value
// of Z
Z = arr[i];
}
// Otherwise If Z is not
// equal to arr[i], then
// there are at least four
// distinct numbers in array
else if (Z != arr[i]) {
System.out.println("NO");
return;
}
}
}
}
// If Y is equal to -1, then all
// the array elements are equal
if (Y == -1) {
System.out.println("YES 0");
return;
}
// If Z is equal to -1, then there
// are only two distinct elements
if (Z == -1) {
System.out.println("YES "+ Math.abs(X - Y));
return;
}
int a = X, b = Y, c = Z;
X = Math.min(a, Math.min(b, c));
Z = Math.max(a, Math.max(b, c));
Y = a + b + c - X - Z;
// If Y - X is not equal to Z - Y
if (Y - X != Z - Y) {
System.out.println("NO");
return;
}
// Finally print "Yes"
System.out.println("Yes "+(Y - X));
}
// Driver Code
public static void main (String[] args)
{
int arr[] = { 2, 3, 3, 4, 2 };
int N =arr.length;
isPossMakeThemEqual(arr, N);
}
}
// This code is contributed by Potta Lokesh
Python3
# Python 3 program for the above approach
# Function to find whether it is possible
# to make array elements equal using
# one external number
def isPossMakeThemEqual(arr, N):
X = -1
Y = -1
Z = -1
# Traverse the array arr[]
for i in range(N):
# If X is equal to -1
if (X == -1):
# Update the value of X
X = arr[i]
# Otherwise, if X is not
# equal to arr[i]
elif (X != arr[i]):
# If Y is equal to -1
if (Y == -1):
# Update Y
Y = arr[i]
# Otherwise, if Y is not
# equal to arr[i]
elif (Y != arr[i]):
# If Z is equal to -1
if (Z == -1):
# Update the value
# of Z
Z = arr[i]
# Otherwise If Z is not
# equal to arr[i], then
# there are at least four
# distinct numbers in array
elif (Z != arr[i]):
print("NO")
return
# If Y is equal to -1, then all
# the array elements are equal
if (Y == -1):
print("YES 0")
return
# If Z is equal to -1, then there
# are only two distinct elements
if (Z == -1):
print("YES ",abs(X - Y))
return
a = X
b = Y
c = Z
X = min(a, min(b, c))
Z = max(a, max(b, c))
Y = a + b + c - X - Z
# If Y - X is not equal to Z - Y
if (Y - X != Z - Y):
print("NO")
return
# Finally print "Yes"
print("Yes ",(Y - X))
# Driver Code
if __name__ == '__main__':
arr = [2, 3, 3, 4, 2]
N = len(arr)
isPossMakeThemEqual(arr, N)
# This code is contributed by bgangwar59.
C#
// C# program for above approach
using System;
class GFG{
// Function to find whether it is possible
// to make array elements equal using
// one external number
static void isPossMakeThemEqual(int[] arr,
int N)
{
int X = -1;
int Y = -1;
int Z = -1;
// Traverse the array arr[]
for (int i = 0; i < N; i++) {
// If X is equal to -1
if (X == -1) {
// Update the value of X
X = arr[i];
}
// Otherwise, if X is not
// equal to arr[i]
else if (X != arr[i]) {
// If Y is equal to -1
if (Y == -1)
{
// Update Y
Y = arr[i];
}
// Otherwise, if Y is not
// equal to arr[i]
else if (Y != arr[i]) {
// If Z is equal to -1
if (Z == -1) {
// Update the value
// of Z
Z = arr[i];
}
// Otherwise If Z is not
// equal to arr[i], then
// there are at least four
// distinct numbers in array
else if (Z != arr[i]) {
Console.Write("NO");
return;
}
}
}
}
// If Y is equal to -1, then all
// the array elements are equal
if (Y == -1) {
Console.Write("YES 0");
return;
}
// If Z is equal to -1, then there
// are only two distinct elements
if (Z == -1) {
Console.Write("YES "+ Math.Abs(X - Y));
return;
}
int a = X, b = Y, c = Z;
X = Math.Min(a, Math.Min(b, c));
Z = Math.Max(a, Math.Max(b, c));
Y = a + b + c - X - Z;
// If Y - X is not equal to Z - Y
if (Y - X != Z - Y) {
Console.Write("NO");
return;
}
// Finally print "Yes"
Console.Write("Yes "+(Y - X));
}
// Driver Code
public static void Main(String[] args)
{
int[] arr = { 2, 3, 3, 4, 2 };
int N =arr.Length;
isPossMakeThemEqual(arr, N);
}
}
// This code is contributed by mukesh07.
Javascript
输出:
Yes 1
时间复杂度: O(N)
辅助空间: O(1)