📌  相关文章
📜  Array 上满足给定条件所需的最小增量/减量操作

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

给定一个大小为N的数组arr[] ,任务是找到在任何索引 i 处所需的最小递增或递减操作次数,使得对于每个i (1 ≤ i < N),如果索引处的元素总和从1i,则从1i + 1的元素之和必须为,反之亦然。

注意:将数组视为基于 1 的索引。

例子:

方法:如果对于从1 到 N – 1 的每个 i,数组将满足条件:

  • 如果 i 是奇数,则从 1 到 i 的元素之和为
  • 如果我是偶数,那么 从 1 到 i 的元素之和为,反之亦然。

尝试以上两种可能性并选择一种操作次数最少的方法。以下是步骤:

  1. 初始化一个变量num_of_ops = 0 表示到目前为止完成的操作数。
  2. 对于任何索引i ,如果i偶数并且从 1 到 i 的元素的总和 ,然后在arr[i] 中添加(1+|sum|)使其为。现在从 1 到 i 的元素总和将为1 。还要在num_of_ops 中添加(1+|sum|)即计算操作次数。
  3. 如果i奇数并且从 1 到 i 的元素之和为,则从a[i] 中减去(1+|sum|)使其为。现在从 1 到 i 的元素总和将为-1。还要在num_of_ops 中添加(1+|sum|) 。即,计算操作次数。
  4. 类似地,求偶数 i 的操作次数,直到 i 的元素总和为负,对于奇数 i 的元素总和直到 i 为正。
  5. 从以上两个程序中选择最少的操作次数。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find minimum number
// of operations to get desired array
int minOperations(int a[], int N)
{
    int num_of_ops1, num_of_ops2, sum;
    num_of_ops1 = num_of_ops2 = sum = 0;
 
    // For even 'i', sum of
    // elements till 'i' is negative
 
    // For odd 'i', sum of
    // elements till 'i' is positive
    for (int i = 0; i < N; i++) {
        sum += a[i];
 
        // If i is even and sum is positive,
        // make it negative by subtracting
        // 1 + |s| from a[i]
        if (i % 2 == 0 && sum >= 0) {
            num_of_ops1 += (1 + abs(sum));
            sum = -1;
        }
 
        // If i is odd and sum is negative,
        // make it positive by
        // adding 1 + |s| into a[i]
        else if (i % 2 == 1 && sum <= 0) {
            num_of_ops1 += (1 + abs(sum));
            sum = 1;
        }
    }
 
    sum = 0;
 
    // For even 'i', the sum of
    // elements till 'i' is positive
 
    // For odd 'i', sum of
    // elements till 'i' is negative
    for (int i = 0; i < N; i++) {
        sum += a[i];
 
        // Check if 'i' is odd and sum is
        // positive, make it negative by
        // subtracting  1 + |s| from a[i]
        if (i % 2 == 1 && sum >= 0) {
            num_of_ops2 += (1 + abs(sum));
            sum = -1;
        }
 
        // Check if 'i' is even and sum
        // is negative, make it positive
        // by adding 1 + |s| into a[i]
        else if (i % 2 == 0 && sum <= 0) {
            num_of_ops2 += (1 + abs(sum));
            sum = 1;
        }
    }
 
    // Return the minimum of the two
    return min(num_of_ops1, num_of_ops2);
}
 
// Driver Code
int main()
{
    // Given array arr[]
    int arr[] = { 3, -4, 5, 0, 1 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    cout << minOperations(arr, N);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG{
 
// Function to find minimum number
// of operations to get desired array
static int minOperations(int a[], int N)
{
    int num_of_ops1, num_of_ops2, sum;
    num_of_ops1 = num_of_ops2 = sum = 0;
 
    // For even 'i', sum of
    // elements till 'i' is negative
 
    // For odd 'i', sum of
    // elements till 'i' is positive
    for (int i = 0; i < N; i++)
    {
        sum += a[i];
 
        // If i is even and sum is positive,
        // make it negative by subtracting
        // 1 + |s| from a[i]
        if (i % 2 == 0 && sum >= 0)
        {
            num_of_ops1 += (1 + Math.abs(sum));
            sum = -1;
        }
 
        // If i is odd and sum is negative,
        // make it positive by
        // adding 1 + |s| into a[i]
        else if (i % 2 == 1 && sum <= 0)
        {
            num_of_ops1 += (1 + Math.abs(sum));
            sum = 1;
        }
    }
 
    sum = 0;
 
    // For even 'i', the sum of
    // elements till 'i' is positive
 
    // For odd 'i', sum of
    // elements till 'i' is negative
    for (int i = 0; i < N; i++)
    {
        sum += a[i];
 
        // Check if 'i' is odd and sum is
        // positive, make it negative by
        // subtracting  1 + |s| from a[i]
        if (i % 2 == 1 && sum >= 0)
        {
            num_of_ops2 += (1 + Math.abs(sum));
            sum = -1;
        }
 
        // Check if 'i' is even and sum
        // is negative, make it positive
        // by adding 1 + |s| into a[i]
        else if (i % 2 == 0 && sum <= 0)
        {
            num_of_ops2 += (1 + Math.abs(sum));
            sum = 1;
        }
    }
 
    // Return the minimum of the two
    return Math.min(num_of_ops1, num_of_ops2);
}
 
// Driver Code
public static void main(String[] args)
{
    // Given array arr[]
    int arr[] = { 3, -4, 5, 0, 1 };
    int N = arr.length;
 
    // Function Call
    System.out.print(minOperations(arr, N));
}
}
 
// This code is contributed by Amit Katiyar


Python3
# Python3 program for the above approach
 
# Function to find minimum number
# of operations to get desired array
def minOperations(a, N):
 
    num_of_ops1 = num_of_ops2 = sum = 0;
 
    # For even 'i', sum of
    # elements till 'i' is negative
 
    # For odd 'i', sum of
    # elements till 'i' is positive
    for i in range(N):
        sum += a[i]
 
        # If i is even and sum is positive,
        # make it negative by subtracting
        # 1 + |s| from a[i]
        if (i % 2 == 0 and sum >= 0):
            num_of_ops1 += (1 + abs(sum))
            sum = -1
 
        # If i is odd and sum is negative,
        # make it positive by
        # adding 1 + |s| into a[i]
        elif (i % 2 == 1 and sum <= 0):
            num_of_ops1 += (1 + abs(sum))
            sum = 1
 
    sum = 0
 
    # For even 'i', the sum of
    # elements till 'i' is positive
 
    # For odd 'i', sum of
    # elements till 'i' is negative
    for i in range (N):
        sum += a[i]
 
        # Check if 'i' is odd and sum is
        # positive, make it negative by
        # subtracting 1 + |s| from a[i]
        if (i % 2 == 1 and sum >= 0):
            num_of_ops2 += (1 + abs(sum))
            sum = -1
 
        # Check if 'i' is even and sum
        # is negative, make it positive
        # by adding 1 + |s| into a[i]
        elif (i % 2 == 0 and sum <= 0):
            num_of_ops2 += (1 + abs(sum))
            sum = 1
 
    # Return the minimum of the two
    return min(num_of_ops1, num_of_ops2)
 
# Driver Code
if __name__ == "__main__":
     
    # Given array arr[]
    arr = [ 3, -4, 5, 0, 1 ]
    N = len(arr)
 
    # Function call
    print(minOperations(arr, N))
 
# This code is contributed by chitranayal


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to find minimum number
// of operations to get desired array
static int minOperations(int []a, int N)
{
    int num_of_ops1, num_of_ops2, sum;
    num_of_ops1 = num_of_ops2 = sum = 0;
 
    // For even 'i', sum of
    // elements till 'i' is negative
 
    // For odd 'i', sum of
    // elements till 'i' is positive
    for(int i = 0; i < N; i++)
    {
        sum += a[i];
 
        // If i is even and sum is positive,
        // make it negative by subtracting
        // 1 + |s| from a[i]
        if (i % 2 == 0 && sum >= 0)
        {
            num_of_ops1 += (1 + Math.Abs(sum));
            sum = -1;
        }
 
        // If i is odd and sum is negative,
        // make it positive by
        // adding 1 + |s| into a[i]
        else if (i % 2 == 1 && sum <= 0)
        {
            num_of_ops1 += (1 + Math.Abs(sum));
            sum = 1;
        }
    }
 
    sum = 0;
 
    // For even 'i', the sum of
    // elements till 'i' is positive
 
    // For odd 'i', sum of
    // elements till 'i' is negative
    for(int i = 0; i < N; i++)
    {
        sum += a[i];
 
        // Check if 'i' is odd and sum is
        // positive, make it negative by
        // subtracting 1 + |s| from a[i]
        if (i % 2 == 1 && sum >= 0)
        {
            num_of_ops2 += (1 + Math.Abs(sum));
            sum = -1;
        }
 
        // Check if 'i' is even and sum
        // is negative, make it positive
        // by adding 1 + |s| into a[i]
        else if (i % 2 == 0 && sum <= 0)
        {
            num_of_ops2 += (1 + Math.Abs(sum));
            sum = 1;
        }
    }
 
    // Return the minimum of the two
    return Math.Min(num_of_ops1, num_of_ops2);
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given array []arr
    int []arr = { 3, -4, 5, 0, 1 };
    int N = arr.Length;
 
    // Function call
    Console.Write(minOperations(arr, N));
}
}
 
// This code is contributed by PrinciRaj1992


Javascript


输出:
6

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

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