Don_quixxote's blog

By Don_quixxote, history, 11 months ago, In English
Problem 1.
Given an array find no. of non-empty subsequences which does not have three consecutive odd or three consecutive even numbers in subsequence. Since answer is large print it modulo 1e9+7.
Expected time complexity is O(N) or (ONlogN).

hint
code
Problem 2.
A number X is said to be palindrome special, if every digit i present in X occurs exactly i times and digits of X form a palindrome, you are given t test cases (<=1e5) and in each test case integer N(<=1e17). Find nearest greater no. which is a special palindrome.
hint
code
  • Vote: I like it
  • +7
  • Vote: I do not like it

»
11 months ago, hide # |
 
Vote: I like it -10 Vote: I do not like it

In the solution of question 2, 442244 and 244442 and 424424 all are valid special palindromes but the given code adds all the digits d//2 times together. So, I think only 244442 will be generated but other 2 will not be generated. Correct me if I am wrong.

»
11 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by Don_quixxote (previous revision, new revision, compare).

»
11 months ago, hide # |
Rev. 2  
Vote: I like it -10 Vote: I do not like it

The second problem's code is incorrect. What you can do is ---- generate all bitmasks, then generate all permutations of said bitmask's size, if it's a valid bitmask. Create a multiset out of the valid digits. So, assuming all 9 digit nos, you have 512 * 9! permutations in total, which is admittedly pretty high, but actual time complexity will obviously be far far lower, infact of the order of actual valid "special palindromes".

  • »
    »
    11 months ago, hide # ^ |
    Rev. 2  
    Vote: I like it 0 Vote: I do not like it

    I think no.of ways to arrange will be for half length, (total_digits) !/ (product of factorial of frequency)!. Let's say we want palindrome containing 2 and 4, total ways = 3!/(2!*1!) = 3 (424, 244, 442). So to say 9! ways exist is not correct, imo. In worst case (all included), it will be equal to, 45!/(1!*2!...*9!), idk how much this is or will this pass or not.

    • »
      »
      »
      11 months ago, hide # ^ |
       
      Vote: I like it -10 Vote: I do not like it

      you're not passing it if it's more than 9 digits. So, what you're doing is --> generate bitmask -- check if valid (cnt <= 18, no of odd vals <= 1).

      in this scenario, you get a multiset with <= 9 digits in all instances. moreover, the uniqueness factor will automatically take care of itself. now simply iterate over all permutations of this multiset, and you have valid nos. all such valid nos will create your collection of special permutations!

    • »
      »
      »
      11 months ago, hide # ^ |
      Rev. 6  
      Vote: I like it -10 Vote: I do not like it

      TRY THIS CODE. ofc, not including the binary search too, just the generation of "Special Palindromes"

      Spoiler
»
11 months ago, hide # |
 
Vote: I like it -10 Vote: I do not like it

could somebody suggest the best resource to practice these type of questions?

  • »
    »
    11 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    You can go through past oa of different companies, there are multiple websites like joboverflow, desi qna, learnyard...

»
11 months ago, hide # |
 
Vote: I like it -10 Vote: I do not like it

Auto comment: topic has been updated by Don_quixxote (previous revision, new revision, compare).

»
11 months ago, hide # |
Rev. 2  
Vote: I like it +3 Vote: I do not like it

(Will improve my explanation later when I get time) For problem 2,

Assume there are 2N digits i.e. the number of digits are even. For i=0...N-1, I will try to create a special palindrome by only changing the digits in the range [N-i,N+1+i] where the digit at position N-i is greater than in the originally provided number. When I find an answer, I will stop as this is the best answer.

Let's fix the value of i. The best answer should have the smallest possible increase in digit value at position N-i followed by as low of digits as possible. We can find if a solution exists using a slightly modified 0/1 knapsack. There are some digits we already need to place in the range [N-i,N+i+1] because that digits occurs outside the range but not the required amount of times. Once you know the amount of digits you can decide upon, run a knapsack on all the even digits that aren't already present in the number with the requirement that at least one of the digits added are >= the digit at position N-i. If you are required to add a digit that is >= the digit at position N-i, you can ignore this requirement.

If you have a knapsack solution, that means you do have a solution. Make sure to keep track of a "parent" vector so you can backtrack to get to a valid answer. How do we perform the knapsack in a way that gives the optimal solution? You should be able to do this by modifying the order in which you add elements. If the >= condition is already satisfied you can just go in decreasing order while overriding the parent vector whenever you get the opportunity. Otherwise, say j is the digit at position N-1. Add all elements less than j in decreasing order, overriding the parent vector whenever you get the opportunity. Then do all elements greater than j in decreasing order, only overriding the parent vector when this is the first visit or if it the end position. The path back using the parent vector should tell you the numbers to use. Simply choose the smallest number greater than j to use first and then build as small as possible using the numbers given to you by the knapsack.

This runs in $$$O(log(N)^2)$$$ I believe.

  • »
    »
    11 months ago, hide # ^ |
     
    Vote: I like it +8 Vote: I do not like it

    Won't it TLE, with t = 10^5??

    • »
      »
      »
      11 months ago, hide # ^ |
       
      Vote: I like it +3 Vote: I do not like it

      I think you would be right if it were $$$O(\log(N)^3)$$$ which I previously claimed. It's actually $$$O(D\log(N)^2)$$$ where D is the base (10). I would expect this to AC because of low constant factors.

      I haven't counted how many special palindromes actually exist here. If it is low enough to actually generate them all, that is probably a better solution. This solution however works for incredibly big N and large bases.