📌  相关文章
📜  通过减少或增加 X 使所有数组元素相等的 X 的最小值

📅  最后修改于: 2021-10-26 05:26:58             🧑  作者: Mango

    给定一个包含N 个元素的数组,您可以对其执行两个操作:
  • 将任何数组元素增加一次X。
  • 将任何数组元素减少X一次。

任务是通过应用操作 1、2 或不应用任何操作来找到X的最小值,使得所有数组元素都相等。如果不能使所有数组元素相等,则打印 -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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程