Find a pair with given sum in the array

 Problem statement:- Program to Find a pair with a given sum in the array.

Example:-

              Input : Given 

                                size=5

                                 arr[]=[4,5,7,3,9] 

                                 sum=10

             Output:

                        pair(7,3)              

Data requirement:-

   Input Data:- size, arr, sum

  Output Data:-arr

  Additional Data:- i, j, flag

Program in C

Here is the source code of the C Program to Find a pair with a given sum in the array.

Code:

#include<stdio.h>
main()
{
    printf("Enter the size of the array:");
    int size;
    scanf("%d",&size);
    int arr[size];
    int i,temp,j;
    printf("Enter the Element of the array:\n");
    for(i=0;i<size;i++)
    {
        scanf("%d",&arr[i]);
    }
    printf("Enter the Sum Value:");
    int sum;
    scanf("%d",&sum);
    int flag=0;
    for(i=0;i<size-1;i++)
    {
        for(j=i+1;j<size;j++)
        {
            if(arr[i]+arr[j]==sum)
            {  flag=1;
                printf("Given sum pairs of elements are %d and %d.\n",arr[i],arr[j]);
            }

        }
    }
    if(flag==0)
    printf("Given sum Pair is not Present.");
}

Input/Output:
Enter the size of the array:5
Enter the Element of the array:
4 5 7 3 9
Enter the Sum Value:10
Given sum pairs of elements are 7 and 3.

Program in C++

Here is the source code of the C++ Program to Find a pair with a given sum in the array.

Code:

#include<iostream>
using namespace std;
main()
{
    cout<<"Enter the size of the array:";
    int size;
    cin>>size;
    int arr[size];
    int i,temp=0,j;
    cout<<"Enter the Element of the array:\n";
    for(i=0;i<size;i++)
    {
        cin>>arr[i];
    }

    cout<<"Enter the Sum Value:";
    int sum;
    cin>>sum;
    int flag=0;
    for(i=0;i<size-1;i++)
    {
        for(j=i+1;j<size;j++)
        {
            if(arr[i]+arr[j]==sum)
            {  flag=1;
                cout<<"Given sum pair of element are "<<arr[i]<<" and "<<arr[j]<<".\n";
            }

        }
    }
    if(flag==0)
    cout<<"Given sum Pair is not Present.";
}

Input/Output:
Enter the size of the array:6
Enter the Element of the array:
8 7 11 5 2 3
Enter the Sum Value:20
Given sum Pair is not Present.

Program in Java

Here is the source code of the Java Program to Find a pair with a given sum in the array.

Code:

import java.util.Scanner;
public class FindPair {

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
  System.out.println("Enter the size of the array:");
     int size,i,j;
     size=sc.nextInt();
     int arr[ ]=new int[size];
  System.out.println("Enter the Element of the array:");
      for(i=0;i<size;i++)
     {
         arr[i]=sc.nextInt();
     }
      System.out.print("Enter the Sum Value:");
      int sum=sc.nextInt();
      int flag=0;
      for(i=0;i<size-1;i++)
      {
          for(j=i+1;j<size;j++)
          {
              if(arr[i]+arr[j]==sum)
              {  flag=1;
        System.out.print("Given sum pairs of elements are "+arr[i]+" and "+arr[j]+".\n");
              }

          }
      }
      if(flag==0)
      System.out.print("Given sum Pair is not Present.");
      sc.close();
}
}

Input/Output:
Enter the size of the array:
4
Enter the Element of the array:
8
9
5
10
Enter the Sum Value:13
Given sum pairs of elements are 8 and 5.


Program in Python

Here is the source code of the Python Program to Find a pair with a given sum in the array.

Code:

arr=[]
size = int(input("Enter the size of the array: "))
print("Enter the Element of the array:")
for i in range(0,size):
    num = int(input())
    arr.append(num)

sum=int(input("Enter the Sum Value:"))
flag=0

for i in range(0,size-1):
    for j in range(i+1, size):
        if arr[i]+arr[j]==sum:
            flag=1
            print("Given sum pairs of elements are ", arr[i]," and ", arr[j],".\n")
if flag==0:
  print("Given sum Pair is not Present.")

Input/Output:
Enter the size of the array: 5
Enter the Element of the array:
4
7
10
5
14
Enter the Sum Value:14
Given sum pairs of elements are  4  and  10.

Post a Comment

0 Comments