- 给定一个由N个元素组成的数组,您可以对其执行两项操作:
- 将任何数组元素增加X一次。
- 将任何数组元素减少X一次。
任务是找到X的最小值,以使所有数组元素都相等,方法是应用操作1、2或不应用任何操作。如果不能使所有数组元素相等,则打印-1。
例子:
Input: a[] = {1, 4, 4, 7, 4, 1}
Output: 3
Increase the first and last element by 3.
Decrease the fourth element by 3.
Input: {1, 5, 7, 9, 1}
Output: -1
方法:由于两个操作只能在数组元素上应用一次,因此观察到的共同点是,如果存在3个以上的唯一元素,则答案为-1。可能会出现以下三种情况,这些情况可以通过以下方式解决:
- 如果存在3个唯一元素,则如果abs(el2-el1)== abs(el3-el2) ,则答案是abs(el2-el1) 。如果它们不相等,则答案为-1。
- 如果有2个唯一元素,则答案为(el2 – el1)/ 2 ,如果el2 – el1是偶数,否则答案为(el2 – el1)
- 如果只有一个唯一元素,则答案为0
下面是上述方法的实现:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function that returns the
// minimum value of X
int findMinimumX(int a[], int n)
{
// Declare a set
set st;
// Iterate in the array element
// and insert them into the set
for (int i = 0; i < n; i++)
st.insert(a[i]);
// If unique elements is 1
if (st.size() == 1)
return 0;
// Unique elements is 2
if (st.size() == 2) {
// Get both el2 and el1
int el1 = *st.begin();
int el2 = *st.rbegin();
// Check if they are even
if ((el2 - el1) % 2 == 0)
return (el2 - el1) / 2;
else
return (el2 - el1);
}
// If there are 3 unique elements
if (st.size() == 3) {
// Get three unique elements
auto it = st.begin();
int el1 = *it;
it++;
int el2 = *it;
it++;
int el3 = *it;
// Check if their difference is same
if ((el2 - el1) == (el3 - el2))
return el2 - el1;
else
return -1;
}
// More than 3 unique elements
return -1;
}
// Driver code
int main()
{
int a[] = { 1, 4, 4, 7, 4, 1 };
int n = sizeof(a) / sizeof(a[0]);
cout << findMinimumX(a, n);
return 0;
}
Java
// Java implementation of the approach
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
class GFG
{
// Function that returns the
// minimum value of X
static int findMinimumX(int a[], int n)
{
// Declare a set
Set st = new HashSet<>();
// Iterate in the array element
// and insert them into the set
for (int i = 0; i < n; i++)
st.add(a[i]);
// If unique elements is 1
if (st.size() == 1)
return 0;
// Unique elements is 2
if (st.size() == 2)
{
// Get both el2 and el1
Iterator it = st.iterator();
int el1 = it.next();
int el2 = it.next();
// Check if they are even
if ((el2 - el1) % 2 == 0)
return (el2 - el1) / 2;
else
return (el2 - el1);
}
// If there are 3 unique elements
if (st.size() == 3)
{
// Get three unique elements
Iterator it = st.iterator();
int el1 = it.next();
int el2 = it.next();
int el3 = it.next();
// Check if their difference is same
if ((el2 - el1) == (el3 - el2))
return el2 - el1;
else
return -1;
}
// More than 3 unique elements
return -1;
}
// Driver code
public static void main(String[] args)
{
int a[] = {1, 4, 4, 7, 4, 1};
int n = a.length;
System.out.println(findMinimumX(a, n));
}
}
// This code is contributed by
// Rajnis09
Python3
# Python 3 program to implement
# the above approach
# Function that returns the
# minimum value of X
def findMinimumX(a, n):
# Declare a set
st = set()
# Iterate in the array element
# and insert them into the set
for i in range(n):
st.add(a[i])
# If unique elements is 1
if (len(st) == 1):
return 0
# Unique elements is 2
if (len(st) == 2):
# Get both el2 and el1
st = list(st)
el1 = st[0]
el2 = st[1]
# Check if they are even
if ((el2 - el1) % 2 == 0):
return int((el2 - el1) / 2)
else:
return (el2 - el1)
# If there are 3 unique elements
if (len(st) == 3):
st = list(st)
# Get three unique elements
el1 = st[0]
el2 = st[1]
el3 = st[2]
# Check if their difference is same
if ((el2 - el1) == (el3 - el2)):
return el2 - el1
else:
return -1
# More than 3 unique elements
return -1
# Driver code
if __name__ == '__main__':
a = [1, 4, 4, 7, 4, 1]
n = len(a)
print(findMinimumX(a, n))
# This code is contributed by
# Surendra_Gangwar
C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
class GFG
{
// Function that returns the
// minimum value of X
static int findMinimumX(int []a, int n)
{
// Declare a set
List st = new List();
// Iterate in the array element
// and insert them into the set
for (int i = 0; i < n; i++)
if(!st.Contains(a[i]))
st.Add(a[i]);
// If unique elements is 1
if (st.Count == 1)
return 0;
// Unique elements is 2
if (st.Count == 2)
{
// Get both el2 and el1
int el1 = st[0];
int el2 = st[1];
// Check if they are even
if ((el2 - el1) % 2 == 0)
return (el2 - el1) / 2;
else
return (el2 - el1);
}
// If there are 3 unique elements
if (st.Count == 3)
{
// Get three unique elements
int el1 = st[0];
int el2 = st[1];
int el3 = st[2];
// Check if their difference is same
if ((el2 - el1) == (el3 - el2))
return el2 - el1;
else
return -1;
}
// More than 3 unique elements
return -1;
}
// Driver code
public static void Main(String[] args)
{
int []a = {1, 4, 4, 7, 4, 1};
int n = a.Length;
Console.WriteLine(findMinimumX(a, n));
}
}
// This code is contributed by Princi Singh
输出:
3