Q. Write an algorithm and program to find all pairs of elements in an integer array whose sum is equal to a given number.

Solution : You are given an array and a number and task is to find all pairs of elements in an integer array whose sum is equal to a given number.

Array :- An array is a collection of data items, all of the same type, accessed using a common name.
For example: int arr[5]={1,2,3,4,5};

Algorithm to find pairs of elements in an integer array

//Algorithm To Find All Pairs Of Elements In An Integer Array.

START
  Step 1 ->  Input ARRAY arr[] and NUMBER num.
  Step 2 ->  for i=0 to length of the arr[]-1:
                for j=i+1 to length of the arr[]:
                    if arr[i]+arr[j] equals to num:
                        print the output
                Loop End
              Loop End
STOP


Program to find all pairs of elements in an integer array

  • C
  • C++
  • Java
  • Python
  • C#
  • PHP
  • //C Program To Find All Pairs Of Elements In An Integer Array.
    
    #include <stdio.h>
    
    int main()
    {
        int arr[7]={4, 6, 8, 10, 12, 14, 16};
        int num=18;
        for(int i=0;i<6;i++)
        {
            for(int j=i+1;j<7;j++)
            {
                    if((arr[i]+arr[j])==num)
                    {
                        printf("%d + %d = %d\n",arr[i],arr[j],num);
                    }
            }
        }
        return 0;
    }
    
    
    //C++ Program To Find All Pairs Of Elements In An Integer Array.
    
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int arr[7]={4, 6, 8, 10, 12, 14, 16};
        int num=18;
        for(int i=0;i<6;i++)
        {
            for(int j=i+1;j<7;j++)
            {
                    if((arr[i]+arr[j])==num)
                    {
                        cout<<arr[i]<<" + "<<arr[j]<<" = "<<num<<"\n";
                    }
            }
        }
        return 0;
    } 
    //Java Program To Find All Pairs Of Elements In An Integer Array.
    
    import java.util.Arrays;
     
    public class Main
    {
        public static void main(String[] args)
        {
            int arr[] = { 4, 6, 8, 10, 12, 14, 16 }; 
            int num = 18; 
            for(int i=0;i<6;i++)
            {
                for(int j=i+1;j<7;j++)
                {
                    if((arr[i]+arr[j])==num)
                    {
                        System.out.println(arr[i]+" + "+arr[j]+" = "+num);
                    }
                }
            }
        }
    }
    
    //Python Program To Find All Pairs Of Elements In An Integer Array.
     
    arr = [ 4, 6, 8, 10, 12, 14, 16 ]
    n = len(arr)
    num = 18
    for i in range(0, n): 
        for j in range(i+1, n):
            if((arr[i]+arr[j])==num):
                print(arr[i]," + ",arr[j]," = ",num) 
      
    //C# Program To Find All Pairs Of Elements In An Integer Array.
    
    using System;
    class LFC {
      static void Main() {
        int[] arr = { 4, 6, 8, 10, 12, 14, 16 }; 
        int num = 18;
        for(int i=0;i<6;i++)
            {
                for(int j=i+1;j<7;j++)
                {
                    if((arr[i]+arr[j])==num)
                    {
                        Console.WriteLine(arr[i]+" + "+arr[j]+" = "+num);
                    }
                }
            }
      }
    } 
    //PHP Program To Find All Pairs Of Elements In An Integer Array.
    
    <?php
    $arr = array(4, 6, 8, 10, 12, 14, 16); 
    $num = 18; 
    for($i=0;$i<6;$i++)
    {
        for($j=$i+1;$j<7;$j++)
        {
            if(($arr[$i]+$arr[$j])==$num)
            {
                echo "$arr[$i] + $arr[$j] = $num\n";
            }
        }
    }?>
    //C Program To Find All Pairs Of Elements In An Integer Array.
    
    #include <stdio.h>
    
    int main()
    {
        int arr[7]={4, 6, 8, 10, 12, 14, 16};
        int num=18;
        for(int i=0;i<6;i++)
        {
            for(int j=i+1;j<7;j++)
            {
                    if((arr[i]+arr[j])==num)
                    {
                        printf("%d + %d = %d\n",arr[i],arr[j],num);
                    }
            }
        }
        return 0;
    }
    
    

    Output

    4 + 14 = 18
    6 + 12 = 18
    8 + 10 = 18
    

    Recommended Programs

       Write an algorithm and program to subtract two matrices.
       Write an algorithm and program to reverse a string.