Author Archives: admin

Finish the quote: “Never let the fear of striking out keep you from…”?

Finish the quote: “Never let the fear of striking out keep you from reaching for your dreams. No matter how many times you try and fail, never give up. You will eventually find success if you keep pushing forward and never give up.

Finish the quote: “Never let the fear of striking out keep you from…”?

  1. Playing the game
  2. Winning the trophy
  3. Losing your cool
  4. Skipping
The correct answer is Playing the game.

Finish the quote: “Never let the fear of striking out keep you from…”?

Yankees slugger George Herman “Babe” Ruth certainly had no fear of striking out: The “Sultan of Swat” hit a total of 714 home runs during his career, including 60 home runs in the 1927 season alone (not to mention 59 homers in 1921 and 54 in 1920). He was one of the first five players inducted into the Baseball Hall of Fame.

Also read: Shakespeare’s Polonius advised, “This above all: to thine own self” what?

Shakespeare’s Polonius advised, “This above all: to thine own self” what?

Shakespeare’s Polonius advised, “This above all: to thine own self” what? – And it must follow, as the night the day, Thou canst not then be false to any man.

Shakespeare’s Polonius advised, “This above all: to thine own self” what?

  1. Give the spoils
  2. Be true
  3. Return
  4. Send a letter
The correct answer is Be true.

Shakespeare’s Polonius advised, “This above all: to thine own self” what?

William Shakespeare’s tragedy “Hamlet” was first printed in 1603 and performed at the Globe Theatre in London. This line comes from a scene in which the king’s adviser gives his son Laertes some well-meaning but long-winded council before his trip to Paris. In the end, both characters die at the hands of Hamlet, who decides to be true not to himself but to the vengeful demands of his father’s ghost.

Maximum Xor Sum solution codechef

Maximum Xor Sum solution codechef – You are given arrays  and  with  non-negative integers each.

Maximum Xor Sum solution codechef

An array  of length  is called good, if:

  • All elements of the array  are non-negative;
  • �1  �2    ��=�� for all (1≤�≤�);
  • �� & �(�+1) &  & ��=�� for all (1≤�≤�).

Find the maximum bitwise XOR of all elements over all good arrays .
More formally, find the maximum value of �1⊕�2⊕…��, over all good arrays .
It is guaranteed that at least one such array  exists.

Note that ∣,&, and  denote the bitwise orand, and xor operations respectively.

Input Format

  • The first line of input will contain a single integer , denoting the number of test cases.
  • Each test case consists of multiple lines of input.
    • The first line of each test case contains one integer  — the size of the array.
    • The next line contains  space-separated integers describing the elements of the array .
    • The next line contains  space-separated integers describing the elements of the array .

Maximum Xor Sum solution codechef

For each test case, output on a new line, the maximum bitwise XOR of all elements over all good arrays .

Constraints

  • 1≤�≤105
  • 1≤�≤105
  • 0≤��<230
  • 0≤��<230
  • It is guaranteed that at least one such array  exists.
  • Sum of  over all test cases is less than 3⋅105.

Sample 1:

Input

Output

2
3
0 3 3
0 2 2
2
2 3
0 1
1
3

Maximum Xor Sum solution codechef

Test case 1: An optimal good array is �=[0,3,2].

  • For �=1�1=�1=0 and �1=�1 & �2 & �3=0.
  • For �=2�2=�1  �2=3 and �2=�2 & �3=2.
  • For �=3�3=�1  �2  �3=3 and �3=�3=2.

The XOR of all elements of  is 0⊕3⊕2=1. It can be proven that this is the maximum XOR value for any .

Test case 2: An optimal good array is �=[2,1].

  • For �=1�1=�1=2 and �1=�1 & �2=0.
  • For �=2�2=�1  �2=3 and �2=�2=1.

The XOR of all elements of  is 2⊕1=3. It can be proven that this is the maximum XOR value for any .

Minimal Inversions solution codechef

Minimal Inversions solution codechef – Initially, Chef had an array  of length . Chef performs the following operation on  at most once:

  • Select  and  such that 1≤�≤�≤� and set ��:=��+1 for all �≤�≤�.

Minimal Inversions solution codechef

Determine the maximum number of inversions Chef can decrease from the array  by applying the operation at most once.
More formally, let the final array obtained after applying the operation at most once be . You need to determine the maximum value of ���(�)−���(�) (where ���(�) denotes the number of inversions in array ).

Note: The number of inversions in an array  is the number of pairs (�,�) such that 1≤�<�≤� and ��>��.

Input Format

  • The first line contains a single integer  — the number of test cases. Then the test cases follow.
  • The first line of each test case contains an integer  — the size of the array .
  • The second line of each test case contains  space-separated integers �1,�2,…,�� denoting the array .

Minimal Inversions solution codechef

For each test case, output the maximum value of ���(�)−���(�) which can be obtained after applying at most one operation.

Constraints

  • 1≤�≤105
  • 1≤�≤105
  • 1≤��≤�
  • Sum of  over all test cases does not exceed 2⋅105.

Sample 1:

Input

Output

3
5
4 2 3 1 5
6
1 2 3 4 5 6
4
2 1 1 1
2
0
3

Minimal Inversions solution codechef

Test case 1: The initial array  is [4,2,3,1,5] which has 5 inversions. We can perform operation on �=3,�=4. The resultant array will be [4,2,4,2,5] which has 3 inversions. Therefore we reduce the number of inversion by 2 which is the maximum decrement possible.

Test case 2: The initial array  is [1,2,3,4,5,6] which has 0 inversions. In this case, we do not need to apply any operation and the final array  will be same as the initial array . Therefore the maximum possible decrement in inversions is 0.

Test case 3: The initial array  is [2,1,1,1] which has 3 inversions. We can perform operation on �=2,�=4. The resultant array will be [2,2,2,2] which has 0 inversions. Therefore we reduce the number of inversion by 3 which is the maximum decrement possible.

Distinct Values solution codechef

Distinct Values solution codechef – The beauty value of an array is defined as the difference between the largest and second largest elements of the array. Note that the largest and second largest elements can have the same value in case of duplicates.

Distinct Values solution codechef

For example, beauty value of [2,5,3,1]=5−3=2 and beauty value of [7,6,7]=7−7=0

You are given an array  of length . Your task is to find the total number of distinct beauty values among all subarrays of  having length greater than 1.

Note that, a subarray is obtained by deleting some (possibly zero) elements from the beginning and some (possibly zero) elements from the end of the array.

Input Format

  • The first line of input will contain a single integer , denoting the number of test cases.
  • Each test case consists of two lines of input.
    • The first line of each test case contains a single integer  — the size of the array.
    • The second line contains  space-separated numbers – �1,�2,…,��, the elements of the array.

Output Format

For each test case, output a single line, the total number of distinct beauty among all subarrays of  having length greater than 1.

Distinct Values solution codechef

  • 1≤�≤104
  • 2≤�≤2⋅105
  • 1≤��≤109
  • Sum of  over all test cases does not exceed 2⋅105.

Sample 1:

Input

Output

4
2
1 1
3
4 2 1
4
8 1 7 2
5
6 9 4 2 1
1
2
4
4

 

 

Distinct Values solution codechef

Test case 1: The only subarray is [1,1] whose beauty is 0. Thus, there is only 1 distinct value of beauty.

Test case 2: The subarrays are [4,2],[2,1], and [4,2,1] having beauty 2,1, and 2 respectively. There are 2 distinct values of beauty.

Test case 3: The unique values of beauty are 7,1,6, and 5.

Test case 4: The unique values of beauty are 3,5,2, and 1.

Lcm hates Gcd solution codechef

Lcm hates Gcd solution codechef – Chef has two integers  and .
Chef wants to find the minimum value of lcm(�,�)−gcd(�,�) where  is any positive integer.

Lcm hates Gcd solution codechef

Help him determine this value.

Note: gcd(�,�) denotes the greatest common divisor of  and  and lcm(�,�) denotes the least common multiple of  and .

Input Format

  • The first line contains a single integer  — the number of test cases. Then the test cases follow.
  • The first and only line of each test case contains two space-separated integers  and  — the integers mentioned in the statement.

Output Format

For each test case, output the minimum value of lcm(�,�)−gcd(�,�).

Lcm hates Gcd solution codechef

  • 1≤�≤105
  • 1≤�,�≤109

Sample 1:

Input

Output

3
12 15
5 50
9 11
9
0
8

Lcm hates Gcd solution codechef Explanation:

Test case 1: For �=6lcm(12,6)−gcd(15,6)=12−3=9 which is the minimum value required.

Test case 2: For �=50lcm(5,50)−gcd(50,50)=50−50=0 which is the minimum value required.

Test case 3: For �=1lcm(9,1)−gcd(11,1)=9−1=8 which is the minimum value required.

Chef And Babla solution codechef

Chef And Babla solution codechef – Chef gives an array  with  elements to Babla. Babla’s task is to find the maximum non-negative integer  such that:

  • No element in the array belongs to the range [−�,�]. In other words, for all (1≤�≤�), either ��<−� or ��>�.

Chef And Babla solution codechef

Help Babla to find the maximum non-negative integer  for which the given condition is satisfied or determine if no such  exists.

Input Format

  • The first line of input will contain a single integer , denoting the number of test cases.
  • Each test case consists of multiple lines of input.
    • The first line of each test case contains an integer  — the number of elements in the array.
    • The second line of each test case contains  space-separated integers �1,�2,…,�� representing the array .

Output Format

For each test case, output on a new line, the maximum non-negative integer , satisfying the above condition.
If no such  exists, output −1 instead.

Chef And Babla solution codechef

  • 1≤�≤105
  • 1≤�≤105
  • −109≤��≤109
  • Sum of  over all test cases does not exceed 2⋅105.

Sample 1:

Input

Output

3
5
8 4 2 5 2
6
7 9 -10 8 12 17
4
0 -3 -1 -10
1
6
-1

Chef And Babla solution codechef Explanation:

Test case 1: The maximum value of  such that no element of the array belongs to the range [−�,�] is 1. All elements of the array are strictly greater than 1.

Test case 2: The maximum value of  such that no element of the array belongs to the range [−�,�] is 6. All positive elements of the array are strictly greater than 6 and negative elements are strictly less than −6.

Test case 3: It is not possible to choose an element  that satisfies the given condition.

Odd Even Binary String solution codechef

Odd Even Binary String solution codechef – Chef had an array  of length  such that 1≤��≤� for all 1≤�≤�.

Odd Even Binary String solution codechef

Chef constructed another binary array  of length  in the following manner:

  • ��=1 if the frequency of element  in  is odd.
  • ��=0 if the frequency of element  in  is even.

Such an array  is called the parity encoding array of .

For example, if �=[1,1,2,3], then �=[0,1,1,0].

Unfortunately, Chef completely forgot the array  and vaguely remembers the parity encoding array . He is now wondering whether there exists any valid array  for which the parity encoding array is . Can you help Chef?

Odd Even Binary String solution codechef

  • The first line contains a single integer  — the number of test cases. Then the test cases follow.
  • The first line of each test case contains an integer  — the size of the arrays  and .
  • The second line of each test case contains  space-separated integers �1,�2,…,�� denoting the parity encoding array .

Output Format

For each test case, output YES if there exists any valid array  for which the parity encoding array is . Otherwise, output NO.

You may print each character of YES and NO in uppercase or lowercase (for example, yesyEsYes will be considered identical).

Odd Even Binary String solution codechef

  • 1≤�≤105
  • 1≤�≤105
  • ��∈{0,1}
  • Sum of  over all test cases does not exceed 2⋅105.

Sample 1:

Input

Output

3
4
0 1 0 1
5
1 1 1 1 0
6
1 1 1 1 1 1
YES
NO
YES

Odd Even Binary String solution codechef

Test case 1: �=[2,4,3,3] is a valid array for the given array .

Test case 2: It can be proven that there does not exist any array  for the given parity encoding array .

Test case 3: �=[1,2,3,4,5,6] is a valid array for the given array .

Mana Points solution codechef

Mana Points solution codechef – Chef is playing a mobile game. In the game, Chef’s character Chefario can perform special attacks. However, one special attack costs  mana points to Chefario.

Mana Points solution codechef

If Chefario currently has  mana points, determine the maximum number of special attacks he can perform.

Input Format

  • The first line contains a single integer  — the number of test cases. Then the test cases follow.
  • The first and only line of each test case contains two space-separated integers  and  — the cost of one special attack and the number of mana points Chefario has initially.

Output Format

For each test case, output the maximum number of special attacks Chefario can perform.

Mana Points solution codechef

  • 1≤�≤105
  • 1≤�≤100
  • 1≤�≤1000

Sample 1:

Input

Output

3
10 30
6 41
50 2
3
6
0

Mana Points solution codechef Explanation:

Test case 1: Chefario can perform a maximum of 3 special attacks which will cost him 30 mana points.

Test case 2: Chefario can perform a maximum of 6 special attacks which will cost him 36 mana points. Note that Chefario can not perform 7 special attacks as these will cost him 42 mana points while he has only 41 mana points.

Test case 3: Chefario will not be able to perform any special attacks in this case.

Wonderful Jump solution codeforces

Wonderful Jump solution codeforces – You are given an array of positive integers 𝑎1,𝑎2,,𝑎𝑛a1,a2,…,an of length 𝑛n.

Wonderful Jump solution codeforces

In one operation you can jump from index 𝑖i to index 𝑗j (1𝑖𝑗𝑛1≤i≤j≤n) by paying min(𝑎𝑖,𝑎𝑖+1,,𝑎𝑗)(𝑗𝑖)2min(ai,ai+1,…,aj)⋅(j−i)2 eris.

For all 𝑘k from 11 to 𝑛n, find the minimum number of eris needed to get from index 11 to index 𝑘k.

Input

The first line contains a single integer 𝑛n (2𝑛41052≤n≤4⋅105).

The second line contains 𝑛n integers 𝑎1,𝑎2,𝑎𝑛a1,a2,…an (1𝑎𝑖𝑛1≤ai≤n).

Output

Output 𝑛n integers — the 𝑘k-th integer is the minimum number of eris needed to reach index 𝑘k if you start from index 11.

Wonderful Jump solution codeforces

input

Copy
3
2 1 3
output

Copy
0 1 2 

Wonderful Jump solution codeforces

Copy
6
1 4 1 6 3 2
output

Copy
0 1 2 3 6 8 
input

Copy
2
1 2
output

Copy
0 1 
input

Copy
4
1 4 4 4
output

Copy
0 1 4 8 

Wonderful Jump solution codeforces

In the first example:

  • From 11 to 11: the cost is 00,
  • From 11 to 22121→2 — the cost is min(2,1)(21)2=1min(2,1)⋅(2−1)2=1,
  • From 11 to 331231→2→3 — the cost is min(2,1)(21)2+min(1,3)(32)2=1+1=2min(2,1)⋅(2−1)2+min(1,3)⋅(3−2)2=1+1=2.

In the fourth example from 11 to 441341→3→4 — the cost is min(1,4,4)(31)2+min(4,4)(43)2=4+4=8min(1,4,4)⋅(3−1)2+min(4,4)⋅(4−3)2=4+4=8.

Also read: Partial Sorting solution codeforces