Count number of positive pairs in an array

Here given code implementation process.

/*
    C program for
    Count number of positive pairs in an array 
*/
#include <stdio.h>

// Display array elements
void printArray(int arr[], int n)
{
	for (int i = 0; i < n; ++i)
	{
		printf(" %d", arr[i]);
	}
}
void positivePairs(int arr[], int n)
{
	if (n <= 0)
	{
		return;
	}
	int count = 0;
	// Outer loop from 0...n
	for (int i = 0; i < n; ++i)
	{
		// Inner loop from i+1...n
		for (int j = i + 1; j < n; ++j)
		{
			if (arr[i] + arr[j] > 0)
			{
				// When pair sum is positive
				count++;
			}
		}
	}
	// Display given array
	printf("\n Given array : ");
	printArray(arr, n);
	// Display calculated result
	printf("\n Number of positive pairs is : %d", count);
}
int main(int argc, char
	const *argv[])
{
	int arr1[] = {
		1 , 2 , -3 , -2 , 1 , 3
	};
	int arr2[] = {
		3 , -2 , 4 , -1
	};
	int arr3[] = {
		1 , 2 , 1
	};
	int arr4[] = {
		1 , -2 , -1
	};
	// Test A
	int n = sizeof(arr1) / sizeof(arr1[0]);
	/*
	    arr = [ 1,  2, -3, -2, 1, 3 ]
	    ----------------------------------
	    (1,2)(1,1)(1,3)
	    (2,1)(2,3)(-2,3)
	    (1,3)
	    -----------------------
	    Positive pairs : 7
	*/
	positivePairs(arr1, n);
	// Test B
	n = sizeof(arr2) / sizeof(arr2[0]);
	/*
	    arr = [3, -2, 4, - 1]
	    ---------------------------------
	    (3,-2)(3,4)(3,-1)
	    (-2,4)(4,-1)
	    -----------------------
	    Positive pairs : 5
	*/
	positivePairs(arr2, n);
	// Test C
	n = sizeof(arr3) / sizeof(arr3[0]);
	/*
	    arr = [1, 2, 1]
	    ----------------
	    (1,2)(1,1)(2,1)
	    -------------------
	    Positive pairs : 3
	*/
	positivePairs(arr3, n);
	// Test D
	n = sizeof(arr4) / sizeof(arr4[0]);
	/*
	    arr = [1, -2, -1]
	    ----------------
	    None
	    -------------------
	    Positive pairs : 0
	*/
	positivePairs(arr4, n);
	return 0;
}

Output

 Given array :  1 2 -3 -2 1 3
 Number of positive pairs is : 7
 Given array :  3 -2 4 -1
 Number of positive pairs is : 5
 Given array :  1 2 1
 Number of positive pairs is : 3
 Given array :  1 -2 -1
 Number of positive pairs is : 0
// Java program for
// Count number of positive pairs in an array 
public class Pairs
{
	// Display array elements
	public void printArray(int[] arr, int n)
	{
		for (int i = 0; i < n; ++i)
		{
			System.out.print(" " + arr[i]);
		}
	}
	public void positivePairs(int[] arr, int n)
	{
		if (n <= 0)
		{
			return;
		}
		int count = 0;
		// Outer loop from 0...n
		for (int i = 0; i < n; ++i)
		{
			// Inner loop from i+1...n
			for (int j = i + 1; j < n; ++j)
			{
				if (arr[i] + arr[j] > 0)
				{
					// When pair sum is positive
					count++;
				}
			}
		}
		// Display given array
		System.out.print("\n Given array : ");
		printArray(arr, n);
		// Display calculated result
		System.out.print("\n Number of positive pairs is : " + count);
	}
	public static void main(String[] args)
	{
		Pairs task = new Pairs();
		int[] arr1 = {
			1 , 2 , -3 , -2 , 1 , 3
		};
		int[] arr2 = {
			3 , -2 , 4 , -1
		};
		int[] arr3 = {
			1 , 2 , 1
		};
		int[] arr4 = {
			1 , -2 , -1
		};
		// Test A
		int n = arr1.length;
		/*
		    arr = [ 1,  2, -3, -2, 1, 3 ]
		    ----------------------------------
		    (1,2)(1,1)(1,3)
		    (2,1)(2,3)(-2,3)
		    (1,3)
		    -----------------------
		    Positive pairs : 7
		*/
		task.positivePairs(arr1, n);
		// Test B
		n = arr2.length;
		/*
		    arr = [3, -2, 4, - 1]
		    ---------------------------------
		    (3,-2)(3,4)(3,-1)
		    (-2,4)(4,-1)
		    -----------------------
		    Positive pairs : 5
		*/
		task.positivePairs(arr2, n);
		// Test C
		n = arr3.length;
		/*
		    arr = [1, 2, 1]
		    ----------------
		    (1,2)(1,1)(2,1)
		    -------------------
		    Positive pairs : 3
		*/
		task.positivePairs(arr3, n);
		// Test D
		n = arr4.length;
		/*
		    arr = [1, -2, -1]
		    ----------------
		    None
		    -------------------
		    Positive pairs : 0
		*/
		task.positivePairs(arr4, n);
	}
}

Output

 Given array :  1 2 -3 -2 1 3
 Number of positive pairs is : 7
 Given array :  3 -2 4 -1
 Number of positive pairs is : 5
 Given array :  1 2 1
 Number of positive pairs is : 3
 Given array :  1 -2 -1
 Number of positive pairs is : 0
// Include header file
#include <iostream>

using namespace std;
// C++ program for
// Count number of positive pairs in an array
class Pairs
{
	public:
		// Display array elements
		void printArray(int arr[], int n)
		{
			for (int i = 0; i < n; ++i)
			{
				cout << " " << arr[i];
			}
		}
	void positivePairs(int arr[], int n)
	{
		if (n <= 0)
		{
			return;
		}
		int count = 0;
		// Outer loop from 0...n
		for (int i = 0; i < n; ++i)
		{
			// Inner loop from i+1...n
			for (int j = i + 1; j < n; ++j)
			{
				if (arr[i] + arr[j] > 0)
				{
					// When pair sum is positive
					count++;
				}
			}
		}
		// Display given array
		cout << "\n Given array : ";
		this->printArray(arr, n);
		// Display calculated result
		cout << "\n Number of positive pairs is : " << count;
	}
};
int main()
{
	Pairs *task = new Pairs();
	int arr1[] = {
		1 , 2 , -3 , -2 , 1 , 3
	};
	int arr2[] = {
		3 , -2 , 4 , -1
	};
	int arr3[] = {
		1 , 2 , 1
	};
	int arr4[] = {
		1 , -2 , -1
	};
	// Test A
	int n = sizeof(arr1) / sizeof(arr1[0]);
	/*
	    arr = [ 1,  2, -3, -2, 1, 3 ]
	    ----------------------------------
	    (1,2)(1,1)(1,3)
	    (2,1)(2,3)(-2,3)
	    (1,3)
	    -----------------------
	    Positive pairs : 7
	*/
	task->positivePairs(arr1, n);
	// Test B
	n = sizeof(arr2) / sizeof(arr2[0]);
	/*
	    arr = [3, -2, 4, - 1]
	    ---------------------------------
	    (3,-2)(3,4)(3,-1)
	    (-2,4)(4,-1)
	    -----------------------
	    Positive pairs : 5
	*/
	task->positivePairs(arr2, n);
	// Test C
	n = sizeof(arr3) / sizeof(arr3[0]);
	/*
	    arr = [1, 2, 1]
	    ----------------
	    (1,2)(1,1)(2,1)
	    -------------------
	    Positive pairs : 3
	*/
	task->positivePairs(arr3, n);
	// Test D
	n = sizeof(arr4) / sizeof(arr4[0]);
	/*
	    arr = [1, -2, -1]
	    ----------------
	    None
	    -------------------
	    Positive pairs : 0
	*/
	task->positivePairs(arr4, n);
	return 0;
}

Output

 Given array :  1 2 -3 -2 1 3
 Number of positive pairs is : 7
 Given array :  3 -2 4 -1
 Number of positive pairs is : 5
 Given array :  1 2 1
 Number of positive pairs is : 3
 Given array :  1 -2 -1
 Number of positive pairs is : 0
// Include namespace system
using System;
// Csharp program for
// Count number of positive pairs in an array
public class Pairs
{
	// Display array elements
	public void printArray(int[] arr, int n)
	{
		for (int i = 0; i < n; ++i)
		{
			Console.Write(" " + arr[i]);
		}
	}
	public void positivePairs(int[] arr, int n)
	{
		if (n <= 0)
		{
			return;
		}
		int count = 0;
		// Outer loop from 0...n
		for (int i = 0; i < n; ++i)
		{
			// Inner loop from i+1...n
			for (int j = i + 1; j < n; ++j)
			{
				if (arr[i] + arr[j] > 0)
				{
					// When pair sum is positive
					count++;
				}
			}
		}
		// Display given array
		Console.Write("\n Given array : ");
		this.printArray(arr, n);
		// Display calculated result
		Console.Write("\n Number of positive pairs is : " + count);
	}
	public static void Main(String[] args)
	{
		Pairs task = new Pairs();
		int[] arr1 = {
			1 , 2 , -3 , -2 , 1 , 3
		};
		int[] arr2 = {
			3 , -2 , 4 , -1
		};
		int[] arr3 = {
			1 , 2 , 1
		};
		int[] arr4 = {
			1 , -2 , -1
		};
		// Test A
		int n = arr1.Length;
		/*
		    arr = [ 1,  2, -3, -2, 1, 3 ]
		    ----------------------------------
		    (1,2)(1,1)(1,3)
		    (2,1)(2,3)(-2,3)
		    (1,3)
		    -----------------------
		    Positive pairs : 7
		*/
		task.positivePairs(arr1, n);
		// Test B
		n = arr2.Length;
		/*
		    arr = [3, -2, 4, - 1]
		    ---------------------------------
		    (3,-2)(3,4)(3,-1)
		    (-2,4)(4,-1)
		    -----------------------
		    Positive pairs : 5
		*/
		task.positivePairs(arr2, n);
		// Test C
		n = arr3.Length;
		/*
		    arr = [1, 2, 1]
		    ----------------
		    (1,2)(1,1)(2,1)
		    -------------------
		    Positive pairs : 3
		*/
		task.positivePairs(arr3, n);
		// Test D
		n = arr4.Length;
		/*
		    arr = [1, -2, -1]
		    ----------------
		    None
		    -------------------
		    Positive pairs : 0
		*/
		task.positivePairs(arr4, n);
	}
}

Output

 Given array :  1 2 -3 -2 1 3
 Number of positive pairs is : 7
 Given array :  3 -2 4 -1
 Number of positive pairs is : 5
 Given array :  1 2 1
 Number of positive pairs is : 3
 Given array :  1 -2 -1
 Number of positive pairs is : 0
package main
import "fmt"
// Go program for
// Count number of positive pairs in an array

// Display array elements
func printArray(arr[] int, n int) {
	for i := 0 ; i < n ; i++ {
		fmt.Print(" ", arr[i])
	}
}
func positivePairs(arr[] int, n int) {
	if n <= 0 {
		return
	}
	var count int = 0
	// Outer loop from 0...n
	for i := 0 ; i < n ; i++ {
		// Inner loop from i+1...n
		for j := i + 1 ; j < n ; j++ {
			if arr[i] + arr[j] > 0 {
				// When pair sum is positive
				count++
			}
		}
	}
	// Display given array
	fmt.Print("\n Given array : ")
	printArray(arr, n)
	// Display calculated result
	fmt.Print("\n Number of positive pairs is : ", count)
}
func main() {
	
	var arr1 = [] int {1,  2, -3, -2, 1, 3 }
	var arr2 = [] int {3, -2, 4, - 1}
	var arr3 = [] int {1, 2, 1}
	var arr4 = [] int {1, -2, -1}
	// Test A
	var n int = len(arr1)
	/*
	    arr = [ 1,  2, -3, -2, 1, 3 ]
	    ----------------------------------
	    (1,2)(1,1)(1,3)
	    (2,1)(2,3)(-2,3)
	    (1,3)
	    -----------------------
	    Positive pairs : 7
	*/
	positivePairs(arr1, n)
	// Test B
	n = len(arr2)
	/*
	    arr = [3, -2, 4, - 1]
	    ---------------------------------
	    (3,-2)(3,4)(3,-1)
	    (-2,4)(4,-1)
	    -----------------------
	    Positive pairs : 5
	*/
	positivePairs(arr2, n)
	// Test C
	n = len(arr3)
	/*
	    arr = [1, 2, 1]
	    ----------------
	    (1,2)(1,1)(2,1)
	    -------------------
	    Positive pairs : 3
	*/
	positivePairs(arr3, n)
	// Test D
	n = len(arr4)
	/*
	    arr = [1, -2, -1]
	    ----------------
	    None
	    -------------------
	    Positive pairs : 0
	*/
	positivePairs(arr4, n)
}

Output

 Given array :  1 2 -3 -2 1 3
 Number of positive pairs is : 7
 Given array :  3 -2 4 -1
 Number of positive pairs is : 5
 Given array :  1 2 1
 Number of positive pairs is : 3
 Given array :  1 -2 -1
 Number of positive pairs is : 0
<?php
// Php program for
// Count number of positive pairs in an array
class Pairs
{
	// Display array elements
	public function printArray($arr, $n)
	{
		for ($i = 0; $i < $n; ++$i)
		{
			echo(" ".$arr[$i]);
		}
	}
	public function positivePairs($arr, $n)
	{
		if ($n <= 0)
		{
			return;
		}
		$count = 0;
		// Outer loop from 0...n
		for ($i = 0; $i < $n; ++$i)
		{
			// Inner loop from i+1...n
			for ($j = $i + 1; $j < $n; ++$j)
			{
				if ($arr[$i] + $arr[$j] > 0)
				{
					// When pair sum is positive
					$count++;
				}
			}
		}
		// Display given array
		echo("\n Given array : ");
		$this->printArray($arr, $n);
		// Display calculated result
		echo("\n Number of positive pairs is : ".$count);
	}
}

function main()
{
	$task = new Pairs();
	$arr1 = array(1, 2, -3, -2, 1, 3);
	$arr2 = array(3, -2, 4, -1);
	$arr3 = array(1, 2, 1);
	$arr4 = array(1, -2, -1);
	// Test A
	$n = count($arr1);
	/*
	    arr = [ 1,  2, -3, -2, 1, 3 ]
	    ----------------------------------
	    (1,2)(1,1)(1,3)
	    (2,1)(2,3)(-2,3)
	    (1,3)
	    -----------------------
	    Positive pairs : 7
	*/
	$task->positivePairs($arr1, $n);
	// Test B
	$n = count($arr2);
	/*
	    arr = [3, -2, 4, - 1]
	    ---------------------------------
	    (3,-2)(3,4)(3,-1)
	    (-2,4)(4,-1)
	    -----------------------
	    Positive pairs : 5
	*/
	$task->positivePairs($arr2, $n);
	// Test C
	$n = count($arr3);
	/*
	    arr = [1, 2, 1]
	    ----------------
	    (1,2)(1,1)(2,1)
	    -------------------
	    Positive pairs : 3
	*/
	$task->positivePairs($arr3, $n);
	// Test D
	$n = count($arr4);
	/*
	    arr = [1, -2, -1]
	    ----------------
	    None
	    -------------------
	    Positive pairs : 0
	*/
	$task->positivePairs($arr4, $n);
}
main();

Output

 Given array :  1 2 -3 -2 1 3
 Number of positive pairs is : 7
 Given array :  3 -2 4 -1
 Number of positive pairs is : 5
 Given array :  1 2 1
 Number of positive pairs is : 3
 Given array :  1 -2 -1
 Number of positive pairs is : 0
// Node JS program for
// Count number of positive pairs in an array
class Pairs
{
	// Display array elements
	printArray(arr, n)
	{
		for (var i = 0; i < n; ++i)
		{
			process.stdout.write(" " + arr[i]);
		}
	}
	positivePairs(arr, n)
	{
		if (n <= 0)
		{
			return;
		}
		var count = 0;
		// Outer loop from 0...n
		for (var i = 0; i < n; ++i)
		{
			// Inner loop from i+1...n
			for (var j = i + 1; j < n; ++j)
			{
				if (arr[i] + arr[j] > 0)
				{
					// When pair sum is positive
					count++;
				}
			}
		}
		// Display given array
		process.stdout.write("\n Given array : ");
		this.printArray(arr, n);
		// Display calculated result
		process.stdout.write("\n Number of positive pairs is : " + count);
	}
}

function main()
{
	var task = new Pairs();
	var arr1 = [1, 2, -3, -2, 1, 3];
	var arr2 = [3, -2, 4, -1];
	var arr3 = [1, 2, 1];
	var arr4 = [1, -2, -1];
	// Test A
	var n = arr1.length;
	/*
	    arr = [ 1,  2, -3, -2, 1, 3 ]
	    ----------------------------------
	    (1,2)(1,1)(1,3)
	    (2,1)(2,3)(-2,3)
	    (1,3)
	    -----------------------
	    Positive pairs : 7
	*/
	task.positivePairs(arr1, n);
	// Test B
	n = arr2.length;
	/*
	    arr = [3, -2, 4, - 1]
	    ---------------------------------
	    (3,-2)(3,4)(3,-1)
	    (-2,4)(4,-1)
	    -----------------------
	    Positive pairs : 5
	*/
	task.positivePairs(arr2, n);
	// Test C
	n = arr3.length;
	/*
	    arr = [1, 2, 1]
	    ----------------
	    (1,2)(1,1)(2,1)
	    -------------------
	    Positive pairs : 3
	*/
	task.positivePairs(arr3, n);
	// Test D
	n = arr4.length;
	/*
	    arr = [1, -2, -1]
	    ----------------
	    None
	    -------------------
	    Positive pairs : 0
	*/
	task.positivePairs(arr4, n);
}
main();

Output

 Given array :  1 2 -3 -2 1 3
 Number of positive pairs is : 7
 Given array :  3 -2 4 -1
 Number of positive pairs is : 5
 Given array :  1 2 1
 Number of positive pairs is : 3
 Given array :  1 -2 -1
 Number of positive pairs is : 0
#  Python 3 program for
#  Count number of positive pairs in an array
class Pairs :
	#  Display list elements
	def printArray(self, arr, n) :
		i = 0
		while (i < n) :
			print(" ", arr[i], end = "")
			i += 1
		
	
	def positivePairs(self, arr, n) :
		if (n <= 0) :
			return
		
		count = 0
		i = 0
		#  Outer loop from 0...n
		while (i < n) :
			j = i + 1
			#  Inner loop from i+1...n
			while (j < n) :
				if (arr[i] + arr[j] > 0) :
					#  When pair sum is positive
					count += 1
				
				j += 1
			
			i += 1
		
		#  Display given list
		print("\n Given array : ", end = "")
		self.printArray(arr, n)
		#  Display calculated result
		print("\n Number of positive pairs is : ", count, end = "")
	

def main() :
	task = Pairs()
	arr1 = [1, 2, -3, -2, 1, 3]
	arr2 = [3, -2, 4, -1]
	arr3 = [1, 2, 1]
	arr4 = [1, -2, -1]
	#  Test A
	n = len(arr1)
	#    arr = [ 1,  2, -3, -2, 1, 3 ]
	#    ----------------------------------
	#    (1,2)(1,1)(1,3)
	#    (2,1)(2,3)(-2,3)
	#    (1,3)
	#    -----------------------
	#    Positive pairs : 7
	task.positivePairs(arr1, n)
	#  Test B
	n = len(arr2)
	#    arr = [3, -2, 4, - 1]
	#    ---------------------------------
	#    (3,-2)(3,4)(3,-1)
	#    (-2,4)(4,-1)
	#    -----------------------
	#    Positive pairs : 5
	task.positivePairs(arr2, n)
	#  Test C
	n = len(arr3)
	#    arr = [1, 2, 1]
	#    ----------------
	#    (1,2)(1,1)(2,1)
	#    -------------------
	#    Positive pairs : 3
	task.positivePairs(arr3, n)
	#  Test D
	n = len(arr4)
	#    arr = [1, -2, -1]
	#    ----------------
	#    None
	#    -------------------
	#    Positive pairs : 0
	task.positivePairs(arr4, n)

if __name__ == "__main__": main()

Output

 Given array :   1  2  -3  -2  1  3
 Number of positive pairs is :  7
 Given array :   3  -2  4  -1
 Number of positive pairs is :  5
 Given array :   1  2  1
 Number of positive pairs is :  3
 Given array :   1  -2  -1
 Number of positive pairs is :  0
#  Ruby program for
#  Count number of positive pairs in an array
class Pairs 
	#  Display array elements
	def printArray(arr, n) 
		i = 0
		while (i < n) 
			print(" ", arr[i])
			i += 1
		end

	end

	def positivePairs(arr, n) 
		if (n <= 0) 
			return
		end

		count = 0
		i = 0
		#  Outer loop from 0...n
		while (i < n) 
			j = i + 1
			#  Inner loop from i+1...n
			while (j < n) 
				if (arr[i] + arr[j] > 0) 
					#  When pair sum is positive
					count += 1
				end

				j += 1
			end

			i += 1
		end

		#  Display given array
		print("\n Given array : ")
		self.printArray(arr, n)
		#  Display calculated result
		print("\n Number of positive pairs is : ", count)
	end

end

def main() 
	task = Pairs.new()
	arr1 = [1, 2, -3, -2, 1, 3]
	arr2 = [3, -2, 4, -1]
	arr3 = [1, 2, 1]
	arr4 = [1, -2, -1]
	#  Test A
	n = arr1.length
	#    arr = [ 1,  2, -3, -2, 1, 3 ]
	#    ----------------------------------
	#    (1,2)(1,1)(1,3)
	#    (2,1)(2,3)(-2,3)
	#    (1,3)
	#    -----------------------
	#    Positive pairs : 7
	task.positivePairs(arr1, n)
	#  Test B
	n = arr2.length
	#    arr = [3, -2, 4, - 1]
	#    ---------------------------------
	#    (3,-2)(3,4)(3,-1)
	#    (-2,4)(4,-1)
	#    -----------------------
	#    Positive pairs : 5
	task.positivePairs(arr2, n)
	#  Test C
	n = arr3.length
	#    arr = [1, 2, 1]
	#    ----------------
	#    (1,2)(1,1)(2,1)
	#    -------------------
	#    Positive pairs : 3
	task.positivePairs(arr3, n)
	#  Test D
	n = arr4.length
	#    arr = [1, -2, -1]
	#    ----------------
	#    None
	#    -------------------
	#    Positive pairs : 0
	task.positivePairs(arr4, n)
end

main()

Output

 Given array :  1 2 -3 -2 1 3
 Number of positive pairs is : 7
 Given array :  3 -2 4 -1
 Number of positive pairs is : 5
 Given array :  1 2 1
 Number of positive pairs is : 3
 Given array :  1 -2 -1
 Number of positive pairs is : 0
// Scala program for
// Count number of positive pairs in an array
class Pairs()
{
	// Display array elements
	def printArray(arr: Array[Int], n: Int): Unit = {
		var i: Int = 0;
		while (i < n)
		{
			print(" " + arr(i));
			i += 1;
		}
	}
	def positivePairs(arr: Array[Int], n: Int): Unit = {
		if (n <= 0)
		{
			return;
		}
		var count: Int = 0;
		var i: Int = 0;
		// Outer loop from 0...n
		while (i < n)
		{
			var j: Int = i + 1;
			// Inner loop from i+1...n
			while (j < n)
			{
				if (arr(i) + arr(j) > 0)
				{
					// When pair sum is positive
					count += 1;
				}
				j += 1;
			}
			i += 1;
		}
		// Display given array
		print("\n Given array : ");
		printArray(arr, n);
		// Display calculated result
		print("\n Number of positive pairs is : " + count);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Pairs = new Pairs();
		var arr1: Array[Int] = Array(1, 2, -3, -2, 1, 3);
		var arr2: Array[Int] = Array(3, -2, 4, -1);
		var arr3: Array[Int] = Array(1, 2, 1);
		var arr4: Array[Int] = Array(1, -2, -1);
		// Test A
		var n: Int = arr1.length;
		/*
		    arr = [ 1,  2, -3, -2, 1, 3 ]
		    ----------------------------------
		    (1,2)(1,1)(1,3)
		    (2,1)(2,3)(-2,3)
		    (1,3)
		    -----------------------
		    Positive pairs : 7
		*/
		task.positivePairs(arr1, n);
		// Test B
		n = arr2.length;
		/*
		    arr = [3, -2, 4, - 1]
		    ---------------------------------
		    (3,-2)(3,4)(3,-1)
		    (-2,4)(4,-1)
		    -----------------------
		    Positive pairs : 5
		*/
		task.positivePairs(arr2, n);
		// Test C
		n = arr3.length;
		/*
		    arr = [1, 2, 1]
		    ----------------
		    (1,2)(1,1)(2,1)
		    -------------------
		    Positive pairs : 3
		*/
		task.positivePairs(arr3, n);
		// Test D
		n = arr4.length;
		/*
		    arr = [1, -2, -1]
		    ----------------
		    None
		    -------------------
		    Positive pairs : 0
		*/
		task.positivePairs(arr4, n);
	}
}

Output

 Given array :  1 2 -3 -2 1 3
 Number of positive pairs is : 7
 Given array :  3 -2 4 -1
 Number of positive pairs is : 5
 Given array :  1 2 1
 Number of positive pairs is : 3
 Given array :  1 -2 -1
 Number of positive pairs is : 0
import Foundation;
// Swift 4 program for
// Count number of positive pairs in an array
class Pairs
{
	// Display array elements
	func printArray(_ arr: [Int], _ n: Int)
	{
		var i: Int = 0;
		while (i < n)
		{
			print(" ", arr[i], terminator: "");
			i += 1;
		}
	}
	func positivePairs(_ arr: [Int], _ n: Int)
	{
		if (n <= 0)
		{
			return;
		}
		var count: Int = 0;
		var i: Int = 0;
		// Outer loop from 0...n
		while (i < n)
		{
			var j: Int = i + 1;
			// Inner loop from i+1...n
			while (j < n)
			{
				if (arr[i] + arr[j] > 0)
				{
					// When pair sum is positive
					count += 1;
				}
				j += 1;
			}
			i += 1;
		}
		// Display given array
		print("\n Given array : ", terminator: "");
		self.printArray(arr, n);
		// Display calculated result
		print("\n Number of positive pairs is : ", count, terminator: "");
	}
}
func main()
{
	let task: Pairs = Pairs();
	let arr1: [Int] = [1, 2, -3, -2, 1, 3];
	let arr2: [Int] = [3, -2, 4, -1];
	let arr3: [Int] = [1, 2, 1];
	let arr4: [Int] = [1, -2, -1];
	// Test A
	var n: Int = arr1.count;
	/*
	    arr = [ 1,  2, -3, -2, 1, 3 ]
	    ----------------------------------
	    (1,2)(1,1)(1,3)
	    (2,1)(2,3)(-2,3)
	    (1,3)
	    -----------------------
	    Positive pairs : 7
	*/
	task.positivePairs(arr1, n);
	// Test B
	n = arr2.count;
	/*
	    arr = [3, -2, 4, - 1]
	    ---------------------------------
	    (3,-2)(3,4)(3,-1)
	    (-2,4)(4,-1)
	    -----------------------
	    Positive pairs : 5
	*/
	task.positivePairs(arr2, n);
	// Test C
	n = arr3.count;
	/*
	    arr = [1, 2, 1]
	    ----------------
	    (1,2)(1,1)(2,1)
	    -------------------
	    Positive pairs : 3
	*/
	task.positivePairs(arr3, n);
	// Test D
	n = arr4.count;
	/*
	    arr = [1, -2, -1]
	    ----------------
	    None
	    -------------------
	    Positive pairs : 0
	*/
	task.positivePairs(arr4, n);
}
main();

Output

 Given array :   1  2  -3  -2  1  3
 Number of positive pairs is :  7
 Given array :   3  -2  4  -1
 Number of positive pairs is :  5
 Given array :   1  2  1
 Number of positive pairs is :  3
 Given array :   1  -2  -1
 Number of positive pairs is :  0
// Kotlin program for
// Count number of positive pairs in an array
class Pairs
{
	// Display array elements
	fun printArray(arr: Array < Int > , n: Int): Unit
	{
		var i: Int = 0;
		while (i < n)
		{
			print(" " + arr[i]);
			i += 1;
		}
	}
	fun positivePairs(arr: Array < Int > , n: Int): Unit
	{
		if (n <= 0)
		{
			return;
		}
		var count: Int = 0;
		var i: Int = 0;
		// Outer loop from 0...n
		while (i < n)
		{
			var j: Int = i + 1;
			// Inner loop from i+1...n
			while (j < n)
			{
				if (arr[i] + arr[j] > 0)
				{
					// When pair sum is positive
					count += 1;
				}
				j += 1;
			}
			i += 1;
		}
		// Display given array
		print("\n Given array : ");
		this.printArray(arr, n);
		// Display calculated result
		print("\n Number of positive pairs is : " + count);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Pairs = Pairs();
	val arr1: Array < Int > = arrayOf(1, 2, -3, -2, 1, 3);
	val arr2: Array < Int > = arrayOf(3, -2, 4, -1);
	val arr3: Array < Int > = arrayOf(1, 2, 1);
	val arr4: Array < Int > = arrayOf(1, -2, -1);
	// Test A
	var n: Int = arr1.count();
	/*
	    arr = [ 1,  2, -3, -2, 1, 3 ]
	    ----------------------------------
	    (1,2)(1,1)(1,3)
	    (2,1)(2,3)(-2,3)
	    (1,3)
	    -----------------------
	    Positive pairs : 7
	*/
	task.positivePairs(arr1, n);
	// Test B
	n = arr2.count();
	/*
	    arr = [3, -2, 4, - 1]
	    ---------------------------------
	    (3,-2)(3,4)(3,-1)
	    (-2,4)(4,-1)
	    -----------------------
	    Positive pairs : 5
	*/
	task.positivePairs(arr2, n);
	// Test C
	n = arr3.count();
	/*
	    arr = [1, 2, 1]
	    ----------------
	    (1,2)(1,1)(2,1)
	    -------------------
	    Positive pairs : 3
	*/
	task.positivePairs(arr3, n);
	// Test D
	n = arr4.count();
	/*
	    arr = [1, -2, -1]
	    ----------------
	    None
	    -------------------
	    Positive pairs : 0
	*/
	task.positivePairs(arr4, n);
}

Output

 Given array :  1 2 -3 -2 1 3
 Number of positive pairs is : 7
 Given array :  3 -2 4 -1
 Number of positive pairs is : 5
 Given array :  1 2 1
 Number of positive pairs is : 3
 Given array :  1 -2 -1
 Number of positive pairs is : 0


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved