给定大小为N的数组arr [] ,任务是通过选择arr []的奇数长度子数组使所有数组元素为奇数,并在此子数组中将所有奇数位置的元素递增1 。打印所需的此类操作的计数。
例子:
Input: arr[] = {2, 3, 4, 3, 5, 3, 2}
Output: 2
Explanation:
In first operation, choose the subarray {2, 3, 4} and increment all its elements at odd positions, i.e., 1 and 3 of this subarray. The updated array is {3, 3, 5, 3, 5, 3, 2}.
In second operation, choose the last index which is subarray of length 1 and increment its value. The updated array is {3, 3, 5, 3, 5, 3, 3}
Input: arr[] = {1, 5, 7}
Output: 0
Explanation: Since all array elements are odd, no changes required.
方法:该想法基于以下观察结果:无论何时选择子数组,原始数组中的奇数位置值或偶数位置值都会更改。可以通过在每个操作中贪婪地选择子数组来解决该问题。首先,对所有奇数索引进行迭代,并在找到偶数时立即标记子数组的开始,并在找到奇数时结束子数组,同时更新操作数。对偶数索引重复相同的过程。请按照以下步骤解决问题:
- 初始化一个变量,例如flips ,以存储所需的最少操作数。
- 遍历数组arr []的偶数索引将执行以下步骤:
- 如果当前元素为奇数,则继续迭代。
- 否则,从该索引开始迭代每个第二个元素,直到遇到偶数元素为止。数组的完整遍历后,或者遇到偶数元素时,将翻转翻转1 。
- 对奇数索引也重复步骤2 。
- 完成上述步骤后,根据需要将翻转值打印为最小操作数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count minimum subarrays
// whose odd-indexed elements need to
// be incremented to make array odd
void minOperations(int arr[], int n)
{
// Stores the minimum number of
// operations required
int flips = 0;
// Iterate over even-indices
for (int i = 0; i < n; i += 2) {
// Check if the current
// element is odd
if (arr[i] % 2 == 1) {
// If true, continue
continue;
}
// Otherwise, mark the starting
// of the subarray and iterate
// until i < n and arr[i] is even
while (i < n && arr[i] % 2 == 0) {
i += 2;
}
// Increment number of operations
flips++;
}
// Iterate over odd indexed
// positions of arr[]
for (int i = 1; i < n; i += 2) {
// Check if the current
// element is odd
if (arr[i] % 2 == 1) {
// If true, continue
continue;
}
// Otherwise, mark the starting
// of the subarray and iterate
// until i < n and arr[i] is even
while (i < n && arr[i] % 2 == 0) {
i += 2;
}
// Increment the number
// of operations
flips++;
}
// Print the number of operations
cout << flips;
}
// Driver Code
int main()
{
int arr[] = { 2, 3, 4, 3, 5, 3, 2 };
int N = sizeof(arr) / sizeof(int);
// Function Call
minOperations(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to count minimum subarrays
// whose odd-indexed elements need to
// be incremented to make array odd
static void minOperations(int arr[], int n)
{
// Stores the minimum number of
// operations required
int flips = 0;
// Iterate over even-indices
for(int i = 0; i < n; i += 2)
{
// Check if the current
// element is odd
if (arr[i] % 2 == 1)
{
// If true, continue
continue;
}
// Otherwise, mark the starting
// of the subarray and iterate
// until i < n and arr[i] is even
while (i < n && arr[i] % 2 == 0)
{
i += 2;
}
// Increment number of operations
flips++;
}
// Iterate over odd indexed
// positions of arr[]
for(int i = 1; i < n; i += 2)
{
// Check if the current
// element is odd
if (arr[i] % 2 == 1)
{
// If true, continue
continue;
}
// Otherwise, mark the starting
// of the subarray and iterate
// until i < n and arr[i] is even
while (i < n && arr[i] % 2 == 0)
{
i += 2;
}
// Increment the number
// of operations
flips++;
}
// Print the number of operations
System.out.println(flips);
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 2, 3, 4, 3, 5, 3, 2 };
int N = arr.length;
// Function Call
minOperations(arr, N);
}
}
// This code is contributed by jana_sayantan
C#
// C# program for the above approach
using System;
class GFG
{
// Function to count minimum subarrays
// whose odd-indexed elements need to
// be incremented to make array odd
static void minOperations(int []arr, int n)
{
// Stores the minimum number of
// operations required
int flips = 0;
// Iterate over even-indices
for(int i = 0; i < n; i += 2)
{
// Check if the current
// element is odd
if (arr[i] % 2 == 1)
{
// If true, continue
continue;
}
// Otherwise, mark the starting
// of the subarray and iterate
// until i < n and arr[i] is even
while (i < n && arr[i] % 2 == 0)
{
i += 2;
}
// Increment number of operations
flips++;
}
// Iterate over odd indexed
// positions of []arr
for(int i = 1; i < n; i += 2)
{
// Check if the current
// element is odd
if (arr[i] % 2 == 1)
{
// If true, continue
continue;
}
// Otherwise, mark the starting
// of the subarray and iterate
// until i < n and arr[i] is even
while (i < n && arr[i] % 2 == 0)
{
i += 2;
}
// Increment the number
// of operations
flips++;
}
// Print the number of operations
Console.WriteLine(flips);
}
// Driver Code
public static void Main(String[] args)
{
int []arr = { 2, 3, 4, 3, 5, 3, 2 };
int N = arr.Length;
// Function Call
minOperations(arr, N);
}
}
// This code is contributed by Princi Singh
Python3
# Python3 program for the above approach
# Function to count minimum subarrays
# whose odd-indexed elements need to
# be incremented to make array odd
def minOperations(arr, n) :
# Stores the minimum number of
# operations required
flips = 0;
i = 0;
# Iterate over even-indices
while i < n :
# Check if the current
# element is odd
if (arr[i] % 2 == 1) :
i += 2;
# If true, continue
continue;
# Otherwise, mark the starting
# of the subarray and iterate
# until i < n and arr[i] is even
while (i < n and arr[i] % 2 == 0) :
i += 2;
# Increment number of operations
flips += 1;
i += 2;
# Iterate over odd indexed
# positions of arr[]
i = 1
while i < n :
# Check if the current
# element is odd
if (arr[i] % 2 == 1) :
i += 2;
# If true, continue
continue;
# Otherwise, mark the starting
# of the subarray and iterate
# until i < n and arr[i] is even
while (i < n and arr[i] % 2 == 0) :
i += 2;
# Increment the number
# of operations
flips += 1;
i += 2;
# Print the number of operations
print(flips);
# Driver Code
if __name__ == "__main__" :
arr = [ 2, 3, 4, 3, 5, 3, 2 ];
N = len(arr);
# Function Call
minOperations(arr, N);
# This code is contributed by AnkThon
Javascript
2
时间复杂度: O(N)
辅助空间: O(1)