From b2c4aeed267e740e0131362c2ef3bbafb40a0c11 Mon Sep 17 00:00:00 2001 From: verma420 <44649057+verma420@users.noreply.github.com> Date: Wed, 31 Oct 2018 22:52:31 +0530 Subject: [PATCH] verma.c done!!! --- verma.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 verma.c diff --git a/verma.c b/verma.c new file mode 100644 index 0000000..cfe34ba --- /dev/null +++ b/verma.c @@ -0,0 +1,47 @@ +// C++ program to find a pair with a given sum in a sorted and +// rotated array +#include +using namespace std; + +// This function returns true if arr[0..n-1] has a pair +// with sum equals to x. +bool pairInSortedRotated(int arr[], int n, int x) +{ + // Find the pivot element + int i; + for (i=0; i arr[i+1]) + break; + int l = (i+1)%n; // l is now index of minimum element + int r = i; // r is now index of maximum element + + // Keep moving either l or r till they meet + while (l != r) + { + // If we find a pair with sum x, we return true + if (arr[l] + arr[r] == x) + return true; + + // If current pair sum is less, move to the higher sum + if (arr[l] + arr[r] < x) + l = (l + 1)%n; + else // Move to the lower sum side + r = (n + r - 1)%n; + } + return false; +} + +/* Driver program to test above function */ +int main() +{ + int arr[] = {11, 15, 6, 8, 9, 10}; + int sum = 16; + int n = sizeof(arr)/sizeof(arr[0]); + + if (pairInSortedRotated(arr, n, sum)) + cout << "Array has two elements with sum 16"; + else + cout << "Array doesn't have two elements with sum 16 "; + + return 0; +}