找到一个子序列,它在反转时给出最大和子数组
给定一个大小为N的整数数组arr ,任务是找到一个子序列,在该子序列中颠倒顺序,可以获得最大和子数组。
例子:
Input: arr[] = {-2, -3, 4, -1, -2, 1, 5, -3}
Output: [-2 -3 1 5]
Explanation : After selecting subsequence -2 -3 1 5 and reverse it elements, modified array will be {5, 1, 4, -1, -2, -3, -2, -3} and thus the maximum contagious sum i.e. 5 + 1 + 4 = 10
Input: arr[] = {2, -6, -12, 7, -13, 9, -14}
Output: [-6 -12 7 9]
Explanation: After selecting the above subsequence modified array will be {2, 9, 7, -12, -13, -6, -14} and thus the maximum contagious sum i.e. is 2 + 9 + 7 = 18
方法:这个想法很简单,我们必须修改数组以使所有正元素聚集在一起,因此我们必须找到子序列,使得当我们反转子序列时所有正元素都聚集在一起。
- 假设数组中有“ p ”个非负元素。将数组分成两部分:前 p 个元素和其余元素。
- 让“ p x ”是数组第一部分中的非负元素。所以第一部分的负面因素是:
(size of first part of array – number of non-negative elements) = p – px
- 数组第二部分中非负元素的数量也是
(total non-negative elements – non-negative elements in first part of array) = p – px
- 所以我们必须从数组的第一部分选择负元素pp x 个元素,从数组的第二部分选择pp x 个非负元素。
下面是上述方法的实现:
C++
// C++ code to implement the above approach
#include
using namespace std;
vector findSubsequce(int arr[], int n)
{
int p = 0;
for (int i = 0; i < n; i++) {
if (arr[i] >= 0)
p++;
}
vector res;
// store negative elements present
// from 0 to p-1 index
for (int i = 0; i < p; i++) {
if (arr[i] < 0)
res.push_back(arr[i]);
}
// store non-negative elements
// present from p to n index
for (int i = p; i < n; i++) {
if (arr[i] >= 0)
res.push_back(arr[i]);
}
return res;
}
// Driver code
int main()
{
int arr[] = { -2, -3, 4, -1,
-2, 1, 5, -3 };
int n = sizeof(arr) / sizeof(arr[0]);
vector res = findSubsequce(arr, n);
for (int i = 0; i < res.size(); i++) {
cout << res[i] << " ";
}
}
Java
// Java code to implement the above approach
import java.io.*;
import java.util.*;
class GFG {
public static ArrayList
findSubsequence(int arr[], int n)
{
int p = 0;
for (int i = 0; i < n; i++) {
if (arr[i] >= 0)
p++;
}
ArrayList res
= new ArrayList();
// store negative elements
// present from 0 to p-1 index
for (int i = 0; i < p; i++) {
if (arr[i] < 0)
res.add(arr[i]);
}
// store non-negative elements
// present from p to n index
for (int i = p; i < n; i++) {
if (arr[i] >= 0)
res.add(arr[i]);
}
return res;
}
// Driver code
public static void main(String[] args)
{
int arr[] = { -2, -3, 4, -1, -2, 1, 5, -3 };
int n = arr.length;
ArrayList res = findSubsequence(arr, n);
for (int i = 0; i < res.size(); i++) {
System.out.print(res.get(i) + " ");
}
}
}
Python3
# Python 3 code to implement the above approach
def findSubsequce(arr, n):
p = 0
for i in range(n):
if (arr[i] >= 0):
p += 1
res = []
# store negative elements present
# from 0 to p-1 index
for i in range(p):
if (arr[i] < 0):
res.append(arr[i])
# store non-negative elements
# present from p to n index
for i in range(p, n):
if (arr[i] >= 0):
res.append(arr[i])
return res
# Driver code
if __name__ == "__main__":
arr = [-2, -3, 4, -1,
-2, 1, 5, -3]
n = len(arr)
res = findSubsequce(arr, n)
for i in range(len(res)):
print(res[i], end=" ")
# This code is contributed by ukasp.
C#
// C# code to implement the above approach
using System;
using System.Collections;
public class GFG{
public static ArrayList
findSubsequence(int[] arr, int n)
{
int p = 0;
for (int i = 0; i < n; i++) {
if (arr[i] >= 0)
p++;
}
var res = new ArrayList();
// store negative elements
// present from 0 to p-1 index
for (int i = 0; i < p; i++) {
if (arr[i] < 0)
res.Add(arr[i]);
}
// store non-negative elements
// present from p to n index
for (int i = p; i < n; i++) {
if (arr[i] >= 0)
res.Add(arr[i]);
}
return res;
}
// Driver code
static public void Main (){
int[] arr = { -2, -3, 4, -1, -2, 1, 5, -3 };
int n = arr.Length;
ArrayList res = findSubsequence(arr, n);
for (int i = 0; i < res.Count; i++) {
Console.Write(res[i] + " ");
}
}
}
// This code is contributed by hrithikgarg03188.
Javascript
-2 -3 1 5
时间复杂度:O(N)
辅助空间:O(N)