CodeNation online test questions-need solutions!!
Anonymous User
641

There were three questions, one based on string, one on graph and one on array. I need solutions to the 1st and 3rd questions so please do share any ideas.

  1. Given two strings A and B of same length. In one operation you can set some A[x]=B[x] and this operation costs 1 coin. You can also reverse a substring A[i..j] atmost once. This costs C coins. Find the minimum operations required to make the strings equal.

Example: A="abceda" B="bdecbo" C=1. Output: 3 operations. Explanation: set A[0]=B[0] so A becomes "bbceda", set A[5]=B[5] so A becomes "bbcedo" and then reverse A(1..4) so A becomes "bdecbo" which is B. So 3 operations.

  1. Mex of subtree- given tree of size n, 1<=n<=1e5 indexed from 0, rooted at node 0 and each node having distinct value from 0 till n-1. Find mex of each subtree.

Example : n=3 , edges : (0,1),(0,2) and values : v[0]=0, v[1]=1, v[2]=2. Output : mex[0]=3,mex[1]=0,mex[2]=0.

  1. Given an array A of size n such that 1<=n<=1e5 and 1<=a[i]<=2e5, find out the number of swaps to be done on array elements such that i%3==a[i]%3 for every ith index.

Example: for A = [3, 5, 4]. Output: 1. Swapping 5 and 4 gives array [3, 4, 5] which fulfills i%3==a[i]%3

Comments (4)