# Check if Point Is Reachable solution leetcode

Check if Point Is Reachable solution leetcode – There exists an infinitely large grid. You are currently at point (1, 1), and you need to reach the point (targetX, targetY) using a finite number of steps.

## Check if Point Is Reachable solution leetcode

In one step, you can move from point (x, y) to any one of the following points:

• (x, y - x)
• (x - y, y)
• (2 * x, y)
• (x, 2 * y)

Given two integers targetX and targetY representing the X-coordinate and Y-coordinate of your final position, return true if you can reach the point from (1, 1) using some number of steps, and false otherwise.

## Check if Point Is Reachable solution leetcode

Input: targetX = 6, targetY = 9
Output: false
Explanation: It is impossible to reach (6,9) from (1,1) using any sequence of moves, so false is returned.


Example 2:

Input: targetX = 4, targetY = 7
Output: true
Explanation: You can follow the path (1,1) -> (1,2) -> (1,4) -> (1,8) -> (1,7) -> (2,7) -> (4,7).


Constraints:

• 1 <= targetX, targetY <= 109

# Maximum Subsequence Score solution leetcode

Maximum Subsequence Score solution leetcode – You are given two 0-indexed integer arrays nums1 and nums2 of equal length n and a positive integer k. You must choose a subsequence of indices from nums1 of length k.

## Maximum Subsequence Score solution leetcode

For chosen indices i0i1, …, ik - 1, your score is defined as:

• The sum of the selected elements from nums1 multiplied with the minimum of the selected elements from nums2.
• It can defined simply as: (nums1[i0] + nums1[i1] +...+ nums1[ik - 1]) * min(nums2[i0] , nums2[i1], ... ,nums2[ik - 1]).

Return the maximum possible score.

subsequence of indices of an array is a set that can be derived from the set {0, 1, ..., n-1} by deleting some or no elements.

## Maximum Subsequence Score solution leetcode

Input: nums1 = [1,3,3,2], nums2 = [2,1,3,4], k = 3
Output: 12
Explanation:
The four possible subsequence scores are:
- We choose the indices 0, 1, and 2 with score = (1+3+3) * min(2,1,3) = 7.
- We choose the indices 0, 1, and 3 with score = (1+3+2) * min(2,1,4) = 6.
- We choose the indices 0, 2, and 3 with score = (1+3+2) * min(2,3,4) = 12.
- We choose the indices 1, 2, and 3 with score = (3+3+2) * min(1,3,4) = 8.
Therefore, we return the max score, which is 12.


## Maximum Subsequence Score solution leetcode

Input: nums1 = [4,2,3,1,1], nums2 = [7,5,10,9,6], k = 1
Output: 30
Explanation:
Choosing index 2 is optimal: nums1[2] * nums2[2] = 3 * 10 = 30 is the maximum possible score.


Constraints:

• n == nums1.length == nums2.length
• 1 <= n <= 105
• 0 <= nums1[i], nums2[j] <= 105
• 1 <= k <= n

# Minimum Operations to Make Array Equal II solution leetcode

Minimum Operations to Make Array Equal II solution leetcode – You are given two integer arrays nums1 and nums2 of equal length n and an integer k.

## Minimum Operations to Make Array Equal II solution leetcode

You can perform the following operation on nums1:

• Choose two indexes i and j and increment nums1[i] by k and decrement nums1[j] by k. In other words, nums1[i] = nums1[i] + k and nums1[j] = nums1[j] - k.

nums1 is said to be equal to nums2 if for all indices i such that 0 <= i < nnums1[i] == nums2[i].

Return the minimum number of operations required to make nums1 equal to nums2. If it is impossible to make them equal, return -1.

## Minimum Operations to Make Array Equal II solution leetcode

Input: nums1 = [4,3,1,4], nums2 = [1,3,7,1], k = 3
Output: 2
Explanation: In 2 operations, we can transform nums1 to nums2.
1st operation: i = 2, j = 0. After applying the operation, nums1 = [1,3,4,4].
2nd operation: i = 2, j = 3. After applying the operation, nums1 = [1,3,7,1].
One can prove that it is impossible to make arrays equal in fewer operations.

Example 2:

Input: nums1 = [3,8,5,2], nums2 = [2,4,1,6], k = 1
Output: -1
Explanation: It can be proved that it is impossible to make the two arrays equal.


## Minimum Operations to Make Array Equal II solution leetcode

• n == nums1.length == nums2.length
• 2 <= n <= 105
• 0 <= nums1[i], nums2[j] <= 109
• 0 <= k <= 105

# Minimum Common Value solution leetcode

Minimum Common Value solution leetcode – Given two integer arrays nums1 and nums2, sorted in non-decreasing order, return the minimum integer common to both arrays. If there is no common integer amongst nums1 and nums2, return -1.

## Minimum Common Value solution leetcode

Note that an integer is said to be common to nums1 and nums2 if both arrays have at least one occurrence of that integer.

Example 1:

Input: nums1 = [1,2,3], nums2 = [2,4]
Output: 2
Explanation: The smallest element common to both arrays is 2, so we return 2.


## Minimum Common Value solution leetcode

Input: nums1 = [1,2,3,6], nums2 = [2,3,4,5]
Output: 2
Explanation: There are two common elements in the array 2 and 3 out of which 2 is the smallest, so 2 is returned.


## Minimum Common Value solution leetcode

• 1 <= nums1.length, nums2.length <= 105
• 1 <= nums1[i], nums2[j] <= 109
• Both nums1 and nums2 are sorted in non-decreasing order.

# 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 .