Palindromic Indices solution codeforces

Palindromic Indices solution codeforces You are given a palindromic string ss of length nn. You have to count the number of indices ii (1≤i≤n)(1≤i≤n) such that the string after removing sisi from ss still remains a palindrome. For example, consider ss = “aba” If we rem ove s1s1 from ss, the string becomes “ba” which is not a palindrome. If we remove s2s2 from ss, the string becomes “aa” which is a palindrome. If … Read more

AND Sorting solution codeforces

AND Sorting solution codeforces You are given a permutation pp of integers from 00 to n−1n−1 (each of them occurs exactly once). Initially, the permutation is not sorted (that is, pi>pi+1pi>pi+1 for at least one 1≤i≤n−11≤i≤n−1). The permutation is called XX-sortable for some non-negative integer XX if it is possible to sort the permutation by performing the operation below some finite number of times: Choose two indices ii and jj (1≤i<j≤n)(1≤i<j≤n) such that pi&pj=Xpi&pj=X. Swap pipi and pjpj. … Read more

LIS or Reverse LIS? solution codeforces

LIS or Reverse LIS? solution codeforces You are given an array aa of nn positive integers. Let LIS(a)LIS(a) denote the length of longest strictly increasing subsequence of aa. For example, LIS([2,1–,1,3–])LIS([2,1_,1,3_]) = 22. LIS([3–,5–,10–––,20–––])LIS([3_,5_,10_,20_]) = 44. LIS([3,1–,2–,4–])LIS([3,1_,2_,4_]) = 33. We define array a′a′ as the array obtained after reversing the array aa i.e. a′=[an,an−1,…,a1]a′=[an,an−1,…,a1]. The beauty of array aa is defined as min(LIS(a),LIS(a′))min(LIS(a),LIS(a′)). Your task is to determine the maximum possible beauty of the array aa if you can rearrange … Read more

Circular Spanning Tree solution codeforces

Circular Spanning Tree solution codeforces There are nn nodes arranged in a circle numbered from 11 to nn in the clockwise order. You are also given a binary string ss of length nn. Your task is to construct a tree on the given nn nodes satisfying the two conditions below or report that there such tree does not exist: For each node ii (1≤i≤n)(1≤i≤n), the degree of node … Read more

Unordered Swaps solution codeforces

Unordered Swaps solution codeforces Alice had a permutation pp of numbers from 11 to nn. Alice can swap a pair (x,y)(x,y) which means swapping elements at positions xx and yy in pp (i.e. swap pxpx and pypy). Alice recently learned her first sorting algorithm, so she decided to sort her permutation in the minimum number of swaps possible. She wrote down all the swaps in the order in which she performed them to … Read more

MCMF? solution codeforces

MCMF? solution codeforces You are given two integer arrays aa and bb (bi≠0bi≠0 and |bi|≤109|bi|≤109). Array aa is sorted in non-decreasing order. The cost of a subarray a[l:r]a[l:r] is defined as follows: If ∑j=lrbj≠0∑j=lrbj≠0, then the cost is not defined. Otherwise: Construct a bipartite flow graph with r−l+1r−l+1 vertices, labeled from ll to rr, with all vertices having bi<0bi<0 on the left and those with bi>0bi>0 on right. For each i,ji,j such that l≤i,j≤rl≤i,j≤r, bi<0bi<0 and bj>0bj>0, draw an edge from ii to jj with infinite capacity … Read more

Sum of Total Strength of Wizards solution leetcode

Sum of Total Strength of Wizards solution leetcode As the ruler of a kingdom, you have an army of wizards at your command. You are given a 0-indexed integer array strength, where strength[i] denotes the strength of the ith wizard. For a contiguous group of wizards (i.e. the wizards’ strengths form a subarray of strength), the total strength is defined as the product of the following two values: The strength of … Read more

Minimum Lines to Represent a Line Chart solution leetcode

Minimum Lines to Represent a Line Chart solution leetcode You are given a 2D integer array stockPrices where stockPrices[i] = [dayi, pricei] indicates the price of the stock on day dayi is pricei. A line chart is created from the array by plotting the points on an XY plane with the X-axis representing the day and the Y-axis representing the price and connecting adjacent … Read more