检查 Array 是否有 2 个不同的子序列,其中较小的子序列具有更高的总和
给定一个包含N个整数的数组A[] 。检查是否存在给定数组的 2 个不同的子序列X和Y ,使得X的元素之和大于Y的元素之和,但X中的元素数小于是。
例子:
Input: N = 5, A[] = {2, 3, 8, 1, 6}
Output: YES
Explanation: Consider the sequences: X = {6}, Y = {2, 3}.
It can be easily seen that size of X(i.e. 1) < size of Y(i.e 2), whereas sum of elements of X (i.e 6) > sum of elements of Y(i.e 5).
Input: N = 6, A[] = {1, 1, 1, 1, 1, 1}
Output: NO
方法:可以使用基于以下思想的两个指针方法来解决该问题:
In a sorted array if the sum of first K (where K > N/2) smaller elements is less than the sum of the remaining elements then there can be such subsequences, otherwise not.
请按照以下步骤解决问题:
- 对给定的数组进行排序。
- 声明两个变量beg和end来存储排序数组开头和结尾的元素之和。
- 用 arr[0] 初始化beg并以 0结束,因为在子序列中应该至少有 1 个元素且总和更少。
- 使用 i = 1 和 j = N-1 中的两个指针:
- 将 arr[i] 与 beg 和 arr[j] 与 end 相加。
- 如果beg小于end,则中断迭代。因为有可能找到这样的子序列,因为较高侧的所有其他元素都大于前半部分,而较低侧已经多出一个元素。
- 如果总迭代后没有找到这样的序列,则返回它是不可能的。
下面是上述方法的实现:
C++
// C++ Code for above approach
#include
using namespace std;
// Function to check if the given
// array have 2 distinct subsequences
// such that number of elements in one
// having greater sum is less
bool haveSubsequences(int N, int A[])
{
// Sorting the given sequence Array
sort(A, A + N);
// Variable "beg" stores the sum of
// elements from beginning variable
// "end" stores the sum of elements
// from end
int beg = A[0], end = 0;
// Flag variable to check for wrong
// answer
int flag = 0;
// Initializing 2 pointers
int i = 1, j = N - 1;
// Iterating array from both sides
// via 2 pointers and checking if
// sum of starting say k+1 elements is
// less than sum of k elements from the
// end
while (i < j) {
beg += A[i];
end += A[j];
// If sum of starting i elements is
// less than the sum of j elements,
// we make flag 1 and break the loop
if (beg < end) {
flag = 1;
break;
}
// Else we simply increment i and
// decrement j
i++;
j--;
}
// Returning true or false in accordance
// with the flag
if (flag == 1) {
return true;
}
else {
return false;
}
}
// Driver Code
int main()
{
int N = 5;
int A[] = { 2, 3, 8, 1, 6 };
bool answer = haveSubsequences(N, A);
if (answer == true) {
cout << "YES";
}
else {
cout << "NO";
}
}
Java
/*package whatever //do not write package name here */
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG
{
// Function to check if the given
// array have 2 distinct subsequences
// such that number of elements in one
// having greater sum is less
public static boolean haveSubsequences(int N, int A[])
{
// Sorting the given sequence Array
Arrays.sort(A);
// Variable "beg" stores the sum of
// elements from beginning variable
// "end" stores the sum of elements
// from end
int beg = A[0], end = 0;
// Flag variable to check for wrong
// answer
int flag = 0;
// Initializing 2 pointers
int i = 1, j = N - 1;
// Iterating array from both sides
// via 2 pointers and checking if
// sum of starting say k+1 elements is
// less than sum of k elements from the
// end
while (i < j) {
beg += A[i];
end += A[j];
// If sum of starting i elements is
// less than the sum of j elements,
// we make flag 1 and break the loop
if (beg < end) {
flag = 1;
break;
}
// Else we simply increment i and
// decrement j
i++;
j--;
}
// Returning true or false in accordance
// with the flag
if (flag == 1) {
return true;
}
else {
return false;
}
}
// Driver Code
public static void main(String[] args)
{
int N = 5;
int A[] = { 2, 3, 8, 1, 6 };
boolean answer = haveSubsequences(N, A);
if (answer == true) {
System.out.println("YES");
}
else {
System.out.println("NO");
}
}
}
// This code is contributed by amnindersingh1414.
Python3
# Python Code for above approach
# Function to check if the given
# array have 2 distinct subsequences
# such that number of elements in one
# having greater sum is less
def haveSubsequences(N, A):
# Sorting the given sequence Array
A.sort()
# Variable "beg" stores the sum of
# elements from beginning variable
# "end" stores the sum of elements
# from end
beg = A[0]
end = 0
# Flag variable to check for wrong
# answer
flag = 0
# Initializing 2 pointers
i = 1
j = N - 1
# Iterating array from both sides
# via 2 pointers and checking if
# sum of starting say k+1 elements is
# less than sum of k elements from the
# end
while i < j:
beg += A[i]
end += A[j]
# If sum of starting i elements is
# less than the sum of j elements,
# we make flag 1 and break the loop
if (beg < end):
flag = 1
break
# Else we simply increment i and
# decrement j
i += 1
j -= 1
# Returning true or false in accordance
# with the flag
if (flag == 1):
return True
else:
return False
# Driver Code
if __name__ == '__main__':
N = 5
A = [2, 3, 8, 1, 6]
answer = haveSubsequences(N, A)
if (answer == True):
print("YES")
else:
print("NO")
# This code is contributed by amnindersingh1414.
C#
// C# program for the above approach
using System;
class GFG
{
// Function to check if the given
// array have 2 distinct subsequences
// such that number of elements in one
// having greater sum is less
static bool haveSubsequences(int N, int []A)
{
// Sorting the given sequence Array
Array.Sort(A);
// Variable "beg" stores the sum of
// elements from beginning variable
// "end" stores the sum of elements
// from end
int beg = A[0], end = 0;
// Flag variable to check for wrong
// answer
int flag = 0;
// Initializing 2 pointers
int i = 1, j = N - 1;
// Iterating array from both sides
// via 2 pointers and checking if
// sum of starting say k+1 elements is
// less than sum of k elements from the
// end
while (i < j) {
beg += A[i];
end += A[j];
// If sum of starting i elements is
// less than the sum of j elements,
// we make flag 1 and break the loop
if (beg < end) {
flag = 1;
break;
}
// Else we simply increment i and
// decrement j
i++;
j--;
}
// Returning true or false in accordance
// with the flag
if (flag == 1) {
return true;
}
else {
return false;
}
}
// Driver Code
public static void Main()
{
int N = 5;
int []A = { 2, 3, 8, 1, 6 };
bool answer = haveSubsequences(N, A);
if (answer == true) {
Console.WriteLine("YES");
}
else {
Console.WriteLine("NO");
}
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
输出
YES
时间复杂度: O(N*log(N))
辅助空间: O(1)