给定大小为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.
Approach: The idea is based on the observation that whenever a subarray is chosen, either the odd positioned values are changed or the even positioned values in the original array.该问题可以通过在每次操作中贪婪地选择子数组来解决。首先,遍历所有奇数索引,一旦找到偶数就标记子数组的开始,当找到奇数时结束子数组,同时更新操作次数。对偶数索引重复相同的过程。请按照以下步骤解决问题:
- 初始化一个变量,比如flips ,以存储所需的最少操作数。
- 遍历数组arr[] 的偶数索引执行以下步骤:
- 如果当前元素是奇数,则继续迭代。
- 否则,从该索引开始迭代每个第二个元素,直到遇到偶数元素。完成遍历数组后或遇到偶数元素后,将flips增加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)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live