给定大小为N的数组arr [] ,任务是找到在任何索引i处所需的最小递增或递减操作数,以便对于每个i (1≤i
注意:将数组视为基于1的索引。
例子:
Input: arr[] = {3, -4, 5, 0, 1}
Output: 6
Explanation:
Convert the array as {3, -4, 5, -5, 2}. Here, the sum of elements till i is represented as si.
For i = 1, s1 = 3 and s2 = 3 + (-4) = -1. s1 is positive and s2 is negative.
For i=2, s2 = -1 and s3 = 3 + (-4) + 5 = 4. s2 is negative and s3 is positive.
For i = 3, s3 = 4 and s4 = 3 + (-4) + 5 + (-5) = -1. s3 is positive and s4 is negative.
For i = 4, s4 = -1 and s5 = 3 + (-4) + 5 +(-5) + 2 = 1. s4 is negative and s5 is positive.
Input: arr[] = {1, -2, 2, -3}
Output: 0
Explanation:
Given array already satisfies the condition. Therefore, no need to perform any operation.
方法:如果每个i从1到N – 1,则数组将满足条件:
- 如果i为奇数,则从1到i的元素之和为正。
- 如果我是偶数 从1到i的元素之和为负,反之亦然。
尝试上述两种可能性,然后选择操作次数最少的一种。步骤如下:
- 初始化变量num_of_ops = 0,表示到目前为止已完成的操作数。
- 对于任何索引i ,如果i为偶数且元素的总和为1到i 为负数,然后在arr [i]中加上(1+ | sum |)使其为正数。现在从1到i的元素之和为1 。还要在num_of_ops中添加(1+ | sum |) ,以计算操作数。
- 如果i为奇数,并且从1到i的元素之和为正,则从a [i]减去(1+ | sum |)使其为负。现在从1到i的元素之和为-1。还要在num_of_ops中添加(1+ | sum |) 。即,计算操作次数。
- 类似地,求出偶数i,直到i为负的元素之和,以及奇数i直到i为正的元素之和的运算次数。
- 从以上两个过程中选择最少的操作数。
下面是上述方法的实现:
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
6
时间复杂度: O(N)
辅助空间: O(1)