Two pointers technique
The Two Pointers technique is a strategy commonly used in computer science and programming for solving problems involving arrays or sequences. It involves using two pointers that traverse the array or sequence from different positions, often moving in opposite directions or at different speeds. This technique is particularly useful for solving problems related to searching, optimization, or manipulation of arrays efficiently.
Here's a breakdown of how the Two Pointers technique works:
Initialization: Initially, you set up two pointers, usually at different positions within the array or sequence.
Movement: You iteratively move the pointers based on certain conditions until they meet or reach a specific condition. The movement of pointers can be controlled based on the problem requirements. For example, one pointer might move faster than the other, or they might move in opposite directions.
Condition Checking: At each step of iteration, you check certain conditions based on the problem requirements. These conditions often determine whether to move the pointers, update some variables, or perform other operations.
Termination: The process continues until one or both pointers reach the end of the array or sequence, or until some other termination condition is met.
The Two Pointers technique is especially useful in problems that involve searching for pairs or subarrays that meet specific criteria, finding optimal solutions, or manipulating sequences efficiently without using nested loops. It often provides a more efficient solution compared to brute-force approaches, especially for problems with linear or logarithmic time complexity requirements.
Huseyn and Yaroslav are playing a card game. There are n cards laid out in a row on the table, each with a different number written on it. The players take turns. Huseyn starts the game. On each turn, a player can take either the leftmost or the rightmost card. The player will always choose the card with the highest number. The game ends when there are no cards left on the table. Find the sum of the numbers on the cards collected by Huseyn and Yaroslav at the end of the game.
Input. The first line contains the number of cards n~(1 \le n \le 10000) on the table. The second line contains positive integers, each indicating the number on a card. All numbers are not greater than 10^9.
Output. Print the sum of the numbers on the cards collected by Huseyn and Yaroslav at the end of the game.
7
4 7 5 1 12 8 218 21Huseyn arranged cards in a row with numbers a_1, a_2, a_3, ..., a_n. Then he collected them and rearranged them in a different order: a_1, a_3, a_5, ..., a_6, a_4, a_2. This is the sequence he gave to Yaroslav. Your task is to help Yaroslav restore Hussein's original sequence.
For example, if Yaroslav received the sequence (2, 4, 9, 4, 7, 6), then he should return the sequence (2, 6, 4, 7, 9, 4) to Huseyn.
Input. The first line contains one integer n~(1 \le n \le 10000), representing the number of cards. The second line contains positive integers written on the cards. All numbers are no greater than 10^9.
Output. Print Huseyn's original sequence.
6
2 4 9 4 7 62 6 4 7 9 47
5 7 34 1 89 4 25 2 7 4 34 89 1Huseyn has a string consisting of characters and . To entertain himself, he came up with the following game. Huseyn can perform one of two operations on the string:
append to the left end, and to the right end;
append to the right end, and to the left end;
For example, from the string , Huseyn can obtain or .
You are given a string obtained after all of Huseyn's operations (it is possible that he did not perform any operation). Determine the smallest possible length the string could have initially had.
Input. A single string of length no more than 10^5, consisting only of characters and .
Output. Print the smallest possible length of the string that Huseyn could have initially had.
01010010810011101Given an array , sorted in ascending order and containing integers. Determine whether there exists a pair of numbers (A_i, A_j), where i < j, such that their sum is equal to .
Input. The first line contains two integers n~(n \le 10^5) and x~(x \le 10^6). The second line contains non-negative integers, each of which is not greater than 10^6.
Output. Print "YES" if such a pair of elements exists, and "NO" otherwise.
10 13
1 3 5 6 8 10 11 11 11 16YES8 61
5 5 8 12 16 21 44 50NOGiven an array of integers and an integer . Find a triplet of numbers (A_i, A_j, A_k) in the array whose sum equals . All indices i, j, k should be different.
Input. The first line contains the size of the array n~(n \le 3 \cdot 10^4) and the value of x~(|x| \le 10^9). The second line contains integers, each of which does not exceed 10^8 in absolute value.
Output. If the required triplet exists, print it in any order. If multiple triplets exist, print any one of them. If the desired triplet does not exist, print .
8 19
20 3 5 1 15 7 17 121 3 15Given a sorted array of integers. For each index , find the number of elements in the array that lie between A_i and 2 \cdot A_i inclusive.
Input. The first line contains the size n~(n \le 10^5) of array . The second line contains integers, each ranging from to 10^9, in sorted order.
Output. Print integers. For each index i~(1 \le i \le n) of the array, print the number of elements lying between A_i and 2 \cdot A_i inclusive.
10
1 2 3 4 5 6 7 8 9 102 3 4 5 6 5 4 3 2 1Along the beautiful Adriatic coast, there are hotels. Each hotel has its cost in euros. Petr won euros in the lottery. Now he wants to buy a sequence of consecutive hotels one after another so that the sum of the costs of these consecutive hotels is as large as possible, but does not exceed .
You need to calculate this maximum possible total cost.
Input. The first line contains two integers and m~(1 \le n \le 3 \cdot 10^5, 1 \le m < 2^{31}). The next line contains positive integers less than 10^6, representing the costs of the hotels in the order they are located along the coast.
Output. Print the desired maximum cost (it will be greater than in all tests).
5 12
2 1 3 4 512Given an array of positive integers, find the number of subarrays whose sum is equal to .
Input. The first line contains the size of the array n~(1 \le n \le 2 \cdot 10^5) and the target sum x~(1 \le x \le 10^9). The next line contains integers a_1, a_2, \ldots, a_n~(1 \le a_i \le 10^9) — the contents of the array.
Output. Print the required number of subarrays.
5 7
2 4 1 2 73Ziya will take part in the sequence competition tomorrow. The number x \ge 0 is called the top of some sequence if the sequence 1, 2, 3, ..., x - 1, x, x - 1, ..., 3, 2, 1 is a subsequence of this sequence. The strength of each sequence is considered to be its largest vertex.
Tomorrow all students will go to the competition and the winner of the strongest sequence will be the winner. Zia has the sequence a_1, a_2, a_3, ..., a_n. He wants to take over the competition scoring system and remove sequences from it with more force than himself. However, Zia does not know the power of his own consistency, but really wants to win. Help him calculate the strength of his own sequence.
Input. The first line contains the number n~(1 \le n \le 10^5) of numbers in Zia's sequence. The next line contains integers a_i~(1 \le a_i \le 10^5) — the elements of the sequence.
Output. Print one number — the strength of the given sequence.
2
2 1003
1 2 315
1 10 2 3 12Today, programmers have gathered. Each programmer has a rating that reflects his strength. The rating is an integer between and 10^9. Your rating as a programmer is . From all the programmers gathered today, you want to choose two for your team. They must be selected in such a way that their total rating is maximized but does not exceed your rating, as you want to be the leader of this team.
Input. The first line contains two integers: n~(2 \le n \le 10^5) — the number of programmers, and m~(0 \le m \le 10^9) — your rating. The second line contains integers r_1, r_2, ... , r_n~(0 \le r_i \le 10^9) — the ratings of the programmers.
Output. Print a single integer — the maximum sum of the ratings of the selected programmers, or if it is impossible to find such two people.
5 8
5 3 4 6 587 19
8 4 25 1 20 5 12174 76
38 41 39 40-1Bessie the cow, always a fan of shiny objects, has taken up a hobby of mining diamonds in her spare time! She has collected diamonds of varying sizes, and she wants to arrange some of them in a display case in the barn.
Since Bessie wants the diamonds in the case to be relatively similar in size, she decides that she will not include two diamonds in the case if their sizes differ by more than (two diamonds can be displayed together in the case if their sizes differ by exactly ). Given , help Bessie determine the maximum number of diamonds she can display in the case.
Input. The first line contains n~(n \le 1000) and k~(0 \le k \le 10000). The next lines each contain an integer giving the size of one of the diamonds. All sizes will be positive and will not exceed .
Output. Print the maximum number of diamonds that Bessie can showcase.
5 3
1
6
4
3
14There is an array of integers of length , representing the heights of vertical lines. For each -th line, its endpoints are defined by the coordinates and (i, h_i).
Find two lines that, together with the -axis, form a container capable of holding the maximum amount of water.
Input. The first line contains the size n~(n \leq 10^5) of the array . The second line contains positive integers, the elements of the array , each of which does not exceed 10^9.
Output. Print the maximum volume of water that the container can hold.

9
1 8 6 2 5 4 8 3 749You are given a list of songs that played on Radio 106 FM. The list contains a total of songs. Find the length of the longest fragment of the list that consists of non-repeating songs.
Input. The first line contains the number of songs n~(1 \le n \le 10^5) in the list. The second line contains integers k_1, k_2, ..., k_n~(1 \le k_i \le 10^9), which are the identification numbers of the songs.
Output. Print the length of the longest fragment of the list that consists of unique songs.
8
1 2 1 3 2 7 4 254
1 1 2 12
Comments5