Posts

Showing posts with the label Code

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),...

Connect Nodes at Same Level

  Given a binary tree, connect the nodes that are at the same level. The structure of the tree Node contains an addition  nextRight  pointer for this purpose. Initially , all the  nextRight  pointers point to  garbage  values.  Your function  should set these pointers to point next right for each node.        10                       10 ------> NULL       / \                       /      \      3   5       =>     3 ------> 5 --------> NULL     / \     \               /  \     ...

Fill the Matrix

Image
  Given a matrix with dimensions   N   x   M , entirely filled with zeroes except for one position at co-ordinates   X   and   Y  containing '1'. Find the minimum number of iterations in which the whole matrix can be filled with ones. Note:  In one iteration, '1' can be filled in the 4 neighbouring elements of a cell containing '1'. Example 1: Input : N = 2, M = 3 X = 2, Y = 3 Output:  3  Explanation : 3 is the minimum possible number of iterations in which the matrix can be filled. Example 2: Input: N = 1, M = 1 X = 1, Y = 1 Output:  0 Explanation : The whole matrix is already filled with ones. Your Task:   You don't need to read input or print anything. Your task is to complete the function  minIterations()  which takes the dimensions of the matrix N and M and the co-ordinates of the initial position of '1' ie X and Y   as input parameters and returns the minimum number of it...

AND Palindromes

  You are given an array   A A  consisting of  N N  integers. Calculate the number of ways to divide this array into subsegments, such that the sequence formed by taking bitwise AND in each segment of the partition is a palindrome. A partition is  palindromic  if  B i = B k + 1 − i B i = B k + 1 − i  for every  1 ≤ i ≤ k 1 ≤ i ≤ k . Your task is to calculate the number of palindromic partitions. Since this number can be large, calculate it modulo  998244353 998244353 . Input Format The first line of input contains  T T  — the number of test cases you need to solve. The first line of each test case contains one integer  N N  — the size of the array. The second line of each test case contains  N N  space-separated integers  A 1 , … , A N A 1 , … , A N  — the elements of the array  A A . Output Format For each test case, output on a new line the number of ways to partition  A A  in...