📌  相关文章
📜  最小化将每个元素的一半移动到两侧以减少 Array

📅  最后修改于: 2022-05-13 01:56:09.997000             🧑  作者: Mango

最小化将每个元素的一半移动到两侧以减少 Array

给定一个大小为N的数组arr[] ,任务是找到将除第一个和最后一个之外的所有 Array 元素减少到 0 所需的最小步骤数,其中在每个步骤中:

  • 选择任何索引 i (0(arr[i])处的元素2
  • 然后将任意两个元素arr[j](其中 0<=j

如果无法根据需要减少给定数组,则返回 -1。

方法:

直觉:上述问题可以借助以下观察来解决:

插图 1:

插图 2:

以下是实施:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to shift the array
int stepsToRearrange(vector& A, int n)
{
    // if A has 3 elements and
    // 2nd element is odd then
    // answer is not possible
    if (n == 3 && (A[1] % 2 == 1))
        return -1;
 
    int steps = 0, countOne = 0;
 
    // Select even numbers and subtract 2
    // Now for the odd number
    // we add one and make them even.
    // so total steps = (a[i]+1)/2
    for (int i = 1; i < n - 1; i++) {
        steps += (A[i] + 1) / 2;
        countOne += (A[i] == 1);
    }
 
    // If all numbers in index 1 to n-2 = 1
    // then answer is not possible
    if (countOne == n - 2)
        return -1;
 
    // return steps
    return steps;
}
 
// Driver Code
int main()
{
    vector arr = { 3, 2, 1, 3, 5, 4 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    cout << stepsToRearrange(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG {
 
  // Function to shift the array
  public static int stepsToRearrange(int A[], int n)
  {
 
    // if A has 3 elements and
    // 2nd element is odd then
    // answer is not possible
    if (n == 3 && (A[1] % 2 == 1))
      return -1;
 
    int steps = 0, countOne = 0;
 
    // Select even numbers and subtract 2
    // Now for the odd number
    // we add one and make them even.
    // so total steps = (a[i]+1)/2
    for (int i = 1; i < n - 1; i++) {
      steps += (A[i] + 1) / 2;
      if (A[i] == 1) {
        countOne += 1;
      }
    }
 
    // If all numbers in index 1 to n-2 = 1
    // then answer is not possible
    if (countOne == n - 2)
      return -1;
 
    // return steps
    return steps;
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int arr[] = { 3, 2, 1, 3, 5, 4 };
    int N = arr.length;
 
    System.out.print(stepsToRearrange(arr, N));
  }
}
 
// This code is contributed by Taranpreet


Python3
# python3 program for the above approach
 
# Function to shift the array
def stepsToRearrange(A, n):
 
    # if A has 3 elements and
    # 2nd element is odd then
    # answer is not possible
    if (n == 3 and (A[1] % 2 == 1)):
        return -1
 
    steps, countOne = 0, 0
 
    # Select even numbers and subtract 2
    # Now for the odd number
    # we add one and make them even.
    # so total steps = (a[i]+1)/2
    for i in range(1, n-1):
        steps += (A[i] + 1) // 2
        countOne += (A[i] == 1)
 
    # If all numbers in index 1 to n-2 = 1
    # then answer is not possible
    if (countOne == n - 2):
        return -1
 
    # return steps
    return steps
 
# Driver Code
if __name__ == "__main__":
 
    arr = [3, 2, 1, 3, 5, 4]
    N = len(arr)
 
    print(stepsToRearrange(arr, N))
 
# This code is contributed by rakeshsahni


C#
// C# program for the above approach
using System;
class GFG {
 
    // Function to shift the array
    static int stepsToRearrange(int[] A, int n)
    {
        // if A has 3 elements and
        // 2nd element is odd then
        // answer is not possible
        if (n == 3 && (A[1] % 2 == 1))
            return -1;
 
        int steps = 0, countOne = 0;
 
        // Select even numbers and subtract 2
        // Now for the odd number
        // we add one and make them even.
        // so total steps = (a[i]+1)/2
        for (int i = 1; i < n - 1; i++) {
            steps += (A[i] + 1) / 2;
            if (A[i] == 1) {
                countOne += 1;
            }
        }
 
        // If all numbers in index 1 to n-2 = 1
        // then answer is not possible
        if (countOne == n - 2)
            return -1;
 
        // return steps
        return steps;
    }
 
    // Driver Code
    public static void Main()
    {
        int[] arr = { 3, 2, 1, 3, 5, 4 };
        int N = arr.Length;
 
        Console.Write(stepsToRearrange(arr, N));
    }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript


输出
7

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