📌  相关文章
📜  通过移位操作最大化奇数和偶数索引数组元素之间的差异

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

给定一个大小为N的数组arr[] ,任务是通过左移或右移数组元素任意次数来最大化偶数索引元素的总和与奇数索引元素的总和之间的绝对差。

例子:

方法:这个想法是最小化偶数或奇数索引数组元素中的任何一个的值,并最大化另一个的值,以最大化它们的绝对差异。请按照以下步骤解决问题:

  • 存在两种可能的情况。要么最小化偶数索引数组元素并最大化奇数索引数组元素,要么最小化奇数索引数组元素并最大化偶数索引数组元素。
  • 为了最小化一个元素,应用所有的移位操作并取最小可能的值。类似地,要最大化一个元素,请应用所有移位操作并取最大可能值。
  • 取两种情况的最大差异。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to minimize array
// elements by shift operations
int minimize(int n)
{
  int optEle = n;
  string strEle = to_string(n);
 
  // For checking all the
  // left shift operations
  for (int idx = 0; idx < strEle.length();idx++)
  {
 
    // Left shift
    int temp = stoi(strEle.substr(idx) +
                    strEle.substr(0, idx));
 
    // Consider the minimum possible value
    optEle = min(optEle, temp);
  }
 
  return optEle;
}
 
// Function to maximize array
// elements by shift operations
int maximize(int n)
{
  int optEle = n;
  string strEle = to_string(n);
 
  // For checking all the
  // left shift operations
  for (int idx = 0; idx < strEle.length();idx++)
  {
 
    // Left shift
    int temp = stoi(strEle.substr(idx) +
                    strEle.substr(0, idx));
 
    // Consider the maximum possible value
    optEle = max(optEle, temp);
  }
 
  return optEle;
}
 
// Function to maximize the absolute
// difference between even and odd
// indexed array elements
void minimumDifference(int arr[], int N)
{
 
  int caseOne = 0;
  int minVal = 0;
  int maxVal = 0;
 
  // To calculate the difference of
  // odd indexed elements
  // and even indexed elements
  for (int i = 0; i < N; i++)
  {
    if (i % 2 == 0)
      minVal += minimize(arr[i]);
    else
      maxVal += maximize(arr[i]);
  }
  caseOne = abs(maxVal - minVal);
  int caseTwo = 0;
  minVal = 0;
  maxVal = 0;
 
  // To calculate the difference
  // between odd and even indexed
  // array elements
  for (int i = 0; i < N; i++)
  {
 
    if (i % 2 == 0)
      maxVal += maximize(arr[i]);
    else
      minVal += minimize(arr[i]);
    caseTwo = abs(maxVal - minVal);
  }
 
  // Print the maximum value
  cout << max(caseOne, caseTwo) << endl;
}
 
// Driver code
int main()
{
  int arr[] = { 332, 421, 215, 584, 232 };
  int N = sizeof(arr) / sizeof(arr[0]);
  minimumDifference(arr, N);
 
  return 0;
}
 
// This code is contributed by divyesh072019.


Java
// Java program for the above approach
import java.io.*;
class GFG
{
 
  // Function to minimize array
  // elements by shift operations
  static int minimize(int n)
  {
    int optEle = n;
    String strEle = Integer.toString(n);
 
    // For checking all the
    // left shift operations
    for (int idx = 0; idx < strEle.length(); idx++)
    {
 
      // Left shift
      int temp
        = Integer.parseInt(strEle.substring(idx)
                           + strEle.substring(0, idx));
 
      // Consider the minimum possible value
      optEle = Math.min(optEle, temp);
    }
 
    return optEle;
  }
 
  // Function to maximize array
  // elements by shift operations
  static int maximize(int n)
  {
    int optEle = n;
    String strEle = Integer.toString(n);
 
    // For checking all the
    // left shift operations
    for (int idx = 0; idx < strEle.length(); idx++)
    {
 
      // Left shift
      int temp
        = Integer.parseInt(strEle.substring(idx)
                           + strEle.substring(0, idx));
 
      // Consider the maximum possible value
      optEle = Math.max(optEle, temp);
    }
    return optEle;
  }
 
  // Function to maximize the absolute
  // difference between even and odd
  // indexed array elements
  static void minimumDifference(int[] arr)
  {
 
    int caseOne = 0;
    int minVal = 0;
    int maxVal = 0;
 
    // To calculate the difference of
    // odd indexed elements
    // and even indexed elements
    for (int i = 0; i < arr.length; i++)
    {
      if (i % 2 == 0)
        minVal += minimize(arr[i]);
      else
        maxVal += maximize(arr[i]);
    }
    caseOne = Math.abs(maxVal - minVal);
    int caseTwo = 0;
    minVal = 0;
    maxVal = 0;
 
    // To calculate the difference
    // between odd and even indexed
    // array elements
    for (int i = 0; i < arr.length; i++)
    {
      if (i % 2 == 0)
        maxVal += maximize(arr[i]);
      else
        minVal += minimize(arr[i]);
      caseTwo = Math.abs(maxVal - minVal);
    }
 
    // Print the maximum value
    System.out.println(Math.max(caseOne, caseTwo));
  }
 
  // Driver Code
  public static void main(String[] args)
  {
 
    // Given array
    int[] arr = { 332, 421, 215, 584, 232 };
    minimumDifference(arr);
  }
}
 
// This code is contributed by Dharanendra L V.


Python3
# Python3 program for the above approach
 
# Function to minimize array
# elements by shift operations
def minimize(n):
  optEle = n
  strEle = str(n)
   
  # For checking all the
  # left shift operations
  for idx in range(len(strEle)):
     
    # Left shift
    temp = int(strEle[idx:] + strEle[:idx])
     
    # Consider the minimum possible value
    optEle = min(optEle, temp)
 
  return optEle
 
# Function to maximize array
# elements by shift operations
def maximize(n):
  optEle = n
  strEle = str(n)
   
  # For checking all the
  # left shift operations
  for idx in range(len(strEle)):
     
    # Left shift
    temp = int(strEle[idx:] + strEle[:idx])
     
    # Consider the maximum possible value
    optEle = max(optEle, temp)
 
  return optEle
 
# Function to maximize the absolute
# difference between even and odd
# indexed array elements
def minimumDifference(arr):
 
  caseOne = 0
  minVal = 0
  maxVal = 0
   
  # To calculate the difference of
  # odd indexed elements
  # and even indexed elements
  for i in range(len(arr)):
    if i % 2:
      minVal += minimize(arr[i])
    else:
      maxVal += maximize(arr[i])
  caseOne = abs(maxVal - minVal)
  caseTwo = 0
  minVal = 0
  maxVal = 0
   
  # To calculate the difference
  # between odd and even indexed
  # array elements
  for i in range(len(arr)):
 
    if i % 2:
      maxVal += maximize(arr[i])
    else:
      minVal += minimize(arr[i])
  caseTwo = abs(maxVal - minVal)
   
  # Print the maximum value
  print (max(caseOne, caseTwo))
     
     
# Given array
arr = [ 332, 421, 215, 584, 232 ]
minimumDifference(arr)


C#
// C# program for the above approach
using System;
class GFG
{
   
    // Function to minimize array
    // elements by shift operations
    static int minimize(int n)
    {
        int optEle = n;
        string strEle = n.ToString();
 
        // For checking all the
        // left shift operations
        for (int idx = 0; idx < strEle.Length;idx++)
        {
 
            // Left shift
            int temp = Int32.Parse(strEle.Substring(idx) +
                                   strEle.Substring(0, idx));
 
            // Consider the minimum possible value
            optEle = Math.Min(optEle, temp);
        }
 
        return optEle;
    }
 
    // Function to maximize array
    // elements by shift operations
    static int maximize(int n)
    {
        int optEle = n;
        string strEle = n.ToString();
 
        // For checking all the
        // left shift operations
        for (int idx = 0; idx < strEle.Length;idx++)
        {
 
            // Left shift
            int temp = Int32.Parse(strEle.Substring(idx) +
                                   strEle.Substring(0, idx));
 
            // Consider the maximum possible value
            optEle = Math.Max(optEle, temp);
        }
 
        return optEle;
    }
 
    // Function to maximize the absolute
    // difference between even and odd
    // indexed array elements
    static void minimumDifference(int[] arr)
    {
 
        int caseOne = 0;
        int minVal = 0;
        int maxVal = 0;
 
        // To calculate the difference of
        // odd indexed elements
        // and even indexed elements
        for (int i = 0; i < arr.Length; i++)
        {
            if (i % 2 == 0)
                minVal += minimize(arr[i]);
            else
                maxVal += maximize(arr[i]);
        }
        caseOne = Math.Abs(maxVal - minVal);
        int caseTwo = 0;
        minVal = 0;
        maxVal = 0;
 
        // To calculate the difference
        // between odd and even indexed
        // array elements
        for (int i = 0; i < arr.Length; i++)
        {
 
            if (i % 2 == 0)
                maxVal += maximize(arr[i]);
            else
                minVal += minimize(arr[i]);
            caseTwo = Math.Abs(maxVal - minVal);
        }
 
        // Print the maximum value
        Console.WriteLine(Math.Max(caseOne, caseTwo));
    }
 
    // Given array
    public static void Main()
    {
        int[] arr = { 332, 421, 215, 584, 232 };
        minimumDifference(arr);
    }
}
 
// This code is contributed by chitranayal.


Javascript


输出:
658

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

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