Posts

Showing posts with the label Code

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     / \     \               /  \           \    4   1   2          4 --> 1 -----> 2 -------> NULL   Example 1: Input: 3   / \   1 2 Output: 3 1 2 1 3 2 Explanation: The connected tree is        3 ------> NULL      /   \    1---> 2 -----> NULL Example 2: Input: 10   / \   20 30   / \ 40 60 Output: 10 20 30 40 60 40 20 60 10 30 Explanation: The connected tree is         10 ---> NULL       /   \      20---> 30 ---> NULL    /   \  40---> 60 ---> NULL Your Task: You don't have

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 iterations required to fill the matrix. Expected Time Complexity:  O(N

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  into subsegments, such that the bitwise AND of these segments form a palindrome. Constraints T ≤ 1500 T ≤ 1500