Find all pairs whose sum does not exist in the array

Given an array which includes integer elements. Find the all pairs in this array which sums does not contain to this array. For example.

Example A
arr[] = {1,2,3}
Output :
   ➀  (1,3) 1+3 = 4
   ➁  (2,3) 2+3 = 5
   -------------------
    Note (1+2) = 3 is exist in array


Example B
arr[] = {2,2,4,1,1,3}
Output :
    (2,4)
    (2,3)
    (2,4)
    (2,3)
    (4,1)
    (4,1)
    (4,3)
    -------------------
    Note (2,2),(1,1),(1,3),(3,1) etc
       pair sum exist in array 
 
import java.util.HashSet;
/*
    Java Program
    Find all pairs whose sum does not exist in the array
*/
public class Pairs
{
    public void findPairs(int[] arr, int n)
    {
        HashSet < Integer > record = new HashSet < Integer > ();
        for (int i = 0; i < n; ++i)
        {
            // Get unique element
            record.add(arr[i]);
        }
        // Outer loop
        for (int i = 0; i < n; ++i)
        {
            // Inner Loop (i+1..n-1)
            for (int j = i + 1; j < n; ++j)
            {
                if (!record.contains(arr[i] + arr[j]))
                {
                    // When sum not exist in array
                    System.out.println(" (" + arr[i] + "," + arr[j] + ")");
                }
            }
        }
    }
    public static void main(String[] args)
    {
        Pairs task = new Pairs();
        int[] arr = {
            1 , 5 , 7 , 12 , 3 , 4
        };
        int n = arr.length;
        /*
             Pair    Sum
            (1+5)  : 6
            (1+7)  : 8
            (1+12) : 13
            (5+12) : 17
            (5+3)  : 8
            (5+4)  : 9
            (7+12) : 19
            (7+3)  : 10
            (7+4)  : 11
            (12+3) : 15
            (12+4) : 16
        */
        task.findPairs(arr, n);
    }
}

Output

 (1,5)
 (1,7)
 (1,12)
 (5,12)
 (5,3)
 (5,4)
 (7,12)
 (7,3)
 (7,4)
 (12,3)
 (12,4)
// Include header file
#include <iostream>

#include <set>

using namespace std;
/*
    C++ Program
    Find all pairs whose sum does not exist in the array
*/
class Pairs
{
    public: void findPairs(int arr[], int n)
    {
        set < int > record;
        for (int i = 0; i < n; ++i)
        {
            // Get unique element
            record.insert(arr[i]);
        }
        // Outer loop
        for (int i = 0; i < n; ++i)
        {
            // Inner Loop (i+1..n-1)
            for (int j = i + 1; j < n; ++j)
            {
                if (record.find(arr[i] + arr[j]) == record.end())
                {
                    // When sum not exist in array
                    cout << " (" << arr[i] << "," << arr[j] << ")" << endl;
                }
            }
        }
    }
};
int main()
{
    Pairs *task = new Pairs();
    int arr[] = {
        1 , 5 , 7 , 12 , 3 , 4
    };
    int n = sizeof(arr) / sizeof(arr[0]);
    /*
         Pair    Sum
        (1+5)  : 6
        (1+7)  : 8
        (1+12) : 13
        (5+12) : 17
        (5+3)  : 8
        (5+4)  : 9
        (7+12) : 19
        (7+3)  : 10
        (7+4)  : 11
        (12+3) : 15
        (12+4) : 16
    */
    task->findPairs(arr, n);
    return 0;
}

Output

 (1,5)
 (1,7)
 (1,12)
 (5,12)
 (5,3)
 (5,4)
 (7,12)
 (7,3)
 (7,4)
 (12,3)
 (12,4)
// Include namespace system
using System;
using System.Collections.Generic;
/*
    Csharp Program
    Find all pairs whose sum does not exist in the array
*/
public class Pairs
{
    public void findPairs(int[] arr, int n)
    {
        HashSet < int > record = new HashSet < int > ();
        for (int i = 0; i < n; ++i)
        {
            // Get unique element
            record.Add(arr[i]);
        }
        // Outer loop
        for (int i = 0; i < n; ++i)
        {
            // Inner Loop (i+1..n-1)
            for (int j = i + 1; j < n; ++j)
            {
                if (!record.Contains(arr[i] + arr[j]))
                {
                    // When sum not exist in array
                    Console.WriteLine(" (" + arr[i] + "," + arr[j] + ")");
                }
            }
        }
    }
    public static void Main(String[] args)
    {
        Pairs task = new Pairs();
        int[] arr = {
            1 , 5 , 7 , 12 , 3 , 4
        };
        int n = arr.Length;
        /*
             Pair    Sum
            (1+5)  : 6
            (1+7)  : 8
            (1+12) : 13
            (5+12) : 17
            (5+3)  : 8
            (5+4)  : 9
            (7+12) : 19
            (7+3)  : 10
            (7+4)  : 11
            (12+3) : 15
            (12+4) : 16
        */
        task.findPairs(arr, n);
    }
}

Output

 (1,5)
 (1,7)
 (1,12)
 (5,12)
 (5,3)
 (5,4)
 (7,12)
 (7,3)
 (7,4)
 (12,3)
 (12,4)
package main
import "fmt"
/*
    Go Program
    Find all pairs whose sum does not exist in the array
*/

func findPairs(arr[] int, n int) {
    var record = make(map[int] bool)
    for i := 0 ; i < n ; i++ {
        // Get unique element
        record[arr[i]] = true
    }
    // Outer loop
    for i := 0 ; i < n ; i++ {
        // Inner Loop (i+1..n-1)
        for j := i + 1 ; j < n ; j++ {
            if _, found := record[arr[i] + arr[j]] ; !found {
                // When sum not exist in array
                fmt.Println(" (", arr[i], ",", arr[j], ")")
            }
        }
    }
}
func main() {
    
    var arr = [] int {1,5,7,12,3,4}
    var n int = len(arr)
    /*
         Pair    Sum
        (1+5)  : 6
        (1+7)  : 8
        (1+12) : 13
        (5+12) : 17
        (5+3)  : 8
        (5+4)  : 9
        (7+12) : 19
        (7+3)  : 10
        (7+4)  : 11
        (12+3) : 15
        (12+4) : 16
    */
    findPairs(arr, n)
}

Output

 (1,5)
 (1,7)
 (1,12)
 (5,12)
 (5,3)
 (5,4)
 (7,12)
 (7,3)
 (7,4)
 (12,3)
 (12,4)
<?php
/*
    Php Program
    Find all pairs whose sum does not exist in the array
*/
class Pairs
{
    public  function findPairs($arr, $n)
    {
        $record = array();
        for ($i = 0; $i < $n; ++$i)
        {
            // Get unique element
            if (!in_array($arr[$i], $record))
            {
                $record[] = $arr[$i];
            }
        }
        // Outer loop
        for ($i = 0; $i < $n; ++$i)
        {
            // Inner Loop (i+1..n-1)
            for ($j = $i + 1; $j < $n; ++$j)
            {
                if (!in_array($arr[$i] + $arr[$j], $record, TRUE))
                {
                    // When sum not exist in array
                    echo(" (".$arr[$i].
                        ",".$arr[$j].")\n");
                }
            }
        }
    }
}

function main()
{
    $task = new Pairs();
    $arr = array(1, 5, 7, 12, 3, 4);
    $n = count($arr);
    /*
         Pair    Sum
        (1+5)  : 6
        (1+7)  : 8
        (1+12) : 13
        (5+12) : 17
        (5+3)  : 8
        (5+4)  : 9
        (7+12) : 19
        (7+3)  : 10
        (7+4)  : 11
        (12+3) : 15
        (12+4) : 16
    */
    $task->findPairs($arr, $n);
}
main();

Output

 (1,5)
 (1,7)
 (1,12)
 (5,12)
 (5,3)
 (5,4)
 (7,12)
 (7,3)
 (7,4)
 (12,3)
 (12,4)
/*
    Node JS Program
    Find all pairs whose sum does not exist in the array
*/
class Pairs
{
    findPairs(arr, n)
    {
        var record = new Set();
        for (var i = 0; i < n; ++i)
        {
            // Get unique element
            record.add(arr[i]);
        }
        // Outer loop
        for (var i = 0; i < n; ++i)
        {
            // Inner Loop (i+1..n-1)
            for (var j = i + 1; j < n; ++j)
            {
                if (!record.has(arr[i] + arr[j]))
                {
                    // When sum not exist in array
                    console.log(" (" + arr[i] + "," + arr[j] + ")");
                }
            }
        }
    }
}

function main()
{
    var task = new Pairs();
    var arr = [1, 5, 7, 12, 3, 4];
    var n = arr.length;
    /*
         Pair    Sum
        (1+5)  : 6
        (1+7)  : 8
        (1+12) : 13
        (5+12) : 17
        (5+3)  : 8
        (5+4)  : 9
        (7+12) : 19
        (7+3)  : 10
        (7+4)  : 11
        (12+3) : 15
        (12+4) : 16
    */
    task.findPairs(arr, n);
}
main();

Output

 (1,5)
 (1,7)
 (1,12)
 (5,12)
 (5,3)
 (5,4)
 (7,12)
 (7,3)
 (7,4)
 (12,3)
 (12,4)
#    Python 3 Program
#    Find all pairs whose sum does not exist in the array
class Pairs :
    def findPairs(self, arr, n) :
        record = set()
        i = 0
        while (i < n) :
            #  Get unique element
            record.add(arr[i])
            i += 1
        
        i = 0
        #  Outer loop
        while (i < n) :
            j = i + 1
            #  Inner Loop (i+1..n-1)
            while (j < n) :
                if (not arr[i] + arr[j] in record) :
                    #  When sum not exist in list
                    print(" (", arr[i] ,",", arr[j] ,")")
                
                j += 1
            
            i += 1
        
    

def main() :
    task = Pairs()
    arr = [1, 5, 7, 12, 3, 4]
    n = len(arr)
    #     Pair    Sum
    #    (1+5)  : 6
    #    (1+7)  : 8
    #    (1+12) : 13
    #    (5+12) : 17
    #    (5+3)  : 8
    #    (5+4)  : 9
    #    (7+12) : 19
    #    (7+3)  : 10
    #    (7+4)  : 11
    #    (12+3) : 15
    #    (12+4) : 16
    task.findPairs(arr, n)

if __name__ == "__main__": main()

Output

 ( 1 , 5 )
 ( 1 , 7 )
 ( 1 , 12 )
 ( 5 , 12 )
 ( 5 , 3 )
 ( 5 , 4 )
 ( 7 , 12 )
 ( 7 , 3 )
 ( 7 , 4 )
 ( 12 , 3 )
 ( 12 , 4 )
require 'set'
#    Ruby Program
#    Find all pairs whose sum does not exist in the array
class Pairs 
    def findPairs(arr, n) 
        record = SortedSet.new()
        i = 0
        while (i < n) 
            #  Get unique element
            record.add(arr[i])
            i += 1
        end

        i = 0
        #  Outer loop
        while (i < n) 
            j = i + 1
            #  Inner Loop (i+1..n-1)
            while (j < n) 
                if (!record.include?(arr[i] + arr[j])) 
                    #  When sum not exist in array
                    print(" (", arr[i] ,",", arr[j] ,")", "\n")
                end

                j += 1
            end

            i += 1
        end

    end

end

def main() 
    task = Pairs.new()
    arr = [1, 5, 7, 12, 3, 4]
    n = arr.length
    #     Pair    Sum
    #    (1+5)  : 6
    #    (1+7)  : 8
    #    (1+12) : 13
    #    (5+12) : 17
    #    (5+3)  : 8
    #    (5+4)  : 9
    #    (7+12) : 19
    #    (7+3)  : 10
    #    (7+4)  : 11
    #    (12+3) : 15
    #    (12+4) : 16
    task.findPairs(arr, n)
end

main()

Output

 (1,5)
 (1,7)
 (1,12)
 (5,12)
 (5,3)
 (5,4)
 (7,12)
 (7,3)
 (7,4)
 (12,3)
 (12,4)
import scala.collection.mutable._;
/*
    Scala Program
    Find all pairs whose sum does not exist in the array
*/
class Pairs()
{
    def findPairs(arr: Array[Int], n: Int): Unit = {
        var record: Set[Int] = Set();
        var i: Int = 0;
        while (i < n)
        {
            // Get unique element
            record.add(arr(i));
            i += 1;
        }
        i = 0;
        // Outer loop
        while (i < n)
        {
            var j: Int = i + 1;
            // Inner Loop (i+1..n-1)
            while (j < n)
            {
                if (!record.contains(arr(i) + arr(j)))
                {
                    // When sum not exist in array
                    println(" (" + arr(i) + "," + arr(j) + ")");
                }
                j += 1;
            }
            i += 1;
        }
    }
}
object Main
{
    def main(args: Array[String]): Unit = {
        var task: Pairs = new Pairs();
        var arr: Array[Int] = Array(1, 5, 7, 12, 3, 4);
        var n: Int = arr.length;
        /*
             Pair    Sum
            (1+5)  : 6
            (1+7)  : 8
            (1+12) : 13
            (5+12) : 17
            (5+3)  : 8
            (5+4)  : 9
            (7+12) : 19
            (7+3)  : 10
            (7+4)  : 11
            (12+3) : 15
            (12+4) : 16
        */
        task.findPairs(arr, n);
    }
}

Output

 (1,5)
 (1,7)
 (1,12)
 (5,12)
 (5,3)
 (5,4)
 (7,12)
 (7,3)
 (7,4)
 (12,3)
 (12,4)
import Foundation;
/*
    Swift 4 Program
    Find all pairs whose sum does not exist in the array
*/
class Pairs
{
    func findPairs(_ arr: [Int], _ n: Int)
    {
        var record = Set<Int>();
        var i: Int = 0;
        while (i < n)
        {
            // Get unique element
            record.insert(arr[i]);
            i += 1;
        }
        i = 0;
        // Outer loop
        while (i < n)
        {
            var j: Int = i + 1;
            // Inner Loop (i+1..n-1)
            while (j < n)
            {
                if (!record.contains(arr[i] + arr[j]))
                {
                    // When sum not exist in array
                    print(" (", arr[i] ,",", arr[j] ,")");
                }
                j += 1;
            }
            i += 1;
        }
    }
}
func main()
{
    let task: Pairs = Pairs();
    let arr: [Int] = [1, 5, 7, 12, 3, 4];
    let n: Int = arr.count;
    /*
         Pair    Sum
        (1+5)  : 6
        (1+7)  : 8
        (1+12) : 13
        (5+12) : 17
        (5+3)  : 8
        (5+4)  : 9
        (7+12) : 19
        (7+3)  : 10
        (7+4)  : 11
        (12+3) : 15
        (12+4) : 16
    */
    task.findPairs(arr, n);
}
main();

Output

 ( 1 , 5 )
 ( 1 , 7 )
 ( 1 , 12 )
 ( 5 , 12 )
 ( 5 , 3 )
 ( 5 , 4 )
 ( 7 , 12 )
 ( 7 , 3 )
 ( 7 , 4 )
 ( 12 , 3 )
 ( 12 , 4 )
/*
    Kotlin Program
    Find all pairs whose sum does not exist in the array
*/
class Pairs
{
    fun findPairs(arr: Array < Int > , n: Int): Unit
    {
        val record = mutableSetOf< Int > ();
        var i: Int = 0;
        while (i < n)
        {
            // Get unique element
            record.add(arr[i]);
            i += 1;
        }
        i = 0;
        // Outer loop
        while (i < n)
        {
            var j: Int = i + 1;
            // Inner Loop (i+1..n-1)
            while (j < n)
            {
                if (!record.contains(arr[i] + arr[j]))
                {
                    // When sum not exist in array
                    println(" (" + arr[i] + "," + arr[j] + ")");
                }
                j += 1;
            }
            i += 1;
        }
    }
}
fun main(args: Array < String > ): Unit
{
    val task: Pairs = Pairs();
    val arr: Array < Int > = arrayOf(1, 5, 7, 12, 3, 4);
    val n: Int = arr.count();
    /*
         Pair    Sum
        (1+5)  : 6
        (1+7)  : 8
        (1+12) : 13
        (5+12) : 17
        (5+3)  : 8
        (5+4)  : 9
        (7+12) : 19
        (7+3)  : 10
        (7+4)  : 11
        (12+3) : 15
        (12+4) : 16
    */
    task.findPairs(arr, n);
}

Output

 (1,5)
 (1,7)
 (1,12)
 (5,12)
 (5,3)
 (5,4)
 (7,12)
 (7,3)
 (7,4)
 (12,3)
 (12,4)
Pairs whose sum does not exist in the array


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