[GeeksForGeeks] Find if there is a pair with a given sum in a sorted and rotated array.

Given a sorted array, this array is rotated by some unknown times. Find if there is a pair in this array

that sums to a given value.

Solution 1. O(n * log n) runtime, sort this array then use two pointers to check if there is a pair.

Solution 2. O(n) runtime, rotate this array back to its sorted order then use two pointers to check if there is a pair.

How to rotate: first find the split point where the natural order is broke; then apply the 3-step rotation algorithm.

Solution 3. optimal:  O(n) runtime, two pointers without modifying the given array.

 1 public boolean checkPairSum(int[] arr, int target) {
 2     if(arr == null || arr.length < 2) {
 3         return false;
 4     }
 5     int end = 0, start = 0;
 6     for(; end < arr.length - 1; end++) {
 7         if(arr[end] > arr[end + 1]) {
 8             break;
 9         }
10     }
11     start = (end + 1) % arr.length;
12     while(start != end) {
13         int sum = arr[start] + arr[end];
14         if(sum == target) {
15             return true;
16         }
17         else if(sum < target) {
18             start = (start + 1) % arr.length;
19         }
20         else {
21             end = (end - 1 + arr.length) % arr.length;
22         }
23     }
24     return false;
25 }
时间: 2024-10-10 10:17:26

[GeeksForGeeks] Find if there is a pair with a given sum in a sorted and rotated array.的相关文章

LintCode数组题总结

做算法题的时候,几乎不可避免要跟数组打交道.在LintCode上数组那一章有这么一些题目: 1)547. Intersection of Two Arrays 比较简单.要求找到2个数组的交集,简单点的方法就是用2个hashSet,第一个HashSet存第一个数组的元素.然后扫描第二个数组,如果第二个数组中的元素在第一个HashSet中出现了,那么就把它加到第二个HashSet中.最后第二个HashSet就是两个数组的交集了. 2)138. Subarray Sum 要求在一个数组中找到一个子数

哈希(5) - 检测数组A[]中是否存在元素对其和为x

给定一个包括n个数值的数组A[]以及另一个数字x,判断数组中是否存在一对元素,它们的和等于x. 方法1 (使用排序) 算法: hasArrayTwoCandidates (A[], arrSize, sum) 1) 对数组进行递增排序 2) 初始化已排序数组中的两个索引值 (a) 将最左侧的数组位置0做为第一个索引left = 0 (b) 将最右侧的数组位置做为第二个索引right = arrSize-1 3) Loop while left < right. (a) if (A[left] +

170. Two Sum III - Data structure design

Design and implement a TwoSum class. It should support the following operations: add and find. add - Add the number to an internal data structure.find - Find if there exists any pair of numbers which sum is equal to the value. For example, add(1); ad

[算法]K-SUM problem

一.Two Sum Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please

CodeIgniter源码分析之URI.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); // ------------------------------------------------------------------------ /** * URI Class */ class CI_URI { /** * List of cached uri segments */ var $keyval = array(); /** *

Codeforces Round #169 (Div. 2)C. Little Girl and Maximum Sum

传送门 Description The little girl loves the problems on array queries very much. One day she came across a rather well-known problem: you've got an array of n elements (the elements of the array are indexed starting from 1); also, there are q queries,

51nod 1065 最小正子段和

题目链接:51nod 1065 最小正子段和 房教说用前缀和做,然后看了别人博客懂了后就感觉,这个真有意思... 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 const int N = 50001; 6 const int inf = 0x3f3f3f3f; 7 pair<long long, int> sum[N]; 8 int

leetcode 锁掉的题目清单

也刷leetcode, 先把锁掉的题目留备份好了: 156 Binary Tree Upside Down  [1] Problem: Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that shares the same parent node) or empty, flip it upside down and turn it into a tre

Little Girl and Maximum Sum CodeForces - 276C

---恢复内容开始--- The little girl loves the problems on array queries very much. One day she came across a rather well-known problem: you've got an array of n elements (the elements of the array are indexed starting from 1); also, there are q queries, eac