📌  相关文章
📜  通过增加奇数长度子数组的奇数索引元素,使数组的所有元素都为奇数

📅  最后修改于: 2021-05-19 19:10:27             🧑  作者: Mango

给定大小为N的数组arr [] ,任务是通过选择arr []的奇数长度子数组使所有数组元素为奇数,并在此子数组中将所有奇数位置的元素递增1 。打印所需的此类操作的计数。

例子:

方法:该想法基于以下观察结果:无论何时选择子数组,原始数组中的奇数位置值或偶数位置值都会更改。可以通过在每个操作中贪婪地选择子数组来解决该问题。首先,对所有奇数索引进行迭代,并在找到偶数时立即标记子数组的开始,并在找到奇数时结束子数组,同时更新操作数。对偶数索引重复相同的过程。请按照以下步骤解决问题:

  1. 初始化一个变量,例如flips ,以存储所需的最少操作数。
  2. 遍历数组arr []的偶数索引将执行以下步骤:
    • 如果当前元素为奇数,则继续迭代。
    • 否则,从该索引开始迭代每个第二个元素,直到遇到偶数元素为止。数组的完整遍历后,或者遇到偶数元素时,将翻转翻转1
  3. 对奇数索引也重复步骤2
  4. 完成上述步骤后,根据需要将翻转值打印为最小操作数。

下面是上述方法的实现:

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)