给定一个大小为N的数组arr [] ,任务是查找总和> 0的数组中不同对的数量。
例子:
Input: arr[] = { 3, -2, 1 }
Output: 2
Explanation:
There are two pairs of elements in the array whose sum is positive. They are:
{3, -2} = 1
{3, 1} = 4
Input: arr[] = { -1, -1, -1, 0 }
Output: 0
Explanation:
There are no pairs of elements in the array whose sum is positive.
幼稚的方法:针对此问题的幼稚的方法是考虑数组中所有唯一的元素对。对于每对,检查总和是否为正。
时间复杂度: O(N 2 )
高效方法:
- 这个想法是使用排序和两个指针技术的概念。
- 对于此问题,使用了排序,因为对于总和arr [i] + arr [j]> 0 ,其中i,j是数组中的一些随机索引,或者arr [i]> 0或arr [j]> 0或两者皆有arr [i]和arr [j]> 0 。
- 因此,对数组进行排序后,由于我们需要找到唯一的对。对于每个arr [i]> 0的“ i”,我们需要找到j的数量,使得arr [j] + arr [j]> 0 。
- 在这里,由于数组已排序,因此使用两个指针技术很容易找到对数。我们只需要找到条件成立的’j’的最左边位置即可。这是使用-arr [i] + 1的lower_bound找到的。
- 例如,让数组arr [] = {-4、4,-5、5、3,-2,-3,-1、2、1}。该数组已排序。因此,该数组变为{-5,-4,-3,-2,-1、1、2、3、4、5}。对于某些随机i,假设arr [i] =4。因此,在数组2中找到-3的索引。现在,我们可以确保对于索引2和8之间的所有值,该值arr [i] + arr [j]的> 0。
下面是上述方法的实现:
CPP
// C++ program to find the
// number of pairs in the
// array with the sum > 0
#include
using namespace std;
// Function to find the number
// of pairs in the array with
// sum > 0
int findNumOfPair(int* a, int n)
{
// Sorting the given array
sort(a, a + n);
// Variable to store the count of pairs
int ans = 0;
// Loop to iterate through the array
for (int i = 0; i < n; ++i) {
// Ignore if the value is negative
if (a[i] <= 0)
continue;
// Finding the index using lower_bound
int j = lower_bound(a, a + n, -a[i] + 1) - a;
// Finding the number of pairs between
// two indices i and j
ans += i - j;
}
return ans;
}
// Driver code
int main()
{
int a[] = { 3, -2, 1 };
int n = sizeof(a) / sizeof(a[0]);
int ans = findNumOfPair(a, n);
cout << ans << endl;
return 0;
}
Java
// Java program to find the
// number of pairs in the
// array with the sum > 0
import java.util.*;
class GFG {
// Function to find the number
// of pairs in the array with
// sum > 0
static int findNumOfPair(int arr[], int n)
{
// Sorting the given array
Arrays.sort(arr);
// Variable to store the count of pairs
int ans = 0;
// Loop to iterate through the array
for (int i = 0; i < n; ++i) {
// Ignore if the value is negative
if (arr[i] <= 0)
continue;
/*
minReqVal val is the min value ,which will
give >=1 after adding with the arr[i]
*/
int minReqVal = -arr[i] + 1;
int j = lower_bound(arr, minReqVal);
if (j >= 0)
ans += i - j;
}
return ans;
}
/*
it return the index of a minimum Number in the
array which is just >= val
*/
static int lower_bound(int arr[], int val)
{
int start = 0, end = arr.length;
/*
using the Binary search technique , since our
array is sorted
*/
while (start < end) {
int mid = (start + end) >> 1;
if (val > arr[mid])
start = mid + 1;
else
end = mid;
}
// when we dont find the answer return -1
if (start == arr.length)
return -1;
return start;
}
// Driver code
public static void main(String[] args)
{
int a[] = {-2,-1,-1,-1,-1,0 ,1,2,3};
int n = a.length;
int ans = findNumOfPair(a, n);
System.out.println(ans);
}
}
// This code is contributed by Pradeep Mondal P
Python3
# Python3 program to find the
# number of pairs in the
# array with the sum > 0
from bisect import bisect_left as lower_bound
# Function to find the number
# of pairs in the array with
# sum > 0
def findNumOfPair(a, n):
# Sorting the given array
a = sorted(a)
# Variable to store the count of pairs
ans = 0
# Loop to iterate through the array
for i in range(n):
# Ignore if the value is negative
if (a[i] <= 0):
continue
# Finding the index using lower_bound
j = lower_bound(a, -a[i] + 1)
# Finding the number of pairs between
# two indices i and j
ans += i - j
return ans
# Driver code
if __name__ == '__main__':
a = [3, -2, 1]
n = len(a)
ans = findNumOfPair(a, n)
print(ans)
# This code is contributed by mohit kumar 29
C#
// C# program to find the
// number of pairs in the
// array with the sum > 0
using System;
class GFG {
// Function to find the number
// of pairs in the array with
// sum > 0
static int findNumOfPair(int[] arr, int n)
{
// Sorting the given array
Array.Sort(arr);
// Variable to store the count of pairs
int ans = 0;
// Loop to iterate through the array
for (int i = 0; i < n; ++i) {
// Ignore if the value is negative
if (arr[i] <= 0)
continue;
/*
minReqVal val is the min value ,which will
give >=1 after adding with the arr[i]
*/
int minReqVal = -arr[i] + 1;
int j = lower_bound(arr, minReqVal);
if (j >= 0)
ans += i - j;
}
return ans;
}
/*
it return the index of a minimum Number in the
array which is just >= val
*/
static int lower_bound(int[] arr, int val)
{
int start = 0, end = arr.Length;
/*
using the Binary search technique , since our
array is sorted
*/
while (start < end) {
int mid = (start + end) >> 1;
if (val > arr[mid])
start = mid + 1;
else
end = mid;
}
// when we dont find the answer return -1
if (start == arr.Length)
return -1;
return start;
}
// Driver code
public static void Main()
{
int[] a = { -2, 1, 3 };
int n = a.Length;
int ans = findNumOfPair(a, n);
Console.Write(ans);
}
}
// This code is contributed by Pradeep Mondal P
输出
2
时间复杂度: O(N * log(N))