📌  相关文章
📜  使前 N 个奇数数组中的所有元素相等所需的最少操作

📅  最后修改于: 2021-09-03 03:12:44             🧑  作者: Mango

给定一个由前N 个奇数组成的数组,任务是通过重复选择一对并增加一个元素并将该对中的另一个元素减少1来找到使所有数组元素相等所需的最小操作次数。

例子:

朴素的方法:可以根据以下观察解决给定的问题:

  • 可以观察到,使所有数组元素等于数组的中间元素需要最少的操作次数。
  • 因此,想法是使用变量i遍历数组,并在每次操作中选择索引i(N – i – 1)处的元素,并使它们等于中间元素。

请按照以下步骤解决问题:

  • 初始化一个变量,比如mid ,它存储数组的中间元素。
  • 初始化一个数组arr[] ,将数组元素存储为arr[i] = 2 * i + 1。
  • 找到数组arr[]的总和并将其存储在变量中,例如sum
  • 如果N是奇数,则将mid的值更新为arr[N / 2] 。否则,将mid的值更新为sum / N
  • 初始化一个变量,比如ans0 ,它存储最少的操作数。
  • [0, N/2]范围内遍历给定数组,并将ans的值增加值(mid – arr[i])
  • 完成以上步骤后,打印ans的值作为结果。

下面是上述方法的实现:

C++14
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the minimum number
// of operations required to make the
// array elements equal
int minOperations(int N)
{
     
    // Stores the array elements
    int arr[N];
 
    // Stores the sum of the array
    int sum = 0;
 
    // Iterate over the range [0, N]
    for(int i = 0; i < N; i++)
    {
         
        // Update the value arr[i]
        arr[i] = (2 * i) + 1;
 
        // Increment the sum by
        // the value arr[i]
        sum = sum + arr[i];
    }
 
    // Stores the middle element
    int mid = 0;
 
    // If N is even
    if (N % 2 == 0)
    {
        mid = sum / N;
    }
 
    // Otherwise
    else {
        mid = arr[N / 2];
    }
 
    // Stores the result
    int ans = 0;
 
    // Traverse the range [0, N / 2]
    for(int i = 0; i < N / 2; i++)
    {
         
        // Update the value of ans
        ans += mid - arr[i];
    }
 
    // Return the result
    return ans;
}
     
// Driver Code
int main()
{
    int N = 6;
    cout << minOperations(N);
 
    return 0;
}
 
// This code is contributed by susmitakundugoaldanga


Java
// Java program for the above approach
 
class GFG {
 
    // Function to find the minimum number
    // of operations required to make the
    // array elements equal
    public static int minOperations(int N)
    {
        // Stores the array elements
        int[] arr = new int[N];
 
        // Stores the sum of the array
        int sum = 0;
 
        // Iterate over the range [0, N]
        for (int i = 0; i < N; i++) {
 
            // Update the value arr[i]
            arr[i] = (2 * i) + 1;
 
            // Increment the sum by
            // the value arr[i]
            sum = sum + arr[i];
        }
 
        // Stores the middle element
        int mid = 0;
 
        // If N is even
        if (N % 2 == 0) {
            mid = sum / N;
        }
 
        // Otherwise
        else {
            mid = arr[N / 2];
        }
 
        // Stores the result
        int ans = 0;
 
        // Traverse the range [0, N / 2]
        for (int i = 0; i < N / 2; i++) {
 
            // Update the value of ans
            ans += mid - arr[i];
        }
 
        // Return the result
        return ans;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int N = 6;
        System.out.println(
            minOperations(N));
    }
}


Python3
# Python3 program for the above approach
 
# Function to find the minimum number
# of operations required to make the
# array elements equal
def minOperations(N):
     
    # Stores the array elements
    arr = [0] * N
 
    # Stores the sum of the array
    sum = 0
 
    # Iterate over the range [0, N]
    for i in range(N) :
         
        # Update the value arr[i]
        arr[i] = (2 * i) + 1
 
        # Increment the sum by
        # the value arr[i]
        sum = sum + arr[i]
 
    # Stores the middle element
    mid = 0
 
    # If N is even
    if N % 2 == 0 :
        mid = sum / N
 
    # Otherwise
    else:
        mid = arr[int(N / 2)]
 
    # Stores the result
    ans = 0
 
    # Traverse the range [0, N / 2]
    for i in range(int(N / 2)):
         
        # Update the value of ans
        ans += mid - arr[i]
 
    # Return the result
    return int(ans)
     
# Driver Code
N = 6
print(minOperations(N))
 
# This code is contributed by Dharanendra L V.


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to find the minimum number
// of operations required to make the
// array elements equal
public static int minOperations(int N)
{
     
    // Stores the array elements
    int[] arr = new int[N];
 
    // Stores the sum of the array
    int sum = 0;
 
    // Iterate over the range [0, N]
    for(int i = 0; i < N; i++)
    {
         
        // Update the value arr[i]
        arr[i] = (2 * i) + 1;
 
        // Increment the sum by
        // the value arr[i]
        sum = sum + arr[i];
    }
 
    // Stores the middle element
    int mid = 0;
 
    // If N is even
    if (N % 2 == 0)
    {
        mid = sum / N;
    }
 
    // Otherwise
    else
    {
        mid = arr[N / 2];
    }
 
    // Stores the result
    int ans = 0;
 
    // Traverse the range [0, N / 2]
    for(int i = 0; i < N / 2; i++)
    {
         
        // Update the value of ans
        ans += mid - arr[i];
    }
 
    // Return the result
    return ans;
}
 
// Driver Code
public static void Main(string[] args)
{
    int N = 6;
     
    Console.WriteLine(minOperations(N));
}
}
 
// This code is contributed by ukasp


Javascript


C++
// C++ code for the above approach
#include 
using namespace std;
 
// Function to find the minimum number
    // of operations required to make the
    // array elements equal
    int minOperation(int N)
    {
        // If the value of N is even
        if (N % 2 == 0) {
  
            // Return the value
            return (N / 2) * (N / 2);
        }
  
        // Otherwise, N is odd
        int k = (N - 1) / 2;
  
        // Return the value
        return k * (k + 1);
    }
     
// Driver Code
int main()
{
     int N = 6;
     cout << minOperation(N);
 
    return 0;
}
 
// This code is contributed by code_hunt.


Java
// Java program for the above approach
 
class GFG {
 
    // Function to find the minimum number
    // of operations required to make the
    // array elements equal
    public static int minOperation(int N)
    {
        // If the value of N is even
        if (N % 2 == 0) {
 
            // Return the value
            return (N / 2) * (N / 2);
        }
 
        // Otherwise, N is odd
        int k = (N - 1) / 2;
 
        // Return the value
        return k * (k + 1);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int N = 6;
        System.out.println(
            minOperation(N));
    }
}


C#
// C# program for the above approach
using System;
class GFG{
 
// Function to find the minimum number
// of operations required to make the
// array elements equal
public static int minOperation(int N)
{
     
    // If the value of N is even
    if (N % 2 == 0)
    {
         
        // Return the value
        return (N / 2) * (N / 2);
    }
 
    // Otherwise, N is odd
    int k = (N - 1) / 2;
 
    // Return the value
    return k * (k + 1);
}
 
// Driver code
static void Main()
{
    int N = 6;
     
    Console.WriteLine(minOperation(N));
}
}
 
// This code is contributed by abhinavjain194


Python3
# Python3 program for the above approach
 
# Function to find the minimum number
# of operations required to make the
# array elements equal
def minOperation(N) :
     
    # If the value of N is even
    if (N % 2 == 0) :
   
        # Return the value
        return (N / 2) * (N / 2)
           
    # Otherwise, N is odd
    k = (N - 1) / 2
   
    # Return the value
    return (k * (k + 1))
         
# Driver Code
 
N = 6
print(int(minOperation(N)))
 
# This code is contributed by souravghosh0416.


Javascript


输出:
9

时间复杂度: O(N)
辅助空间: O(N)

高效方法:上述方法可以基于以下观察进行优化:

  • 如果 N 是奇数,则结果是前N / 2 个偶数的总和,即(K * (K + 1)) / 2,其中K = (N / 2)
  • 否则,结果是前K 个奇数的总和,即K * K,其中K = ((N – 1) / 2)

因此,如果N的值是偶数,则打印(N / 2) 2 的值。否则,打印K * (K + 1) / 2 的值,其中K = ((N – 1) / 2)作为结果运算。

下面是上述方法的实现:

C++

// C++ code for the above approach
#include 
using namespace std;
 
// Function to find the minimum number
    // of operations required to make the
    // array elements equal
    int minOperation(int N)
    {
        // If the value of N is even
        if (N % 2 == 0) {
  
            // Return the value
            return (N / 2) * (N / 2);
        }
  
        // Otherwise, N is odd
        int k = (N - 1) / 2;
  
        // Return the value
        return k * (k + 1);
    }
     
// Driver Code
int main()
{
     int N = 6;
     cout << minOperation(N);
 
    return 0;
}
 
// This code is contributed by code_hunt.

Java

// Java program for the above approach
 
class GFG {
 
    // Function to find the minimum number
    // of operations required to make the
    // array elements equal
    public static int minOperation(int N)
    {
        // If the value of N is even
        if (N % 2 == 0) {
 
            // Return the value
            return (N / 2) * (N / 2);
        }
 
        // Otherwise, N is odd
        int k = (N - 1) / 2;
 
        // Return the value
        return k * (k + 1);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int N = 6;
        System.out.println(
            minOperation(N));
    }
}

C#

// C# program for the above approach
using System;
class GFG{
 
// Function to find the minimum number
// of operations required to make the
// array elements equal
public static int minOperation(int N)
{
     
    // If the value of N is even
    if (N % 2 == 0)
    {
         
        // Return the value
        return (N / 2) * (N / 2);
    }
 
    // Otherwise, N is odd
    int k = (N - 1) / 2;
 
    // Return the value
    return k * (k + 1);
}
 
// Driver code
static void Main()
{
    int N = 6;
     
    Console.WriteLine(minOperation(N));
}
}
 
// This code is contributed by abhinavjain194

蟒蛇3

# Python3 program for the above approach
 
# Function to find the minimum number
# of operations required to make the
# array elements equal
def minOperation(N) :
     
    # If the value of N is even
    if (N % 2 == 0) :
   
        # Return the value
        return (N / 2) * (N / 2)
           
    # Otherwise, N is odd
    k = (N - 1) / 2
   
    # Return the value
    return (k * (k + 1))
         
# Driver Code
 
N = 6
print(int(minOperation(N)))
 
# This code is contributed by souravghosh0416.

Javascript


输出:
9

时间复杂度: O(1)
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live