给定一个由N个正整数组成的数组arr [] ,任务是即使将任意一对数组元素替换为它们的总和,也要制作所有数组元素。
例子:
Input: arr[] = {5, 6, 3, 7, 20}
Output: 3
Explanation:
Operation 1: Replace arr[0] and arr[2] by their sum ( = 5 + 3 = 8) modifies arr[] to {8, 6, 8, 7, 20}.
Operation 2: Replace arr[2] and arr[3] by their sum ( = 7 + 8 = 15) modifies arr[] to {8, 6, 15, 15, 20}.
Operation 3: Replace arr[2] and arr[3] by their sum ( = 15 + 15 = 30) modifies arr[] to {8, 6, 30, 30, 20}.
Input: arr[] = {2, 4, 16, 8, 7, 9, 3, 1}
Output: 2
方法:这个想法是用两个和数组替换它们的和直到所有的数组元素都是偶数。请按照以下步骤解决问题:
- 初始化一个变量,例如move ,以存储所需的最少替换次数。
- 计算给定数组中存在的奇数元素总数,并将其存储在变量中,例如cnt 。
- 如果cnt的值是奇数,则打印(cnt / 2 + 2)作为结果。否则,打印cnt / 2作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the minimum number
// of replacements required to make
// all array elements even
void minMoves(int arr[], int N)
{
// Stores the count of odd elements
int odd_element_cnt = 0;
// Traverse the array
for (int i = 0; i < N; i++) {
// Increase count of odd elements
if (arr[i] % 2 != 0) {
odd_element_cnt++;
}
}
// Store number of replacements required
int moves = (odd_element_cnt) / 2;
// Two extra moves will be required
// to make the last odd element even
if (odd_element_cnt % 2 != 0)
moves += 2;
// Print the minimum replacements
cout << moves;
}
// Driver Code
int main()
{
int arr[] = { 5, 6, 3, 7, 20 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function call
minMoves(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find the minimum number
// of replacements required to make
// all array elements even
static void minMoves(int arr[], int N)
{
// Stores the count of odd elements
int odd_element_cnt = 0;
// Traverse the array
for (int i = 0; i < N; i++)
{
// Increase count of odd elements
if (arr[i] % 2 != 0)
{
odd_element_cnt++;
}
}
// Store number of replacements required
int moves = (odd_element_cnt) / 2;
// Two extra moves will be required
// to make the last odd element even
if (odd_element_cnt % 2 != 0)
moves += 2;
// Print the minimum replacements
System.out.print(moves);
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 5, 6, 3, 7, 20 };
int N = arr.length;
// Function call
minMoves(arr, N);
}
}
// This code is contributed by shikhasingrajput
C#
// C# program for the above approach
using System;
public class GFG
{
// Function to find the minimum number
// of replacements required to make
// all array elements even
static void minMoves(int []arr, int N)
{
// Stores the count of odd elements
int odd_element_cnt = 0;
// Traverse the array
for (int i = 0; i < N; i++)
{
// Increase count of odd elements
if (arr[i] % 2 != 0)
{
odd_element_cnt++;
}
}
// Store number of replacements required
int moves = (odd_element_cnt) / 2;
// Two extra moves will be required
// to make the last odd element even
if (odd_element_cnt % 2 != 0)
moves += 2;
// Print the minimum replacements
Console.Write(moves);
}
// Driver Code
public static void Main(String[] args)
{
int []arr = { 5, 6, 3, 7, 20 };
int N = arr.Length;
// Function call
minMoves(arr, N);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python program for the above approach
# Function to find the minimum number
# of replacements required to make
# all array elements even
def minMoves(arr, N):
# Stores the count of odd elements
odd_element_cnt = 0;
# Traverse the array
for i in range(N):
# Increase count of odd elements
if (arr[i] % 2 != 0):
odd_element_cnt += 1;
# Store number of replacements required
moves = (odd_element_cnt) // 2;
# Two extra moves will be required
# to make the last odd element even
if (odd_element_cnt % 2 != 0):
moves += 2;
# Prthe minimum replacements
print(moves);
# Driver Code
if __name__ == '__main__':
arr = [5, 6, 3, 7, 20];
N = len(arr);
# Function call
minMoves(arr, N);
# This code is contributed by 29AjayKumar
Javascript
输出:
3
时间复杂度: O(N)
辅助空间: O(1)