You are given an array $$$A$$$ of $$$N$$$ integers, where $$$A_i$$$ denotes the height of the $$$i^{th}$$$ person in the line. For each person, they wish to compare with those who stood before them — including themselves.
For each position $$$i$$$, compute the average height of all people in the prefix $$$[1, i]$$$ whose height is greater than or equal to $$$A_i$$$.
Your task is to compute this value for every position with modulo $$$10^9+7$$$.
The first line contains a single integer $$$N (1 \leq N \leq 2⋅10^5)$$$, the number of people in the line.
The second line contains $$$N$$$ space-separated integers $$$A_1, A_2, \ldots, A_N$$$ $$$(1 \leq A_i \leq 10^{15})$$$, the heights of the people.
Print $$$N$$$ space-separated integers, where the $$$i^{th}$$$ number is the average described above for position $$$i$$$.
5 5 1 8 2 5
5 3 8 5 6
In the sample testcase:
When, $$$i = 1 → \frac{5}{1} = 5$$$
When, $$$i = 2 → \frac{5+1}{2} = 3$$$
When, $$$i = 3 → \frac{8}{1} = 8$$$
When, $$$i = 4 → \frac{5+8+2}{3} = 5$$$
When, $$$i = 5 → \frac{5+8+5}{3} = 6$$$
You are given an integer $$$N$$$. You have to print a permutation such that for every $$$i$$$ $$$(1 \leq i \leq N)$$$, the value of $$$S_i = P_1 \oplus P_2 \oplus \ldots \oplus P_i$$$ is non zero.
If it is impossible to construct such a permutation, then print $$$-1$$$. Otherwise, print the lexicographically smallest permutation.
$$$\dagger$$$ A permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in any order. For example, $$$[2, 3, 1, 5, 4]$$$ is a permutation, but $$$[1, 2, 2]$$$ is not a permutation (the number $$$2$$$ appears twice in the array), and $$$[1, 3, 4]$$$ is also not a permutation (here $$$n = 3$$$, but the array contains the number $$$4$$$).
$$$\dagger$$$ A permutation $$$P$$$ is said to be lexicographically smaller than another permutation $$$Q$$$ if at the first position $$$i$$$ where $$$P_i \ne Q_i$$$, we have $$$P_i \lt Q_i$$$.
For example, among the following permutations of $$$[1, 2, 3]$$$: $$$[1, 2, 3] \lt [1, 3, 2] \lt [2, 1, 3]$$$. So, $$$[1, 2, 3]$$$ is the lexicographically smallest permutation among these.
The only line contains a single integer $$$N (1 \leq N \leq 10^6) $$$.
If it is impossible to construct such a permutation then print $$$-1$$$. Otherwise print the lexicographically smallest permutation.
4
1 2 4 3
Consider the permutation $$$P = [1, 2, 4, 3]$$$.
Now compute the prefix XOR values:
$$$S_1 = 1$$$
$$$S_2 = 1 \oplus 2 = 3$$$
$$$S_3 = 1 \oplus 2 \oplus 4 = 7$$$
$$$S_4 = 1 \oplus 2 \oplus 4 \oplus 3 = 4$$$
All values are non-zero. Also, this is the lexicographically smallest permutation satisfying the condition.
You are given $$$N$$$ distinct integers, ranging from $$$1$$$ to $$$N$$$.
Your task is to split the numbers into two non-empty groups such that the absolute difference between the sums of the two groups is a prime number.
Among all possible valid partitions, you must determine the maximum possible prime that can be obtained as the difference of sums.
The only line contains a single integer $$$N (2 \leq N \leq 10^6)$$$.
Print the maximum possible prime. If no such partition exists, output $$$-1$$$.
4
2
The numbers are $$$[1, 2, 3, 4]$$$.
We must divide them into two non-empty groups such that the absolute difference between the sums of the two groups is a prime number.
Some valid groupings are:
$$$$$$ \text{Group 1: } [4],\quad \text{Group 2: } [1, 2, 3] \Rightarrow \text{Sums are } 4 \text{ and } 6 \Rightarrow \text{Difference} = |4 - 6| = 2\ (\text{prime})\ $$$$$$ $$$$$$ \text{Group 1: } [1, 3],\quad \text{Group 2: } [2, 4] \Rightarrow \text{Sums are } 4 \text{ and } 6 \Rightarrow \text{Difference} = |4 - 6| = 2\ (\text{prime})\ $$$$$$
Among all valid partitions, the maximum prime difference we can achieve is $$$2$$$.
Let there be a function defined as: $$$f(X, Y) = \left\lfloor \frac{\mathrm{lcm}(X, Y)}{X \cdot Y} \right\rfloor $$$
You are given an array $$$A$$$ of $$$N$$$ integers. Your task is to compute the sum of $$$f(X, Y)$$$ over all possible unordered pairs$$$(X, Y)$$$ formed from the array.
$$$\dagger$$$ An unordered pairs refers to a selection of two distinct indices $$$i, j$$$ such that $$$i \ne j$$$, and the corresponding elements are $$$X = A[i],\ Y = A[j]$$$. Note that the values $$$X, Y$$$ may be equal, but the indices must be different. Each unique set of indices should be considered only once, regardless of the order.
The first line contains a single integer $$$N (2 \leq N \leq 2⋅10^5)$$$, the number of elements.
The second line contains $$$N$$$ space-separated integers $$$A_1, A_2, \ldots, A_N$$$ $$$(1 \leq A_i \leq 10^6)$$$.
Print the total sum of $$$f(X,Y)$$$ over all valid unordered triples.
6 15 4 8 3 2 6
6
Piku arranges $$$N$$$ ants evenly around a perfect circle, placing one ant at each position.
When he flips a magical switch, each ant instantly moves, choosing independently to step either to its nearest left or nearest right neighbor. All ants move simultaneously at the same time, and each movement is equally likely. Every ant will move only once .
He will flip the magical switch exactly once .
A collision occurs if two ants move toward each other from adjacent positions.
Piku wants to know what is the probability that at least two ants collide? Can you help him?
The first line contains a single integer $$$T (1 \leq T \leq 10^5)$$$, the number of test cases.
The only line of each test case contains a single integer $$$N (3 \leq N \leq 10^7)$$$, the number of ants.
Print the probability where at least two ants collide.
Let this probability be represented as an irreducible fraction $$$\frac{x}{y}$$$. You have to print $$$x⋅y^{−1}$$$ mod $$$10^9+7$$$, where $$$y^{−1}$$$ is the inverse element of $$$y$$$ modulo $$$10^9+7$$$ (such integer that $$$y⋅y^{−1}$$$ has remainder $$$1$$$ modulo $$$10^9+7$$$ ).
1 3
750000006
Munna has an array $$$A$$$ of $$$N$$$ integers, where $$$N$$$ is an odd number. He enjoys playing with these integers. Initially, his score is $$$0$$$.
In each turn, as long as the array contains at least three elements, he will perform the following operations:
Now, Munna wants to know the maximum possible value of the final remaining element, under the condition that his final score is odd, assuming he plays optimally.
The first line contains a single integer $$$N (1 \leq N \lt 2⋅10^5)$$$, the number of elements in the array.
The second line contains $$$N$$$ space-separated integers $$$A_1, A_2, \ldots, A_N$$$ $$$(1 \leq A_i \leq 10^9)$$$.
Note: It is guranteed that $$$N$$$ is always odd.
If it's impossible to obtain a final odd score output $$$-1$$$.
Otherwise, output the maximum possible value of the final remaining element in the array.
5 3 4 7 8 1
8
3 2 2 2
-1
For the first test case:
At this point, only one element remains, and the final score is $$$13$$$, which is odd. The remaining element is $$$8$$$, which is the maximum possible under this condition..
For the second test case:
It can be shown that it is impossible to obtain a final odd score, regardless of the operations performed.
You are given a binary string $$$S$$$ consisting only of characters $$$0$$$ and $$$1$$$.
Your task is to determine the minimum possible length of a binary string (composed of $$$0$$$ and $$$1$$$) that is not a subsequence of S.
†A string $$$Q$$$ is called a subsequence of string $$$P$$$ if $$$Q$$$ can be obtained from $$$P$$$ by deleting some (possibly, zero or all) characters without changing the order of the remaining characters. For example, subsequences of $$$1011101$$$ are $$$0$$$, $$$1$$$, $$$11111$$$, $$$0111$$$, but not $$$000$$$, nor $$$11100$$$.
A single line containing the binary string $$$S (1 \leq |S| \leq 10^6)$$$.
Output a single integer — the minimum length of a binary string that is not a subsequence of $$$S$$$.
0110
3
1000
2
In the first example, all binary strings of length $$$1$$$ and $$$2$$$ are present as subsequences. But $$$3$$$ length strings like $$$000$$$, $$$101$$$ etc. are not present. So the answer is $$$3$$$.
In the second example, all binary strings of length $$$1$$$ and $$$2$$$ are present as subsequences, except $$$01$$$, $$$11$$$. So the answer is 2.
You are given the following C program:
#include <stdio.h>
int main() {
int n, b;
scanf(n);
b = printf(n) with a newline;
printf(b) with a newline;
}
For a given integer $$$N$$$, determine what this program would output. The program contains several syntax errors and undefined behaviors - your task is to analyze how a real C compiler would process this code and what it would produce.
The only line contains a single integer $$$N (0 \leq N \leq 10^9)$$$.
Print the exact output of the program, line by line.
1
1 2