查找分区以最大化左右部分的偶数和奇数总和
给定一个具有N个正整数的数组arr[] ,任务是找到所有索引i (1 ≤ i ≤ N – 1),使得左子数组 [0, i – 1 ] 并且右子数组 [i, n – 1] 中奇数的计数是所有 i 中的最大值。
例子:
Input: arr[] = {1, 3, 4, 5, 6}
Output: 1 3
Explanation: Count of even integers in the range 0 to 0 is 0 and count of odd integers in the range 1 to 4 is 2.
Total = 0 + 2 = 2 (which is maximum for all i’s).
Count of even integers in the range 0 to 2 is 1 and count of odd integers in the range 3 to 4 is 1.
Total = 1 + 1 = 2 (which is maximum for all i’s).
Input: arr[] = {1, 2, 3, 4, 5, 6, 7}
Output: 2 4 6
Explanation: Count of even integers in the range 0 to 1 is 1 and count of odd integer in the range 2 to 6 is 3.
Total = 1 + 3 = 4 (which is maximum for all i’s).
Count of even integers in the range 0 to 3 is 2 and Count of odd integers in the range 4 to 6 is 2.
Total = 2 + 2 = 4 (which is maximum for all i’s).
Count of even integers in the range 0 to 5 is 3 and Count of odd integers in the range 6 to 6 is 1.
Total = 3 + 1 = 4 (which is maximum for all i’s).
朴素方法:对于每个索引,检查左侧的偶数个数和右侧的奇数个数。在这些和导致最大值的索引中找到最大值。请按照以下步骤操作:
- 对于每个索引i :
- 使用嵌套循环迭代左子数组[0, i – 1]并计算左子数组中偶数的数量。
- 类似地,在另一个嵌套循环中,迭代右子数组[i, N – 1]并计算该子数组中奇数的个数。
- 使用一个整数变量来跟踪最大计数总和。
- 将总和与之前的最大总和进行比较
- 如果当前总和大于前一个,则更新最大总和并将i放入结果数组中。
- 如果当前总和等于前一个最大值,则在前一个结果数组中推送此索引 i。
- 返回结果向量。
下面是上述方法的实现
C++
// C++ code to implement the approach
#include
using namespace std;
// Function to find the answer vector
vector solve(vector& vect)
{
// To keep the track
// of final answer
int maximumSum = 0;
// Size of nums
int n = vect.size();
// It keeps the track
// of final answer
vector ans;
// Iterate over the indices
for (int i = 1; i < n; i++) {
// Stores the count of even
// numbers in the left subarray
int countEven = 0;
// Iterate in the left subarray
for (int j = i - 1; j >= 0; j--) {
if (vect[j] % 2 == 0)
countEven++;
}
// Stores the count of even
// numbers in the left subarray
int countOdd = 0;
// Iterate in the right subarray
for (int j = i; j < n; j++) {
if (vect[j] % 2 == 1)
countOdd++;
}
// Stores the sum for current i
int sum = countEven + countOdd;
// If current score
// is greater than
// previous then push
// in the ans array.
if (sum > maximumSum) {
ans = { i };
maximumSum = sum;
}
// If sum is equal to
// maximum sum then
// consider the index i
// with previous max
else if (sum == maximumSum)
ans.push_back(i);
}
return ans;
}
// Function to print answer
void print(vector& ans)
{
int n = ans.size();
for (int i = 0; i < n; i++)
cout << ans[i] << ' ';
}
// Driver code
int main()
{
// Given vector
vector vect = { 1, 2, 3, 4, 5, 6, 7 };
// Function call
vector ans = solve(vect);
print(ans);
return 0;
}
Java
// Java code to implement the approach
import java.io.*;
import java.util.*;
class GFG
{
// Function to find the answer arraylist
public static ArrayList solve(int vect[])
{
// To keep the track
// of final answer
int maximumSum = 0;
// Size of nums
int n = vect.length;
// It keeps the track
// of final answer
ArrayList ans = new ArrayList();
// Iterate over the indices
for (int i = 1; i < n; i++) {
// Stores the count of even
// numbers in the left subarray
int countEven = 0;
// Iterate in the left subarray
for (int j = i - 1; j >= 0; j--) {
if (vect[j] % 2 == 0)
countEven++;
}
// Stores the count of even
// numbers in the left subarray
int countOdd = 0;
// Iterate in the right subarray
for (int j = i; j < n; j++) {
if (vect[j] % 2 == 1)
countOdd++;
}
// Stores the sum for current i
int sum = countEven + countOdd;
// If current score
// is greater than
// previous then push
// in the ans array.
if (sum > maximumSum) {
ans.clear();
ans.add(i);
maximumSum = sum;
}
// If sum is equal to
// maximum sum then
// consider the index i
// with previous max
else if (sum == maximumSum)
ans.add(i);
}
return ans;
}
// Function to print answer
public static void print(ArrayList ans)
{
int n = ans.size();
for (int i = 0; i < n; i++)
System.out.print(ans.get(i) + " ");
}
public static void main(String[] args)
{
int vect[] = { 1, 2, 3, 4, 5, 6, 7 };
// Function call
ArrayList ans = solve(vect);
print(ans);
}
}
// This code is contributed by Rohit Pradhan
Python3
# python3 code to implement the approach
# Function to find the answer vector
def solve(vect):
# To keep the track
# of final answer
maximumSum = 0
# Size of nums
n = len(vect)
# It keeps the track
# of final answer
ans = []
# Iterate over the indices
for i in range(1, n):
# Stores the count of even
# numbers in the left subarray
countEven = 0
# Iterate in the left subarray
for j in range(i-1, -1, -1):
if (vect[j] % 2 == 0):
countEven += 1
# Stores the count of even
# numbers in the left subarray
countOdd = 0
# Iterate in the right subarray
for j in range(i, n):
if (vect[j] % 2 == 1):
countOdd += 1
# Stores the sum for current i
sum = countEven + countOdd
# If current score
# is greater than
# previous then push
# in the ans array.
if (sum > maximumSum):
ans = [i]
maximumSum = sum
# If sum is equal to
# maximum sum then
# consider the index i
# with previous max
elif (sum == maximumSum):
ans.append(i)
return ans
# Function to print answer
def pnt(ans):
n = len(ans)
for i in range(0, n):
print(ans[i], end=' ')
# Driver code
if __name__ == "__main__":
# Given vector
vect = [1, 2, 3, 4, 5, 6, 7]
# Function call
ans = solve(vect)
pnt(ans)
# This code is contributed by rakeshsahni
C#
// C# code to implement the approach
using System;
using System.Collections;
class GFG {
// Function to find the answer vector
static ArrayList solve(ArrayList vect)
{
// To keep the track
// of final answer
int maximumSum = 0;
// Size of nums
int n = vect.Count;
// It keeps the track
// of final answer
ArrayList ans = new ArrayList();
// Iterate over the indices
for (int i = 1; i < n; i++) {
// Stores the count of even
// numbers in the left subarray
int countEven = 0;
// Iterate in the left subarray
for (int j = i - 1; j >= 0; j--) {
if ((int)vect[j] % 2 == 0)
countEven++;
}
// Stores the count of even
// numbers in the left subarray
int countOdd = 0;
// Iterate in the right subarray
for (int j = i; j < n; j++) {
if ((int)vect[j] % 2 == 1)
countOdd++;
}
// Stores the sum for current i
int sum = countEven + countOdd;
// If current score
// is greater than
// previous then push
// in the ans array.
if (sum > maximumSum) {
ans.Clear();
ans.Add(i);
maximumSum = sum;
}
// If sum is equal to
// maximum sum then
// consider the index i
// with previous max
else if (sum == maximumSum)
ans.Add(i);
}
return ans;
}
// Function to print answer
static void print(ArrayList ans)
{
int n = ans.Count;
for (int i = 0; i < n; i++)
Console.Write(ans[i] + " ");
}
// Driver code
public static void Main()
{
// Given vector
ArrayList vect = new ArrayList();
vect.Add(1);
vect.Add(2);
vect.Add(3);
vect.Add(4);
vect.Add(5);
vect.Add(6);
vect.Add(7);
// Function call
ArrayList ans = solve(vect);
print(ans);
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
C++
// C++ code to implement the approach
#include
using namespace std;
vector solve(vector& vect)
{
// The size of the vector
int n = vect.size();
// It keeps the track of the maximumSum
int maximumSum = 0;
// Initialize vectors
vector countEven(n, 0);
vector countOdd(n, 0);
// Traverse and update countEven vector
for (int i = 0; i < n; i++) {
if (i == 0) {
if (vect[i] % 2 == 0)
countEven[i] = 1;
else
countEven[i] = 0;
}
else {
if (vect[i] % 2 == 0)
countEven[i] = countEven[i - 1] + 1;
else
countEven[i] = countEven[i - 1];
}
}
// Traverse and update countOdd vector
for (int i = n - 1; i >= 0; i--) {
if (i == n - 1) {
if (vect[i] % 2 == 1)
countOdd[i] = 1;
else
countOdd[i] = 0;
}
else {
if (vect[i] % 2 == 1)
countOdd[i] = countOdd[i + 1] + 1;
else
countOdd[i] = countOdd[i + 1];
}
}
// ans will store the indices
vector ans;
for (int i = 1; i < n; i++) {
// Calculate current sum
int sum = countEven[i - 1] + countOdd[i];
maximumSum = max(maximumSum, sum);
}
// Iterate over the indices
for (int i = 1; i < n; i++) {
int sum = countEven[i - 1] + countOdd[i];
// If the value of sum is
// equal to maximumSum then
// consider the index i
if (sum == maximumSum)
ans.push_back(i);
}
// Return the ans vector
return ans;
}
// Function to print ans elements
void print(vector& ans)
{
// Number of elements in the answer vector
int n = ans.size();
// Print values
for (int i = 0; i < n; i++)
cout << ans[i] << ' ';
}
// Driver code
int main()
{
// Input vector
vector nums = { 1, 2, 3, 4, 5, 6, 7 };
// Calling solve function
vector ans = solve(nums);
// Print ans elements
print(ans);
return 0;
}
Java
// JAVA code to implement the approach
import java.util.*;
class GFG {
public static ArrayList
solve(ArrayList vect)
{
// The size of the vector
int n = vect.size();
// It keeps the track of the maximumSum
int maximumSum = 0;
// Initialize vectors
int[] countEven = new int[n];
for (int i = 0; i < n; i++) {
countEven[i] = 0;
}
int[] countOdd = new int[n];
for (int i = 0; i < n; i++) {
countOdd[i] = 0;
}
// Traverse and update countEven vector
for (int i = 0; i < n; i++) {
if (i == 0) {
if (vect.get(i) % 2 == 0)
countEven[i] = 1;
else
countEven[i] = 0;
}
else {
if (vect.get(i) % 2 == 0)
countEven[i] = countEven[i - 1] + 1;
else
countEven[i] = countEven[i - 1];
}
}
// Traverse and update countOdd vector
for (int i = n - 1; i >= 0; i--) {
if (i == n - 1) {
if (vect.get(i) % 2 == 1)
countOdd[i] = 1;
else
countOdd[i] = 0;
}
else {
if (vect.get(i) % 2 == 1)
countOdd[i] = countOdd[i + 1] + 1;
else
countOdd[i] = countOdd[i + 1];
}
}
// ans will store the indices
ArrayList ans = new ArrayList<>();
for (int i = 1; i < n; i++) {
// Calculate current sum
int sum = countEven[i - 1] + countOdd[i];
maximumSum = Math.max(maximumSum, sum);
}
// Iterate over the indices
for (int i = 1; i < n; i++) {
int sum = countEven[i - 1] + countOdd[i];
// If the value of sum is
// equal to maximumSum then
// consider the index i
if (sum == maximumSum)
ans.add(i);
}
// Return the ans vector
return ans;
}
// Function to print ans elements
public static void print(ArrayList ans)
{
// Number of elements in the answer vector
int n = ans.size();
// Print values
for (int i = 0; i < n; i++)
System.out.print(ans.get(i) + " ");
}
// Driver code
public static void main(String[] args)
{
// Input vector
ArrayList nums = new ArrayList(
Arrays.asList(1, 2, 3, 4, 5, 6, 7));
// Calling solve function
ArrayList ans = solve(nums);
// Print ans elements
print(ans);
}
}
// This code is contributed by Taranpreet
2 4 6
时间复杂度: O(N 2 )
辅助空间: O(1)
有效方法:我们可以使用前缀和技术来权衡时间。该方法讨论如下:
- 要跟踪任何i的偶数元素从0到i – 1的计数,请声明一个countEven数组。最初用零初始化它。
- 此外,要跟踪任何i的从i到n – 1的奇数元素的计数,请声明一个countOdd数组。最初用零初始化它。
- 然后对于每个 i 总和将是countEven[i-1]+countOdd[i-1]
- 将总和与之前的最大总和进行比较
- 如果当前总和大于前一个,则更新最大总和并将 i 放入结果数组中。
- 如果当前总和等于前一个最大值,则在前一个结果数组中推送此索引 i。
- 完全遍历后返回结果向量。
下面是上述方法的实现。
C++
// C++ code to implement the approach
#include
using namespace std;
vector solve(vector& vect)
{
// The size of the vector
int n = vect.size();
// It keeps the track of the maximumSum
int maximumSum = 0;
// Initialize vectors
vector countEven(n, 0);
vector countOdd(n, 0);
// Traverse and update countEven vector
for (int i = 0; i < n; i++) {
if (i == 0) {
if (vect[i] % 2 == 0)
countEven[i] = 1;
else
countEven[i] = 0;
}
else {
if (vect[i] % 2 == 0)
countEven[i] = countEven[i - 1] + 1;
else
countEven[i] = countEven[i - 1];
}
}
// Traverse and update countOdd vector
for (int i = n - 1; i >= 0; i--) {
if (i == n - 1) {
if (vect[i] % 2 == 1)
countOdd[i] = 1;
else
countOdd[i] = 0;
}
else {
if (vect[i] % 2 == 1)
countOdd[i] = countOdd[i + 1] + 1;
else
countOdd[i] = countOdd[i + 1];
}
}
// ans will store the indices
vector ans;
for (int i = 1; i < n; i++) {
// Calculate current sum
int sum = countEven[i - 1] + countOdd[i];
maximumSum = max(maximumSum, sum);
}
// Iterate over the indices
for (int i = 1; i < n; i++) {
int sum = countEven[i - 1] + countOdd[i];
// If the value of sum is
// equal to maximumSum then
// consider the index i
if (sum == maximumSum)
ans.push_back(i);
}
// Return the ans vector
return ans;
}
// Function to print ans elements
void print(vector& ans)
{
// Number of elements in the answer vector
int n = ans.size();
// Print values
for (int i = 0; i < n; i++)
cout << ans[i] << ' ';
}
// Driver code
int main()
{
// Input vector
vector nums = { 1, 2, 3, 4, 5, 6, 7 };
// Calling solve function
vector ans = solve(nums);
// Print ans elements
print(ans);
return 0;
}
Java
// JAVA code to implement the approach
import java.util.*;
class GFG {
public static ArrayList
solve(ArrayList vect)
{
// The size of the vector
int n = vect.size();
// It keeps the track of the maximumSum
int maximumSum = 0;
// Initialize vectors
int[] countEven = new int[n];
for (int i = 0; i < n; i++) {
countEven[i] = 0;
}
int[] countOdd = new int[n];
for (int i = 0; i < n; i++) {
countOdd[i] = 0;
}
// Traverse and update countEven vector
for (int i = 0; i < n; i++) {
if (i == 0) {
if (vect.get(i) % 2 == 0)
countEven[i] = 1;
else
countEven[i] = 0;
}
else {
if (vect.get(i) % 2 == 0)
countEven[i] = countEven[i - 1] + 1;
else
countEven[i] = countEven[i - 1];
}
}
// Traverse and update countOdd vector
for (int i = n - 1; i >= 0; i--) {
if (i == n - 1) {
if (vect.get(i) % 2 == 1)
countOdd[i] = 1;
else
countOdd[i] = 0;
}
else {
if (vect.get(i) % 2 == 1)
countOdd[i] = countOdd[i + 1] + 1;
else
countOdd[i] = countOdd[i + 1];
}
}
// ans will store the indices
ArrayList ans = new ArrayList<>();
for (int i = 1; i < n; i++) {
// Calculate current sum
int sum = countEven[i - 1] + countOdd[i];
maximumSum = Math.max(maximumSum, sum);
}
// Iterate over the indices
for (int i = 1; i < n; i++) {
int sum = countEven[i - 1] + countOdd[i];
// If the value of sum is
// equal to maximumSum then
// consider the index i
if (sum == maximumSum)
ans.add(i);
}
// Return the ans vector
return ans;
}
// Function to print ans elements
public static void print(ArrayList ans)
{
// Number of elements in the answer vector
int n = ans.size();
// Print values
for (int i = 0; i < n; i++)
System.out.print(ans.get(i) + " ");
}
// Driver code
public static void main(String[] args)
{
// Input vector
ArrayList nums = new ArrayList(
Arrays.asList(1, 2, 3, 4, 5, 6, 7));
// Calling solve function
ArrayList ans = solve(nums);
// Print ans elements
print(ans);
}
}
// This code is contributed by Taranpreet
2 4 6
时间复杂度: 在)
辅助空间: 在)