65 views

Uploaded by Alisha Jain

Given a Sorted and Rotated Array

You are on page 1of 2

Given an array that is sorted and then rotated around an unknown point. Find if array has a pair with given

sum x. It may be assumed that all elements in array are distinct.

Examples:

Output: true

There is a pair (6, 10) with sum 16

Output: true

There is a pair (26, 9) with sum 35

Output: false

There is no pair with sum 45.

The idea is to first find the maximum element in array which is the pivot point also and the element just before

maximum is the minimum element. Once we have indexes maximum and minimum elements, we use similar

meet in middle algorithm (as discussed in Given an array A[] and a number x, check for pair in A[] with sum as

x in Searching and Sorting) to find if there is a pair. The only thing new here is indexes are incremented and

decremented in rotational manner using modular arithmetic.

// rotated array

#include<iostream>

using namespace std;

// with sum equals to x.

bool pairInSortedRotated(int arr[], int n, int x)

{

// Find the pivot element

int i;

for (i=0; i<n-1; i++)

if (arr[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

while (l != r)

{

// If we find a pair with sum x, we return true

if (arr[l] + arr[r] == x)

return true;

if (arr[l] + arr[r] < x)

l = (l + 1)%n;

else // Move to the lower sum side

r = (n + r - 1)%n;

}

return false;

}

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;

}

Output:

Array has two elements with sum 16

Time complexity of the above solution is O(n). The step to find the pivot can be optimized to O(Logn) using the

Binary Search approach discussed in Find the minimum element in a sorted and rotated array in Array

Rotations.

Exercise:

1) Extend the above solution to work for arrays with duplicates allowed.

2) Extend the above solution to find all pairs.

- Data StructuresUploaded bysabarish001
- Xam Nhap Qua RintUploaded byMinh Nguyễn
- UNIT4Uploaded byVivekananda Ganjigunta Narayana
- File Management in CUploaded byMoetaz Bellah Medhat Samy
- BankingUploaded byHarsh Sharma
- Program 1Uploaded byRohit Zealster
- chap 12.pdfUploaded byInblood
- CCS __ View topic - CRC16, very efficient.pdfUploaded byEsmael Prado
- mak.cppUploaded bymakhardoi
- C++ ProjectUploaded bykeshavjain7
- C in 7 daysUploaded byvksgyt
- All Technical QuestionsUploaded byVara Bikkina
- Tb40laan SortUploaded byGilmer Calderon Quispe
- ICS 2276 Chapter 3-OperatorsUploaded byKamau Gabriel
- RailwayUploaded byNidhi Narang
- CollectionsUploaded byArchana Jayaram Chandra Mohan
- Howto SortingUploaded byMarcelo
- C ProgramUploaded byvigneshaaa
- ArrayUploaded byBasic204
- Matlab -Graphs TutorialsUploaded byJaspreet Singh Sidhu
- Just Bring ItUploaded byTehsin Khan
- Data Structures NotesUploaded byKeith Tanaka Magaka
- Tcs Placement Papers - Tcs Aptitude Paper With Solutions (Id-2608)Uploaded bygvorab
- FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approachUploaded byWilliam Kilpatrick
- C Programming ExerciceUploaded byyankev
- DYNAMIC MEMORY ALLOCATION.docxUploaded byDaman Deep Singh Arneja
- eloquent JS - chapter 7 excersize.txtUploaded byJill Sparks
- 42412007 Mobile Service Provide DatabaseUploaded bySaab Kush
- Binary Search dasUploaded byAndrei Bzk
- Algorithm Cheat SheetUploaded byaskjdalsk ljalsdjalks

- 23098521-CA-7-Final-PptUploaded byklcekishore
- Oracle documents with sqlUploaded byRaja Shekar Komaragiri
- Java TestUploaded byakshay
- Round Robin TechniqueUploaded byveda
- ROC364 Specification Sheet.pdfUploaded byAlaa Ramadan
- Java Lab 9Uploaded byalligiant
- HID Corp 1000 Format Request and Authorization Form (v 03.23.2015)Uploaded byAnderson Cavalheiro
- History Of Flight SimulatorUploaded byMohamad Rizal Firmansjah
- Article10-TestNG-v1Uploaded byAnuj Batta
- AmdUploaded byAamir Rizwan
- HP Desktop June 2014Uploaded bysanjay1009
- GIS Consultant IslamabadUploaded byShahzad Asghar Arain
- English Advanced ManualUploaded bySimoes JB
- SAP RMUploaded bynarayana
- xeon-e7-v4-datasheet-vol-2Uploaded byg.negrini8304
- FlexSuite_User_English.pdfUploaded byEnrique Horta
- CFD 07 Grotjans Kainz ANSYS AIM-UpdateUploaded bymohamedezeldin
- inter process arbitrationUploaded bySampath Reddy Chada
- Build a Display in PI ProcessBookUploaded byZad!
- Encore Enhwi n3Uploaded byMarceloPeres
- HW1Uploaded bykuzhali_mozhi
- 12 Cool Tips and Tricks for iMovie ’11Uploaded byGary Story
- Oracle Forms 10gUploaded bydeepunym
- STM Oil and Gas Networks - Rev AUploaded bytituseapen
- 9781111135393_PPT_ch07Uploaded bykvsrvzm
- ES6510 Spec SheetUploaded byAdvantec Srl
- TCP IP Illustrated Volume 3Uploaded byChad Sawasky
- Brochure MaxLoad ProUploaded byNicholas Roy
- Linux Magazine Column 94__Moose.pdfUploaded byrommelgaspar
- Floating Point in QsysUploaded byLính Kỹ Thuật