• RSS
  • Facebook
  • Twitter

Knowledge is Power.

  • Who you are ?

    Working on machines without understanding them ? Then you should be here..

  • Where you are ?

    Geographical location should not become a barrier to Share our knowledge.

  • What do you do ?

    Puzzles and Interview question are intended to be discussed here.

    Monday, July 8, 2013

    Problem Statement
    You are given an array A[] containing n positive integers. Lets define Bitonic subsequence. A subsequence of array A[] is called Bitonic if it is first strictly increasing and then strictly decreasing. Your task is to find the length of largest bitonic subsequence given an array.

    Note- A sequence sorted in increasing order is considered as Bitonic with decreasing part as empty. Similarly, A sequence sorted in decreasing order is also considered as bitonic with increasing part as empty

    How many of you guys have got the solution for this problem ?

    Well, I have got the logic which work perfect but unfortunately It doesn`t fetch 100 on the Techgig compiler. The problem is not with my Code/Loigc but the problem is Techgig`s definition for Bitonic subsequence. Which can be found here

    Techgig say that the sequence should be strictly increasing and then decreasing where as it can be either way and that too in circular fashion.

    If you refer the previous link we can see that


    3 5 8 9 7 4 2 1
    5 8 9 7 4 2 1 3


    both are bitonic such that 2nd sequence can be converted to first by a circular shift.

    Lets, come to the basic question on Techgig and see how to implement a logic for it.

    I am considering a sequence for example given below.
    9,10,1,3,5,8,2,1,7,5,3,1

    Here starts the actual logic :-
    As we want an subsequence which is first increasing and then decreasing let find the increasing numbers.
    We will be going number by number.

    step 1 : Consider 9, It is the first number so directly put it into a list(Array)
                 9 (List)

    step 2: next is 10, since 10 is greater and 9, put it into same list
                9,10

    step 3: 1, since 1 is less than 10 and we are searching for increasing number, we will create a new list
               9,10
               1
               At this point, If we had any numbers less then 1 in the previous list then those needed to be first      i inserted in the new list and then 1. But we didnt have any here

    step 4: Once we have more than 1 list, every new number has to be checked with the last number of that list and if the new number is greater then it has to be appended to that list. The next number is 3,first check in the previous list if 3 is greater than the last number if it. In our case we have 10 as the last number so we need not append 3.
    Now, 3 is greater than 1, so just append it.
               9,10
               1,3

    Repeat the above steps, we get
              9,10
              1,3,5,8
              1,2
              1,7
              1,5
              1,3
              1
    These are the various lists which we get. Now we can see here that 2nd list has the largest length. But that wont help us, because it may happen the there is not decreasing subsequence for it or may be some other list has a decreasing sequence of greater length.

    Hence, once these lists are formed we perform a similar process for decreasing lists.

    The important thing to keep in mind is the position(in input1[]) of last number in our lists.Because, we need to find the decreasing sequence only after that position.

    For example, lest take our first list 9,10 and try to find the various decreasing sequences for it.

    step 1 :1
    step 2: 1
               3

    step 3: 1
               3

               5   


    step 4: 1
               3

               5
               8

    step 5: 1
               3,2

               5,2
               8,2
    step 6: 1 
               3,2,1
               5,2,1
               8,2,1

    step 7: 1 (Important)
               3,2,1
               5,2,1
               8,2,1
               8,7

    step 8: 1 (Important)
               3,2,1
               5,2,1
               8,2,1
               8,7,5,3,1

    Now, we can see that for increasing order 9,10; we have a decreasing order 8,7,5,3,1 of length 5, so 5+2=7 is the length of this Bitonic.

    Similarly, we can find the decreasing sequences for other lsit`s generated in part I and check the largest length.


               
    Hope this was helpful. There may be other logic for the implementation but this was my own from scratch.
    Also, this can be modified for the circular sequence.

    If anyone want the code for this just put a comment below, and I would share the same.


    40 comments:

    1. Hi,
      I am little confused about it. As you said:
      "step 4: Once we have more than 1 list, every new number has to be checked with the last number of that list and if the new number is greater then it has to be appended to that list. The next number is 3,first check in the previous list if 3 is greater than the last number if it. In our case we have 10 as the last number so we need not append 3.
      Now, 3 is greater than 1, so just append it."

      List: 9,10,1,3,5,8,2,1,7,5,3,1

      step 1) 9(List1) Got it
      step 2) 9 10(List1) Got it
      step 3) 9 10(List1)Got it
      1(list 2)
      step 4) 9 10(List1)Got it
      1 3(list 2)
      step 5) 9 10(List1)Got it
      1 3 5(list 2)
      step 6) 9 10(List1)Got it
      1 3 5 8(list 2)
      step 7) 9,10 (List1)
      1,3,5,8 (List2)
      1,2 (Here Confused)
      According to your saying it has to be like this:

      step 7) 9,10 (List1)
      1,3,5,8 (List2)
      2(List3)
      [Because next element is 2 and 2 is smaller than 10 and 8 so it has to be insert in new list but how comes 1 in new list before 2..]
      step 8) 9,10 (List1)
      1,3,5,8 (List2)
      2(List3)
      1(List 4)
      [Same as 7]

      step 9) 9,10 (List1)
      1,3,5,8,7 (List2)
      2 7(List3)
      1 7(List 4)
      [According to Rule]
      step 10) (For next three elemets :5,3,1)
      9,10 (List1)
      1,3,5,8,7 (List2)
      2 7(List3)
      1 7(List 4)
      5(list 5)
      3(List 6)
      1(List 7)

      This is my list, please explain the logic i m still confused !!!

      ReplyDelete
      Replies
      1. Let me clear your first confusion point, after which you see the example again and let me know if it is clear further. Else I would explain more.

        At step 7 (u mentioned confused).
        New array element is 2. Now 2 is smaller than 8 (last element of current list) Whereas we are in Phase I, which is searching for greater elements.

        So, create a new list consisting of 2. But as soon as we get to create a new list, we must first traverse the previous list (1,3,5,8) and search for elements less than 2 and add it to the new list.

        So only 1 is the element which is less than 2. Hence the new list is (1,2)

        Hope this is clear now. Let me know if you still need some details.

        Delete
      2. still i have confusion??
        pls explain in detail
        thanks in advance

        Delete
      3. Can u tell me exactly where u have confusion ?

        Delete
    2. nice.. Can you send me the code to my mail id.. s_rameshkumar@in.com

      ReplyDelete
      Replies
      1. Yes sure. You will receive the code by today evening. But please dont use to submit it in the live contest as we should be truthful to ourselves. Also, I have already mentioned that the codes is perfect still it doesnt fetch 100. you can test it for various inputs.

        Delete
      2. CAN U SEND THE CODE TO ME AS WELL TO MY ID tripathy.samir@gmail.com, i will anlyse further and update asap

        Delete
      3. 7. Merhaba,


        Great post. Well though out. This piece reminds me when I was starting out #topic after graduating from college.


        This is Erin from Amazon Web Services. I took a look at the support case you provided, I see that one of our agents was able to assist and resolve the case.

        Please let us know if there is anything else we can help with!



        Awesome! Thanks for putting this all in one place. Very useful!


        Kind Regards,

        Delete
      4. Merhaba,


        Great post. Well though out. This piece reminds me when I was starting out #topic after graduating from college.


        This is Erin from Amazon Web Services. I took a look at the support case you provided, I see that one of our agents was able to assist and resolve the case.

        Please let us know if there is anything else we can help with!



        Awesome! Thanks for putting this all in one place. Very useful!


        Kind Regards,

        Delete
    3. This comment has been removed by the author.

      ReplyDelete
    4. Guys, Let me know If you like the articles I would publish some more from recently concluded contests. Also, any other problem from Techgig are also welcome.

      ReplyDelete
    5. pls send code for me too vasanvks@gmail.com

      ReplyDelete
    6. http://www.geeksforgeeks.org/dynamic-programming-set-15-longest-bitonic-subsequence/

      ReplyDelete
      Replies
      1. Good link, Infact its the soluction implemented with Dynamic programming. I also earlier tried this approach but didnt quite get to it.

        Delete
    7. please send me code at umashanker.us@gmail.com thanks

      ReplyDelete
    8. This comment has been removed by a blog administrator.

      ReplyDelete
    9. hi... Can u please send me the code at pshraddha2010@gmail.com

      would like to learn the solution...

      thanks

      ReplyDelete
    10. This comment has been removed by the author.

      ReplyDelete
    11. pls send code for me too internship.ajay@gmail.com

      Thanks in advance...

      ReplyDelete
    12. Hi ,Can you please send me the code..in C .. siddiqueiqbal.md@gmail.com...I am having difficulty in identifying the length of the array ..anyways pls. send me your code in C

      ReplyDelete
    13. sir i want code in java my email id is suchsen@gmail.com

      ReplyDelete
    14. hello sir,
      i want c-lang code for this bitonic seq...

      ReplyDelete
    15. Find the maximum element of the array by finding the end of the increasing part of the array using modified binary search.
      Search the increasing part of the array using binary search.
      If the element we are looking for is found in the last step, result found. If not, search the decreasing part of the array using binary search.

      For explanation and code http://www.algoqueue.com/algoqueue/default/view/2818048

      ReplyDelete
    16. Can you please post / send the logic to solve the expert level problem of July Contest ?
      my email id is zishnuit@gmail.com
      Thanks in advance

      ReplyDelete
    17. Can you send me a code to my mail id gomathipriya.kanagasabapathi@gmail.com

      ReplyDelete
    18. Can you please let me know why u have not taken increase sequence as 1,3,5,8 and decrease sequence as 7,5,3,1
      the length of this Bitonic is 8.

      in your example length is 7 why not 8

      ReplyDelete
    19. can u please explain me what is the length of the bitonic for this array int arr[] = {2,1,7,5,3,1,9,10,1,3,5,8};
      thanks

      ReplyDelete
      Replies
      1. It is 7. I got what you want to say. see, the definition say, that the sequence must be first increasing the then decreasing. For example, if you take 1,3,5,8 as increasing then the decreasing sequence must start after the position of 8 and not from the beginning.

        Delete
      2. How did you get 7.
        it's 1,3,5,8 increasing sequence then after no decreasing sequence so it should be 4 only ,right?

        Delete
    20. 1,3,5,8,7,5,3,1
      is this not the longest bitonic sequence in the values mentioned in the problem?

      ReplyDelete
    21. Salve,


      Interesting piece!Great to see someone write this blog who is not a fanatic or a complete skeptic.


      I enjoy reading the various AWS blogs and staying up to date with new offerings and best practices. I typically go to the root of the blog site and check the "Latest Posts" section at the bottom.

      It looks like the "Latest Posts" section stopped updating about 2 weeks ago on April 20th. It would be very helpful if this could be fixed since this was very useful.



      Excellent tutorials - very easy to understand with all the details. I hope you will continue to provide more such tutorials.


      Merci Beaucoup,
      Morgan

      ReplyDelete
    22. Ni Hau,


      Hot! That was HOT! Glued to the Logic Puzzle: Pigeonhole Principle your proficiency and style!

      I have to login to AWS multiple times a day but usually from the same desktop and browser. Since activating MFA, it has been a nightmare! At least twice a day I have to enter the MFA code from the authenticator. Even though I use the same browser!
      I prefer this AWS Tutorial blog for more reference, and now am using this blog for my training practice.

      But nice Article Mate! Great Information! Keep up the good work!

      Many Thanks,
      Morgan

      ReplyDelete
    23. Hello,

      A really interesting, clear and easily readable Knowledge is Power. article of interesting and different perspectives.I will clap. So much is so well covered here. AWS Training


      We've been analysing our Google Adwords account and have found that since January our clicks have rocketed for no reason. We use Click Cease to track fraud and on closer inspection found that for the past 2 months 7 IP addresses all owned by Amazon are responsible for around 75% of all our spend! The system says that the OS every time was Windows XP, the browser used was always FireFox 3.0.8 and it's VPN software. When I spoke to ClickCease they believe it's a click bot running on AWS.

      Anyways great write up, your efforts are much appreciated.

      Grazie,
      Radhey

      ReplyDelete
    24. Hey There,


      Best thing I have read in a while on this blog There should be a standing ovation button. This is a great piece.

      When creating a crawler that reads from S3, it would be nice if you could specify the Athena table name. At the moment it takes the name of the S3 folder it crawls.

      By the way do you have any YouTube videos, would love to watch it. I would like to connect you on LinkedIn, great to have experts like you in my connection (In case, if you don’t have any issues).


      Shukran,
      Morgan

      ReplyDelete