给定一个由不同整数组成的数组,打印所有具有数组中存在的数字的正值和负值的对。我们需要按出现的顺序打印对。应该首先打印任何元素首先出现的对。
例子:
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
方法一(简单:O(n 2 ))
这个想法是使用两个嵌套循环。对于每个元素 arr[i],从索引 i + 1 到 n – 1 中找到 arr[i] 的负数并将其存储在另一个数组中。对于输出,对存储的元素进行排序并打印存储元素的负正值。
下面是这个方法的实现:
C++
// Simple CPP program to find pairs of positive
// and negative values present in an array.
#include
using namespace std;
// Print pair with negative and positive value
void printPairs(int arr[], int n)
{
vector v;
// For each element of array.
for (int i = 0; i < n; i++)
// Try to find the negative value of
// arr[i] from i + 1 to n
for (int j = i + 1; j < n; j++)
// If absolute values are equal print pair.
if (abs(arr[i]) == abs(arr[j]))
v.push_back(abs(arr[i]));
// If size of vector is 0, therefore there is no
// element with positive negative value, print "0"
if (v.size() == 0)
return;
// Sort the vector
sort(v.begin(), v.end());
// Print the pair with negative positive value.
for (int i = 0; i < v.size(); i++)
cout << -v[i] << " " << v[i];
}
// Driven Program
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.*;
import java.lang.*;
class GFG {
// Print pair with negative and positive value
public static void printPairs(int arr[] , int n)
{
Vector v = new Vector();
// For each element of array.
for (int i = 0; i < n; i++)
// Try to find the negative value of
// arr[i] from i + 1 to n
for (int j = i + 1; j < n; j++)
// If absolute values are equal
// print pair.
if (Math.abs(arr[i]) ==
Math.abs(arr[j]))
v.add(Math.abs(arr[i]));
// If size of vector is 0, therefore there
// is no element with positive negative
// value, print "0"
if (v.size() == 0)
return;
// Sort the vector
Collections.sort(v);
// Print the pair with negative positive
// value.
for (int i = 0; i < v.size(); i++)
System.out.print(-v.get(i) + " " +
v.get(i));
}
// Driven Program
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 Prasad Kshirsagar.
Python 3
# Simple Python 3 program to find
# pairs of positive and negative
# values present in an array.
# Print pair with negative and
# positive value
def printPairs(arr, n):
v = []
# For each element of array.
for i in range(n):
# Try to find the negative value
# of arr[i] from i + 1 to n
for j in range( i + 1,n) :
# If absolute values are
# equal print pair.
if (abs(arr[i]) == abs(arr[j])) :
v.append(abs(arr[i]))
# If size of vector is 0, therefore
# there is no element with positive
# negative value, print "0"
if (len(v) == 0):
return;
# Sort the vector
v.sort()
# Print the pair with negative
# positive value.
for i in range(len( v)):
print(-v[i], "" , v[i], end = " ")
# 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 ChitraNayal
C#
// C# program to find pairs of positive
// and negative values present in an array.
using System;
using System.Collections.Generic;
class GFG
{
// Print pair with negative and positive value
public static void printPairs(int []arr , int n)
{
List v = new List();
// For each element of array.
for (int i = 0; i < n; i++)
// Try to find the negative value of
// arr[i] from i + 1 to n
for (int j = i + 1; j < n; j++)
// If absolute values are equal
// print pair.
if (Math.Abs(arr[i]) ==
Math.Abs(arr[j]))
v.Add(Math.Abs(arr[i]));
// If size of vector is 0, therefore there
// is no element with positive negative
// value, print "0"
if (v.Count == 0)
return;
// Sort the vector
v.Sort();
// Print the pair with negative positive
// value.
for (int i = 0; i < v.Count; i++)
Console.Write(-v[i] + " " +
v[i]);
}
// 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 has been contributed by 29AjayKumar
Javascript
C++
// Efficient CPP program to find pairs of
// positive and negative values present in
// an array.
#include
using namespace std;
// Print pair with negative and positive value
void printPairs(int arr[], int n)
{
vector v;
unordered_map cnt;
// For each element of array.
for (int i = 0; i < n; i++) {
// If element has not encounter early,
// mark it on cnt array.
if (cnt[abs(arr[i])] == 0)
cnt[abs(arr[i])] = 1;
// If seen before, push it in vector (
// given that elements are distinct)
else {
v.push_back(abs(arr[i]));
cnt[abs(arr[i])] = 0;
}
}
if (v.size() == 0)
return;
sort(v.begin(), v.end());
for (int i = 0; i < v.size(); i++)
cout << -v[i] << " " << v[i] << " ";
}
// Driven Program
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
// Efficient Java program to find pairs of
// positive and negative values present in
// an array.
import java.util.*;
class GFG
{
// Print pair with negative
// and positive value
static void printPairs(int arr[], int n)
{
ArrayList v = new ArrayList ();
HashMap cnt = new HashMap();
// For each element of array.
for (int i = 0; i < n; i++)
{
// If element has encounter early,
// then increment its count
if (cnt.containsKey(Math.abs(arr[i])))
cnt.put(Math.abs(arr[i]) , cnt.get(Math.abs(arr[i])) + 1);
// If element has not seen before,
// then initialize its count to 1
else
{
cnt.put(Math.abs(arr[i]), 1);
}
if( cnt.get(Math.abs(arr[i])) == 2 ){
v.add(Math.abs(arr[i]));
}
}
if (v.size() == 0)
return;
Collections.sort(v);
for (int i = 0; i < v.size(); i++)
System.out.print("-" + v.get(i) +
" " + v.get(i) + " ");
}
// 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 Prerna Saini
Python3
# Efficient Python3 program to find pairs of
# positive and negative values present in
# an array.
# Print pair with negative and
# positive value
def printPairs(arr, n):
s = set()
ret = []
# For each element of array.
for i in arr:
if abs(i) in s:
ret.append(abs(i))
else:
s.add(abs(i))
ret.sort()
for i in range(0, len(ret)):
print(-ret[i], "", ret[i], end = " ")
# 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 RohitOberoi
C#
// Efficient C# program to find pairs of
// positive and negative values present in
// an array.
using System;
using System.Collections.Generic;
class GFG
{
// Print pair with negative and positive value
static void printPairs(int[] arr, int n)
{
List v = new List();
Dictionary cnt = new Dictionary();
// For each element of array.
for (int i = 0; i < n; i++)
{
// If element has not encounter early,
// mark it on cnt array.
if(!cnt.ContainsKey(Math.Abs(arr[i])))
{
cnt[Math.Abs(arr[i])] = true;
}
else if(cnt[Math.Abs(arr[i])] == false)
{
cnt[Math.Abs(arr[i])] = true;
}
else
{
v.Add(Math.Abs(arr[i]));
cnt[Math.Abs(arr[i])] = false;
}
}
if (v.Count == 0)
return;
v.Sort();
for (int i = 0; i < v.Count; i++)
Console.Write(-v[i] + " " + v[i] + " ");
}
// Driver code
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 divyeshrabadiya07
输出:
-1 1-4 4-8 8-9 9
方法二(散列)
这个想法是使用散列。遍历给定的数组,在哈希表的绝对值处增加计数。如果计数变为 2,则将其绝对值存储在另一个向量中。最后对向量进行排序。如果向量的大小为 0,则打印“0”,否则对于向量中的每一项,首先打印其负值和正值。
下面是这个方法的实现:
C++
// Efficient CPP program to find pairs of
// positive and negative values present in
// an array.
#include
using namespace std;
// Print pair with negative and positive value
void printPairs(int arr[], int n)
{
vector v;
unordered_map cnt;
// For each element of array.
for (int i = 0; i < n; i++) {
// If element has not encounter early,
// mark it on cnt array.
if (cnt[abs(arr[i])] == 0)
cnt[abs(arr[i])] = 1;
// If seen before, push it in vector (
// given that elements are distinct)
else {
v.push_back(abs(arr[i]));
cnt[abs(arr[i])] = 0;
}
}
if (v.size() == 0)
return;
sort(v.begin(), v.end());
for (int i = 0; i < v.size(); i++)
cout << -v[i] << " " << v[i] << " ";
}
// Driven Program
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
// Efficient Java program to find pairs of
// positive and negative values present in
// an array.
import java.util.*;
class GFG
{
// Print pair with negative
// and positive value
static void printPairs(int arr[], int n)
{
ArrayList v = new ArrayList ();
HashMap cnt = new HashMap();
// For each element of array.
for (int i = 0; i < n; i++)
{
// If element has encounter early,
// then increment its count
if (cnt.containsKey(Math.abs(arr[i])))
cnt.put(Math.abs(arr[i]) , cnt.get(Math.abs(arr[i])) + 1);
// If element has not seen before,
// then initialize its count to 1
else
{
cnt.put(Math.abs(arr[i]), 1);
}
if( cnt.get(Math.abs(arr[i])) == 2 ){
v.add(Math.abs(arr[i]));
}
}
if (v.size() == 0)
return;
Collections.sort(v);
for (int i = 0; i < v.size(); i++)
System.out.print("-" + v.get(i) +
" " + v.get(i) + " ");
}
// 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 Prerna Saini
蟒蛇3
# Efficient Python3 program to find pairs of
# positive and negative values present in
# an array.
# Print pair with negative and
# positive value
def printPairs(arr, n):
s = set()
ret = []
# For each element of array.
for i in arr:
if abs(i) in s:
ret.append(abs(i))
else:
s.add(abs(i))
ret.sort()
for i in range(0, len(ret)):
print(-ret[i], "", ret[i], end = " ")
# 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 RohitOberoi
C#
// Efficient C# program to find pairs of
// positive and negative values present in
// an array.
using System;
using System.Collections.Generic;
class GFG
{
// Print pair with negative and positive value
static void printPairs(int[] arr, int n)
{
List v = new List();
Dictionary cnt = new Dictionary();
// For each element of array.
for (int i = 0; i < n; i++)
{
// If element has not encounter early,
// mark it on cnt array.
if(!cnt.ContainsKey(Math.Abs(arr[i])))
{
cnt[Math.Abs(arr[i])] = true;
}
else if(cnt[Math.Abs(arr[i])] == false)
{
cnt[Math.Abs(arr[i])] = true;
}
else
{
v.Add(Math.Abs(arr[i]));
cnt[Math.Abs(arr[i])] = false;
}
}
if (v.Count == 0)
return;
v.Sort();
for (int i = 0; i < v.Count; i++)
Console.Write(-v[i] + " " + v[i] + " ");
}
// Driver code
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 divyeshrabadiya07
输出:
-1 1 -4 4 -8 8 -9 9
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。