Posts

Showing posts with the label DSA

Female Coding Pioneer

Ada Lovelace, the first programmer and a visionary mathematician, was chiefly known for her work on Charles Babbage's proposed mechanical general-purpose computer, the Analytical Engine. She was fond of sequences. During her studies she came across an infinite sequence that was generated using the following algorithm, starting with an empty sequence. Starting from step number 1, in step number  i,  the integer  i  is appended to the sequence exactly  k  times where  k  is equal to the number of set bits in  i. Given an array  query  of  n  integers,   for each query, find the value at the given index of the array assuming indexing starts at 0 .  Report an array of  n  integers where the  x th   integer represents the answer to the  x th  query. Example Given, n = 2 and query = [4, 10], the sequence is generated as follows. Step Number Bina...

HackTree

Image
 You are given a tree with n nodes. Each node has a value assigned with it. The cost of a path is defined as the summation of all the values assigned to nodes that belong to the path. The root of the tree is node number 1. Cost of path example The cost of the path 6 -> 5 -> 3 -> 1 in the above tree is 42 + 31 + 20 + 10 = 103. A Vertical Path in a tree is the path that is going up towards the root of the tree. It is not necessary for the path to end at the root. Given a tree with n nodes and an integer k . Find the number of vertical paths such that the (cost of the path) % k = 0 where % represents the modulo operation. Note: The modulo operation returns the remainder of a division after one number is divided by another. For example - 5 % 2 = 1. Example k = 2 tree = There are a total of 8 vertical paths: 1 2 4 2->1 4->1 3 3->4 3->4->1 But only (2 -> 1), (4 -> 1),...

Modular Circular Permutations

  You are given an array   A = [ A 1 , A 2 , … , A N ] A = [ A 1 , A 2 , … , A N ]  containing  distinct positive  integers. Let  B B  be a  permutation  of  A A . Define the  value  of  B B  to be ∑ i = 1 N ( B i mod B i + 1 ) ∑ i = 1 N ( B i mod B i + 1 ) where  B N + 1 B N + 1  is treated to be  B 1 B 1 . Find the  maximum   value  across all permutations of  A A . Input Format The first line of input contains a single integer  T T , denoting the number of test cases. The description of  T T  test cases follows. Each test case consists of two lines of input. The first line of each test case contains an integer  N N  — the size of  A A . The second line of each test case contains  N N  space-separated integers — the elements of  A A . Output Format For each test case, output a new line containing the answer — the maximum possible value a...