Given a sorted and rotated array, find if there is a pair of number with a given sum(method -1)

Question - 

find a pair of element with the given sum in sorted or unsorted array.

Example - 

Input : arr[]={1,2,3,4,5} target=5  n=5

Output : 

1 + 4 = 5

2 + 3 = 5

method 1 : using nested loop

Approach - 

first we pick a number and check with next all number and then pick second number and check with next all number if sum is equal to target then print value otherwise leave it.

Implementation - 


#include<iostream>

using namespace std;

void findpair(int arr[],int n,int target)

{

    for(int i=0;i<n;i++){

        for(int j=0;j<n;j++){

            if(arr[i]+arr[j]==target)

            cout<<arr[i]<<" + "<<arr[j]<<" = "<<target<<endl;

        }

    }

}

int main()

{

    int n;

    cout<<"Enter the no of element : ";cin>>n;

    int target;

    cout<<"Enter target number : ";cin>>target;

    int arr[n];

    cout<<"Enter elements : "<<endl;

    for(int i=0;i<n;i++)

        cin>>arr[i];

    findpair(arr,n,target);

    return 0;

}

Time complixity - O(n!)

Comments