通过将长度至少为 K 的由偶数组成的子数组替换为其长度来减少给定数组
给定一个长度为N的数组arr[] ,如果长度大于或等于K ,任务是用它们的长度替换所有只有偶数元素的子数组。
例子:
Input: arr[] = {3, 6, 10, 2, 7, 6, 4, 8}, K = 2
Output: 3 3 7 3
Explanation: There are two subarrays with consecutive even numbers {6, 10, 2} and {6, 4, 8}.
So they are replaced by their length 3 and 3 respectively.
Input: arr[] = {4, 8, 3, 6, 8}, K=3
Output: 4 8 3 6 8
Explanation: No subarray exists for which length of consecutive even numbers is greater than or equal to 3.
方法:按照以下步骤来回答这个问题:
- 创建两个向量,一个用于存储答案ans ,一个用于存储连续偶数temp 。
- 如果(K >= N) ,那么 返回原始向量arr 。
- 现在,遍历数组,并为每个元素:
- 如果当前元素是奇数,则检查temp的长度是否大于或等于K 。
- 如果是,则将其长度推入ans向量。
- 否则将 temp 的元素推送到 ans向量中。
- 清除临时向量。
- 将当前元素推入ans向量中。
- 如果当前元素是偶数,则将其推入temp 。
- 如果当前元素是奇数,则检查temp的长度是否大于或等于K 。
- 循环结束后,向量ans包含最终答案。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to replace all subarrays of
// even elements with their length if their
// length is greater than or equal to K
vector processArray(vector& arr,
int& K)
{
int N = arr.size(), i, count = 0;
vector ans;
vector temp;
if (K >= N)
return arr;
for (i = 0; i < N; i++) {
// If arr[i] is odd
if (arr[i] & 1) {
if (temp.size() >= K)
ans.push_back(temp.size());
else {
for (auto& x : temp) {
ans.push_back(x);
}
}
ans.push_back(arr[i]);
temp.clear();
}
// If arr[i] is even
else {
temp.push_back(arr[i]);
}
}
if (temp.size() >= K) {
ans.push_back(temp.size());
}
else {
for (auto& x : temp) {
ans.push_back(x);
}
}
return ans;
}
// Driver Code
int main()
{
vector arr
= { 3, 6, 10, 2, 7, 6, 4, 8 };
int K = 2;
vector ans = processArray(arr, K);
for (auto& x : ans)
cout << x << " ";
return 0;
}
Java
// Java program for the above approach
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
class GFG {
// Function to replace all subarrays of
// even elements with their length if their
// length is greater than or equal to K
static List processArray(List arr, int K) {
int N = arr.size();
ArrayList ans = new ArrayList();
ArrayList temp = new ArrayList();
;
if (K >= N)
return arr;
for (int i = 0; i < N; i++) {
// If arr[i] is odd
if ((arr.get(i) & 1) > 0) {
if (temp.size() >= K)
ans.add(temp.size());
else {
for (int x : temp) {
ans.add(x);
}
}
ans.add(arr.get(i));
temp.clear();
}
// If arr[i] is even
else {
temp.add(arr.get(i));
}
}
if (temp.size() >= K) {
ans.add(temp.size());
} else {
for (int x : temp) {
ans.add(x);
}
}
return ans;
}
// Driver Code
public static void main(String args[]) {
List arr = Arrays.asList(3, 6, 10, 2, 7, 6, 4, 8);
int K = 2;
List ans = processArray(arr, K);
for (int x : ans)
System.out.print(x + " ");
}
}
// This code is contributed by saurabh_jaiswal.
Python3
# python3 program for the above approach
# Function to replace all subarrays of
# even elements with their length if their
# length is greater than or equal to K
def processArray(arr, K):
N, count = len(arr), 0
ans = []
temp = []
if (K >= N):
return arr
for i in range(0, N):
# If arr[i] is odd
if (arr[i] & 1):
if (len(temp) >= K):
ans.append(len(temp))
else:
for x in temp:
ans.append(x)
ans.append(arr[i])
temp.clear()
# If arr[i] is even
else:
temp.append(arr[i])
if (len(temp) >= K):
ans.append(len(temp))
else:
for x in temp:
ans.append(x)
return ans
# Driver Code
if __name__ == "__main__":
arr = [3, 6, 10, 2, 7, 6, 4, 8]
K = 2
ans = processArray(arr, K)
for x in ans:
print(x, end=" ")
# This code is contributed by rakeshsahni
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
public class GFG {
// Function to replace all subarrays of
// even elements with their length if their
// length is greater than or equal to K
static List processArray(List arr, int K) {
int N = arr.Count;
List ans = new List();
List temp = new List();
;
if (K >= N)
return arr;
for (int i = 0; i < N; i++) {
// If arr[i] is odd
if ((arr[i] & 1) > 0) {
if (temp.Count >= K)
ans.Add(temp.Count);
else {
foreach (int x in temp) {
ans.Add(x);
}
}
ans.Add(arr[i]);
temp.Clear();
}
// If arr[i] is even
else {
temp.Add(arr[i]);
}
}
if (temp.Count >= K) {
ans.Add(temp.Count);
} else {
foreach (int x in temp) {
ans.Add(x);
}
}
return ans;
}
// Driver Code
public static void Main(String []args) {
List arr = new List(new int[]{3, 6, 10, 2, 7, 6, 4, 8});
int K = 2;
List ans = processArray(arr, K);
foreach (int x in ans)
Console.Write(x + " ");
}
}
// This code is contributed by 29AjayKumar
Javascript
输出
3 3 7 3
时间复杂度: 在)
辅助空间: O(N)