[Solution] Tournament Countdown solution codeforces

Tournament Countdown solution codeforces – There was a tournament consisting of 2𝑛2n contestants. The 11-st contestant competed with the 22-nd, the 33-rd competed with the 44-th, and so on. After that, the winner of the first match competed with the winner of second match, etc. The tournament ended when there was only one contestant left, who was declared the winner … Read more

[Solution] Build Permutation solution codeforces

Build Permutation solution codeforces – A 00-indexed array 𝑎a of size 𝑛n is called good if for all valid indices 𝑖i (0≤𝑖≤𝑛−10≤i≤n−1), 𝑎𝑖+𝑖ai+i is a perfect square††. [Solution] Build Permutation solution codeforces Given an integer 𝑛n. Find a permutation‡‡ 𝑝p of [0,1,2,…,𝑛−1][0,1,2,…,n−1] that is good or determine that no such permutation exists. †† An integer 𝑥x is said to be a perfect square if there exists an integer 𝑦y such that 𝑥=𝑦2x=y2. ‡‡ An array 𝑏b is a permutation of … Read more

[Solution] Optimal Reduction solution codeforces

Optimal Reduction solution codeforces – Consider an array 𝑎a of 𝑛n positive integers. You may perform the following operation: select two indices 𝑙l and 𝑟r (1≤𝑙≤𝑟≤𝑛1≤l≤r≤n), then decrease all elements 𝑎𝑙,𝑎𝑙+1,…,𝑎𝑟al,al+1,…,ar by 11. [Solution] Optimal Reduction solution codeforces Let’s call 𝑓(𝑎)f(a) the minimum number of operations needed to change array 𝑎a into an array of 𝑛n zeros. Determine if for all permutations†† 𝑏b of 𝑎a, 𝑓(𝑎)≤𝑓(𝑏)f(a)≤f(b) is true. †† An array 𝑏b is a permutation of an array 𝑎a if 𝑏b consists of … Read more

[Solution] Traveling Salesman Problem solution codeforces

Traveling Salesman Problem solution codeforces – You are living on an infinite plane with the Cartesian coordinate system on it. In one move you can go to any of the four adjacent points (left, right, up, down). [Solution] Traveling Salesman Problem solution codeforces More formally, if you are standing at the point (𝑥,𝑦)(x,y), you can: go … Read more

[Solution] Minimum Replacements to Sort the Array solution leetcode

Minimum Replacements to Sort the Array solution leetcode – You are given a 0-indexed integer array nums. In one operation you can replace any element of the array with any two elements that sum to it. For example, consider nums = [5,6,7]. In one operation, we can replace nums[1] with 2 and 4 and convert nums to [5,2,4,7]. [Solution] Minimum Replacements to Sort the Array solution leetcode Return the minimum number of … Read more

[Solution] Task Scheduler II solution leetcode

Task Scheduler II solution leetcode – You are given a 0-indexed array of positive integers tasks, representing tasks that need to be completed in order, where tasks[i] represents the type of the ith task. [Solution] Task Scheduler II solution leetcode You are also given a positive integer space, which represents the minimum number of days that must pass after the completion of a task before another task of the same type … Read more

[Solution] Count Number of Bad Pairs solution leetcode

Count Number of Bad Pairs solution leetcode – You are given a 0-indexed integer array nums. A pair of indices (i, j) is a bad pair if i < j and j – i != nums[j] – nums[i]. [Solution] Count Number of Bad Pairs solution leetcode Return the total number of bad pairs in nums.   Example 1: Input: nums = [4,1,3,3] Output: 5 Explanation: The pair (0, … Read more

[Solution] Merge Similar Items solution leetcode

Merge Similar Items solution leetcode – You are given two 2D integer arrays, items1 and items2, representing two sets of items. Each array items has the following properties: items[i] = [valuei, weighti] where valuei represents the value and weighti represents the weight of the ith item. The value of each item in items is unique. [Solution] Merge Similar Items solution leetcode Return a 2D integer array ret where ret[i] = [valuei, weighti], with weighti being the sum of weights of all items … Read more

[Solution] Beautiful Array solution codechef

Beautiful Array solution codechef – You’re given an array AA of NN integers. You need to find the minimum cost of creating another array BB of NN integers with the following properties [Solution] Beautiful Array solution codechef B_i \ge 0Bi​≥0 for each 1 \leq i \leq N1≤i≤N The GCD of adjacent elements of BB is equal to 11, i.e, \gcd(B_i, B_{i+1}) = 1gcd(Bi​,Bi+1​)=1 for each 1 \leq i \lt N1≤i<N … Read more

[Solution] Air Blimp solution codechef

Air Blimp solution codechef – There are NN cities in a row. The ii-th city from the left has a sadness of A_iAi​. In an attempt to reduce the sadness of the cities, you can send blimps from the left of city 11 that move rightwards (i.e, a blimp crosses cities 1, 2, \ldots1,2,… in order) [Solution] Air Blimp solution codechef You are given … Read more