Note 1

Take Note:

Take a note while surfing.





Note With Ink

Give your Note a Colorful Tag.




Easy to Access

Stay on same information and in Sync wherever you are.

Note 2

Take Note:

Organize your information,It may take Shape.





Think With Ink

Differ your Content by Color.




Easy to Access

Easy to pull up your content from anywhere anytime.

Note 3

Take Note:

Don't Let information to miss,Because it take shape





Note With Ink

Simple an Easy Way to take a note.




Easy to Access

Get the same in next visit.

Program Discussion :: Basics
Home > Programs > Basics

102. Given a sorted array and an item k, write a program to get a pair of items whose subtraction equal to K.

Answer:

#include <stdio.h>

#include<iostream>
using namespace std;
/* The function assumes that the array is sorted */
bool findPair(int arr[], int size, int n)
{
    /* Initialize positions of two elements */
    int i = 0;  
    int j = 1;
    /* Search for a pair */
    while (i<size && j<size)
    {
        if (i != j && arr[j]-arr[i] == n)
        {
            cout<<"Pair Found: "<< arr[i]<<"and"<< arr[j];
            return true;
        }
        else if (arr[j]-arr[i] < n)
            j++;
        else
            i++;
    }
    cout<<"No such pair";
    return false;
}

/* Driver program to test above function */
int main()
{
    int arr[] = {1, 8, 30, 40, 100};
    int size = sizeof(arr)/sizeof(arr[0]);
    int n = 60;
    findPair(arr, size, n);
    return 0;
}

Post Your Answer Here:

Name *
Email

Language:

Post Your Reply Here:



Language:

Post Your Reply Here: