PHP Program for Count Pairs with Given Sum Last Updated : 22 Jul, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Given an array of integers, and a number 'sum', find the number of pairs of integers in the array whose sum is equal to 'sum'.Examples: Input : Arr = [ 1, 5, 7, -1 ] sum = 6Output : 2Pairs with sum 6 are (1, 5) and (7, -1).Input : Arr = [ 1, 5, 7, -1, 5 ] sum = 6Output : 3Pairs with sum 6 are (1, 5), (7, -1) & (1, 5) Input : Arr = [ 1, 1, 1, 1 ] sum = 2Output : 6There are 3! pairs with sum 2.Input : Arr = [ 10, 12, 10, 15, -1, 7, 6, 5, 4, 2, 1, 1, 1 ] sum = 11Output : 9Expected Time Complexity: O(n)Naive Solution - A simple solution is to traverse each element and check if there's another number in the array which can be added to it to give sum. PHP <?php // PHP implementation of simple // method to find count of // pairs with given sum. // Returns number of pairs in // arr[0..n-1] with sum equal // to 'sum' function getPairsCount($arr, $n, $sum) { // Initialize result $count = 0; // Consider all possible pairs // and check their sums for ($i = 0; $i < $n; $i++) { for ($j = $i + 1; $j < $n; $j++) { if ($arr[$i] + $arr[$j] == $sum) { $count++; } }; } return $count; } // Driver Code $arr = [1, 5, 7, -1, 5]; $n = sizeof($arr); $sum = 6; echo "Count of pairs is " . getPairsCount($arr, $n, $sum); ?> OutputCount of pairs is 3Time Complexity: O(n2) Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article PHP Program for Pairs such that one is a power multiple of other K kartik Follow Improve Article Tags : Hash Web Technologies PHP Programs DSA Arrays Amazon Accolite Hike FactSet +5 More Practice Tags : AccoliteAmazonFactSetHikeArraysHash +2 More Similar Reads PHP Program for Number of pairs with maximum sum Write a PHP program for a given array arr[], count the number of pairs arr[i], arr[j] such that arr[i] + arr[j] is maximum and i < j.Example:Input : arr[] = {1, 1, 1, 2, 2, 2} Output: 3 Explanation: The maximum possible pair sum where i<j is 4, which is given by 3 pairs, so the answer is 3 the 3 min read PHP Program to Count Inversions of size three in a given array Given an array arr[] of size n. Three elements arr[i], arr[j] and arr[k] form an inversion of size 3 if a[i] > a[j] >a[k] and i < j < k. Find total number of inversions of size 3.Example : Input: {8, 4, 2, 1}Output: 4The four inversions are (8,4,2), (8,4,1), (4,2,1) and (8,2,1).Input: {9 4 min read PHP Program to Find Sum of All Matrix Elements Finding the sum of all elements in a matrix is a common operation in mathematical computations and programming. In PHP, this can be achieved using various approaches, including loops and array functions. In this article, we will explore different methods to calculate the sum of all elements in a mat 4 min read PHP Program for Pairs such that one is a power multiple of other You are given an array A[] of n-elements and a positive integer k (k > 1). Now you have find the number of pairs Ai, Aj such that Ai = Aj*(kx) where x is an integer. Note: (Ai, Aj) and (Aj, Ai) must be count once.Examples : Input : A[] = {3, 6, 4, 2}, k = 2Output : 2Explanation : We have only two 3 min read PHP Program to Count Rotations Divisible by 4 Given a large positive number as string, count all rotations of the given number which are divisible by 4. Examples: Input: 8Output: 1Input: 20Output: 1Rotation: 20 is divisible by 4, 02 is not divisible by 4Input : 13502Output : 0No rotation is divisible by 4Input : 43292816Output : 55 rotations ar 2 min read PHP Program to Calculate Combination nCr This article will show you how to Calculate the Combination nCr in PHP.Combinations refer to the different ways in which a set of items can be selected without considering the order. In mathematics, the number of combinations of 'n' distinct items taken 'r' at a time is denoted as nCr. The formula f 2 min read Like