title-img


String Reduction

Given a string consisting of the letters a, b and c, we can perform the following operation: Take any two adjacent distinct characters and replace them with the third character. Find the shortest string obtainable through applying this operation repeatedly. For example, given the string aba we can reduce it to a 1 character string by replacing ab with c and ca with b: aba -> ca -> b. Function Description Complete the stringReduction function in the editor below. It must return an in

View Solution →

Far Vertices

You are given a tree that has N vertices and N-1 edges. Your task is to mark as small number of vertices as possible, such that, the maximum distance between two unmarked vertices is less than or equal to K. Output this value. Distance between two vertices i and j is defined as the minimum number of edges you have to pass in order to reach vertex i from vertex j. Input Format The first line of input contains two integers N and K. The next N-1 lines contain two integers (ui,vi) each, where 1

View Solution →

Superman Celebrates Diwali

Superman has been invited to India to celebrate Diwali. Unfortunately, on his arrival he learns that he has been invited mainly to help rescue people from a fire accident that has happened in a posh residential locale of New Delhi, where rescue is proving to be especially difficult. As he reaches the place of the fire, before him there are N buildings, each of the same height H, which are on fire. Since it is Diwali, some floors of the buildings are empty as the occupants have gone elsewhere for

View Solution →

Hexagonal Grid

You are given a hexagonal grid consisting of two rows, each row consisting of n cells. The cells of the first row are labelled a1,a2,...,an and the cells of the second row are labelled b1,b2,...,bn. For example, for n=6: Grid Shape (Note that the b is connected with a(i+1).) Your task is to tile this grid with 2*1 tiles that look like the following: Orientations As you can see above, there are three possible orientations in which a tile can be placed. Your goal is to tile th

View Solution →

Queens on Board

Queens on Board You have an N * M chessboard on which some squares are blocked out. In how many ways can you place one or more queens on the board, such that, no two queens attack each other? Two queens attack each other, if one can reach the other by moving horizontally, vertically, or diagonally without passing over any blocked square. At most one queen can be placed on a square. A queen cannot be placed on a blocked square. Input Format The first line contains the number of test case

View Solution →