原文:
给定一个不同整数的数组,打印该数组中所有具有正负值的数字对。 注:对子的顺序无关紧要。 示例:
input: arr[] = { 1, -3, 2, 3, 6, -1 }
output: -1 1 -3 3
input: arr[] = { 4, 8, 9, -4, 1, -1, -8, -9 }
output: -1 1 -4 4 -8 8 -9 9
一种天真的方法是运行两个循环,即使用外部循环考虑数组的每个元素,并使用内部循环在数组中搜索其相应的正/负值。同样,找到所有的配对。这种方法的时间复杂度为 0(n2)。 a 更好的方法是使用排序,即首先对数组进行排序,然后对于每个负元素,做一个二分搜索法来找到它的对应项( ve 数)。如果找到了,打印那一对。如果当前元素是正的,那么打破这个循环,因为在那之后将有所有的正数。
c
// cpp program to find pairs of positive
// and negative values present in an array.
#include
using namespace std;
void printpairs(int arr[], int n)
{
bool pair_exists = false;
// sort the array
sort(arr, arr n);
// traverse the array
for (int i = 0; i < n; i ) {
// for every arr[i] < 0 element,
// do a binary search for arr[i] > 0.
if (arr[i] < 0) {
// if found, print the pair.
if (binary_search(arr, arr n, -arr[i])) {
cout << arr[i] << ", " << -arr[i] << endl;
pair_exists = true;
}
}
else
break;
}
if (pair_exists == false)
cout << "no such pair exists";
}
// driver code
int main()
{
int arr[] = { 4, 8, 9, -4, 1, -1, -8, -9 };
int n = sizeof(arr) / sizeof(arr[0]);
printpairs(arr, n);
return 0;
}
java 语言(一种计算机语言,尤用于创建网站)
// java program to find pairs
// of positive and negative
// values present in an array.
import java.util.*;
class gfg
{
static void printpairs(int arr[], int n)
{
boolean pair_exists = false;
// sort the array
arrays.sort(arr);
// traverse the array
for (int i = 0; i < n; i )
{
// for every arr[i] < 0 element,
// do a binary search for arr[i] > 0.
if (arr[i] < 0)
{
// if found, print the pair.
if (java.util.arrays.binarysearch(arr, -arr[i])!=-1)
{
system.out.println(arr[i] ", " -arr[i] );
pair_exists = true;
}
}
else
break;
}
if (pair_exists == false)
system.out.println("no such pair exists");
}
// driver code
public static void main(string args[])
{
int arr[] = { 4, 8, 9, -4, 1, -1, -8, -9 };
int n =arr.length;
printpairs(arr, n);
}
}
// this code is contributed
// by arnab kundu
python 3
# python3 program to find pairs of positive
# and negative values present in an array.
# function for binary search
def binary_search(a, x, lo=0, hi=none):
if hi is none:
hi = len(a)
while lo < hi:
mid = (lo hi)//2
midval = a[mid]
if midval < x:
lo = mid 1
elif midval > x:
hi = mid
else:
return mid
return -1
def printpairs(arr, n):
pair_exists = false
# sort the array
arr.sort()
# traverse the array
for i in range(n):
# for every arr[i] < 0 element,
# do a binary search for arr[i] > 0.
if (arr[i] < 0):
# if found, print the pair.
if (binary_search(arr,-arr[i])):
print(arr[i] , ", " , -arr[i])
pair_exists = true
else:
break
if (pair_exists == false):
print("no such pair exists")
# driver code
if __name__=='__main__':
arr = [ 4, 8, 9, -4, 1, -1, -8, -9 ]
n = len(arr)
printpairs(arr, n)
# this code is contributed by ash264
c
// c# program to find pairs
// of positive and negative
// values present in an array.
using system;
public class gfg{
static void printpairs(int []arr, int n)
{
bool pair_exists = false;
// sort the array
array.sort(arr);
// traverse the array
for (int i = 0; i < n; i )
{
// for every arr[i] < 0 element,
// do a binary search for arr[i] > 0.
if (arr[i] < 0)
{
// if found, print the pair.
if (array.binarysearch(arr, -arr[i])!=-1)
{
console.writeline(arr[i] ", " -arr[i] );
pair_exists = true;
}
}
else
break;
}
if (pair_exists == false)
console.writeline("no such pair exists");
}
// driver code
public static void main()
{
int []arr = { 4, 8, 9, -4, 1, -1, -8, -9 };
int n =arr.length;
printpairs(arr, n);
}
}
// this code is contributed by 29ajaykumar
java 描述语言
output:
-9, 9
-8, 8
-4, 4
-1, 1
时间复杂度: o(nlogn) 一种高效的方法是使用散列。以下是所需步骤:
- 开始遍历数组。
- 将所有粒子值存储在无序集中。
- 检查每个负元素,它们对应的正元素是否存在于集合中。
- 如果是,打印该对
- 此外,保持一个标志,检查是否不存在这样的对。
c
// cpp program to find pairs of positive
// and negative values present in an array
#include
using namespace std;
// function to print pairs of positive
// and negative values present in the array
void printpairs(int arr[], int n)
{
unordered_set pairs;
bool pair_exists = false;
// store all the positive elements
// in the unordered_set
for (int i = 0; i < n; i )
if (arr[i] > 0)
pairs.insert(arr[i]);
// start traversing the array
for (int i = 0; i < n; i ) {
// check if the positive value of current
// element exists in the set or not
if (arr[i] < 0)
if (pairs.find(-arr[i]) != pairs.end())
{ // print that pair
cout << arr[i] << ", " << -arr[i] << endl;
pair_exists = true;
}
}
if (pair_exists == false)
cout << "no such pair exists";
}
// driver code
int main()
{
int arr[] = { 4, 8, 9, -4, 1, -1, -8, -9 };
int n = sizeof(arr) / sizeof(arr[0]);
printpairs(arr, n);
return 0;
}
java 语言(一种计算机语言,尤用于创建网站)
// java program to find pairs of positive
// and negative values present in an array
import java.util.*;
class gfg
{
// function to print pairs of positive
// and negative values present in the array
static void printpairs(int arr[], int n)
{
set pairs = new hashset();
boolean pair_exists = false;
// store all the positive elements
// in the unordered_set
for (int i = 0; i < n; i )
if (arr[i] > 0)
pairs.add(arr[i]);
// start traversing the array
for (int i = 0; i < n; i )
{
// check if the positive value of current
// element exists in the set or not
if (arr[i] < 0)
if (pairs.contains(-arr[i]))
{
// print that pair
system.out.println(arr[i] ", " -arr[i]);
pair_exists = true;
}
}
if (pair_exists == false)
system.out.println("no such pair exists");
}
// driver code
public static void main(string args[])
{
int arr[] = { 4, 8, 9, -4, 1, -1, -8, -9 };
int n = arr.length;
printpairs(arr, n);
}
}
// this code is contributed by arnab kundu
python 3
# python3 program to find pairs of positive
# and negative values present in an array
# function to print pairs of positive
# and negative values present in the array
def printpairs(arr, n):
pairs = set()
pair_exists = false
# store all the positive elements
# in the unordered_set
for i in range(0, n):
if arr[i] > 0:
pairs.add(arr[i])
# start traversing the array
for i in range(0, n):
# check if the positive value of current
# element exists in the set or not
if arr[i] < 0:
if (-arr[i]) in pairs:
# print that pair
print("{}, {}".format(arr[i], -arr[i]))
pair_exists = true
if pair_exists == false:
print("no such pair exists")
# driver code
if __name__ == "__main__":
arr = [4, 8, 9, -4, 1, -1, -8, -9]
n = len(arr)
printpairs(arr, n)
# this code is contributed by rituraj jain
c
// c# program to find pairs of positive
// and negative values present in an array
using system;
using system.collections.generic;
class gfg
{
// function to print pairs of positive
// and negative values present in the array
static void printpairs(int []arr, int n)
{
hashset pairs = new hashset();
bool pair_exists = false;
// store all the positive elements
// in the unordered_set
for (int i = 0; i < n; i )
if (arr[i] > 0)
pairs.add(arr[i]);
// start traversing the array
for (int i = 0; i < n; i )
{
// check if the positive value of current
// element exists in the set or not
if (arr[i] < 0)
if (pairs.contains(-arr[i]))
{
// print that pair
console.writeline(arr[i] ", " -arr[i]);
pair_exists = true;
}
}
if (pair_exists == false)
console.writeline("no such pair exists");
}
// driver code
public static void main(string []args)
{
int []arr = { 4, 8, 9, -4, 1, -1, -8, -9 };
int n = arr.length;
printpairs(arr, n);
}
}
// this code is contributed by princiraj1992
java 描述语言
output:
-4, 4
-1, 1
-8, 8
-9, 9
时间复杂度: o(n)
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处