Two Out of Three Solution codeforces

B. Two Out of Three time limit per test 3 seconds memory limit per test 512 megabytes input standard input output standard output You are given an array a1,a2,…,an�1,�2,…,��. You need to find an array b1,b2,…,bn�1,�2,…,�� consisting of numbers 11, 22, 33 such that exactly two out of the following three conditions are satisfied: There exist indices 1≤i,j≤n1≤�,�≤� such that ai=aj��=��, bi=1��=1, bj=2��=2. There exist indices 1≤i,j≤n1≤�,�≤� such that ai=aj��=��, bi=1��=1, bj=3��=3. There exist … Read more

[SOLUTION] Anonymous Informant solution codeforces

Anonymous Informant time limit per test 3 seconds memory limit per test 512 megabytes input standard input output standard output You are given an array b1,b2,…,bn�1,�2,…,��. An anonymous informant has told you that the array b� was obtained as follows: initially, there existed an array a1,a2,…,an�1,�2,…,��, after which the following two-component operation was performed k� times: A fixed point†† x� of the array a� was … Read more

Maximum Balanced Subsequence Sum solution leetcode

100112. Maximum Balanced Subsequence Sum User Accepted:0 User Tried:0 Total Accepted:0 Total Submissions:0 Difficulty:Hard You are given a 0-indexed integer array nums. A subsequence of nums having length k and consisting of indices i0 < i1 < … < ik-1 is balanced if the following holds: nums[ij] – nums[ij-1] >= ij – ij-1, for every j in the range [1, k – 1]. A subsequence of nums having length 1 is considered balanced. Return an integer denoting the maximum possible sum of elements in a balanced subsequence of nums. A subsequence of an … Read more

100118. Maximum Score After Applying Operations on a Tree Solution leetcode

100118. Maximum Score After Applying Operations on a Tree User Accepted:0 User Tried:0 Total Accepted:0 Total Submissions:0 Difficulty:Medium There is an undirected tree with n nodes labeled from 0 to n – 1, and rooted at node 0. You are given a 2D integer array edges of length n – 1, where edges[i] = [ai, bi] indicates that there is an edge between nodes ai and bi in the tree. You … Read more

100115. Find Champion I solution leetcode

100115. Find Champion I solution leetcode User Accepted:0 User Tried:0 Total Accepted:0 Total Submissions:0 Difficulty:Easy There are n teams numbered from 0 to n – 1 in a tournament. Given a 0-indexed 2D boolean matrix grid of size n * n. For all i, j that 0 <= i, j <= n – 1 and i != j team i is stronger than team j if grid[i][j] == 1, otherwise, team j is stronger than team i. Team a will be the champion of the tournament if there … Read more

[Solution] Find Champion II Solution Leetcode

Find Champion II Solution Leetcode User Accepted:0 User Tried:0 Total Accepted:0 Total Submissions:0 Difficulty:Medium There are n teams numbered from 0 to n – 1 in a tournament; each team is also a node in a DAG. You are given the integer n and a 0-indexed 2D integer array edges of length m representing the DAG, where edges[i] = [ui, vi] indicates that there is a directed edge from team ui to team vi in the graph. … Read more

[SOLUTION] Two Characters, Two Colors Solution Codeforces

G. Two Characters, Two Colors time limit per test 4 seconds memory limit per test 512 megabytes input standard input output standard output You are given a string consisting of characters 0 and/or 1. You have to paint every character of this string into one of two colors, red or blue. If you paint the i�-th character red, you … Read more

[SOLUTION] Fancy Arrays SOLUTION CODEFORCES

F. Fancy Arrays time limit per test 4 seconds memory limit per test 512 megabytes input standard input output standard output Let’s call an array a� of n� non-negative integers fancy if the following conditions hold: at least one from the numbers x�, x+1�+1, …, x+k−1�+�−1 appears in the array; consecutive elements of the array differ by at most k� (i.e. |ai−ai−1|≤k|��−��−1|≤� for each i∈[2,n]�∈[2,�]). You are given n�, x� and k�. Your task … Read more

[solution] Infinite Card Game Solution Codeforces

E. Infinite Card Game time limit per test 3 seconds memory limit per test 512 megabytes input standard input output standard output Monocarp and Bicarp are playing a card game. Each card has two parameters: an attack value and a defence value. A card s� beats another card t� if the attack of s� is strictly greater than the defence of t�. … Read more

[SOLUTION] XOR Construction Solution Codeforces

# Function to construct array b based on a def construct_array(n, a): b = [0] * n b[0] = 0 for i in range(1, n – 1): b[i] = a[i – 1] ^ b[i – 1] all_elements = set(range(n)) used_elements = set(b) missing_element = list(all_elements – used_elements) b[n – 1] = a[n – 2] ^ … Read more