原创转载请注明出处:http://agilestyle.iteye.com/blog/2361909
Question
Print all pairs of values a sorted array that sum up to a given value M
Example
Solution
1. Scan from both ends, calculate the sum
2. if(sum is equal to M), print and shrink the focus window
3. if(sum is less than M), shift left focus index by 1
4. if(sum is bigger than M), shift right focus index by 1
5. Stop the loop when left is larger or equal to right index
package org.fool.java.test; public class PrintAllPairsSumToM { public static void main(String[] args) { int[] sorted = {1, 2, 3, 4, 8, 9, 9, 10}; printAllPairs(sorted, 12); } private static void printAllPairs(int[] sorted, int m) { int left = 0; int right = sorted.length - 1; while (left < right) { int tempSum = sorted[left] + sorted[right]; if (tempSum == m) { // which means we find one pair System.out.println("Sum of {" + sorted[left] + ", " + sorted[right] + "} = " + m); // update the both index values left++; right--; } else if (tempSum > m) { // which means the temp sum is bigger than m right--; // shift right focus index by 1 } else { // which means the temp sum is smaller than m left++; // shift left focus index by 1 } } } }
Console Output
Reference
https://www.youtube.com/watch?v=l5orcTJlT54&list=PLlhDxqlV_-vkak9feCSrnjlrnzzzcopSG&index=39
相关推荐
Give a divide and conquer algorithm for the following problem: you are given two sorted lists of size m and n, and are allowed unit time access to the ith element of each list. Give an O(lg m + lgn) ...
_.where(list, properties) Looks through each value in the list, returning an array of all the values that contain all of the key-value pairs listed in _.findWhere(list, properties) Looks through the ...
Allpairs.pl is a Perl script that constructs a reasonably small set of test cases that include all pairings of each value of each of a set of parameters.
This problem requires that you write a program to compute the exact value of Rn where R is a real number ( 0.0 ) and n is an integer such that 0 输入说明 The input will consist of a set of pairs of...
Pages in a b+tree are usually implemented as a list or array of child pointers and so while finding and inserting a value is a O(log k) operation the process actually has to move children around in ...
g) a full house (i.e., two cards of one face value and three cards of another face value) [Hint: Add methods getFace and getSuit to class Card of Fig. 7.9.] 4.(7.31Card Shuffling and Dealing) Use the...
The article describes a new algorithm for calibrating a Kinect sensor that achieves high accuracy using only 6 to 10 image-disparity pairs of a planar checkerboard pattern. The method estimates the ...
- **Objective:** Write a function that takes a list of numbers and returns their sum. - **Key Concepts:** - Looping over elements in a list. - Using the `sum()` built-in function. - Handling ...
This problem requires that you write a program to compute the exact value of Rn where R is a real number ( 0.0 ) and n is an integer such that 0 Input The input will consist of a set of pairs of ...
Pointer arrays are a special type of array that holds pointers to objects rather than the objects themselves. They are used when direct memory management is required or when working with Core ...
C is a NxN cost (perhaps distance) matrix, where C(I,J) contains the value of the cost to move from point I to point J NOTE: only valid with A as the first input E is a Px2 matrix containing a ...
Hence, it becomes possible for the attacker to issue a command to all the nodes, that target a single node (for example, all nodes in the botnet might be commanded by the attacker to send a TCP SYN ...
Called by the servlet container to indicate to a servlet that the servlet is being taken out of service. destroy() - Method in class javax.servlet.GenericServlet Called by the servlet container to ...
c++ abc159f AC 代码,适合经常写atcoder的人进行访问。同时,这也有助于你学习c++,你也可以拿此数据进行调试、对拍,找出你的错误。...Find the sum of f(L,R) over all pairs of integers (L,R) such that 1≤L≤R
All of the strings in this range will share a growing prefix. Each time the prefix grows, we can output a character. y +--------------------------+ Uncomp- | V ressed | +---------+ p +----------+...
leetcode伪代码numbers-of-good-pairs 题目解读: 题目来源: 原文: Given an array of integers nums. A pair (i,j) is called good if nums[i] == nums[j] and i < j. Return the number of good pairs. 解读: ...