We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • Hackerrank Home
  • Prepare
    NEW
  • Certify
  • Compete
  • Career Fair
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Constructive Algorithms
  4. Lovely Triplets
  5. Discussions

Lovely Triplets

Problem
Submissions
Leaderboard
Discussions
Editorial

    You are viewing a single comment's thread. Return to all comments →

  • Akatoki
    5 years ago+ 0 comments

    I divided this problem into 2 cases: when Q is even and odd. And the code below is what I wrote just for the even cases. It worked correctly for the test case #0 where P=3 and Q=2; however, I received an X on the test case #16 where P=3505 and Q=2. My code should print one of the correct answers for the test #16 as well, but somehow I got a cross. Would anyone here have an idea why this is hapenning? I would appreciate your help.

    #!/bin/python3
    
    import sys
    
    def printTopRow(num):
        for i in range(num):
            print(i+1,i+2)
    
    def printLowerRows(P, Q, b):
        for i in range(P):
                for j in range(int(Q/2)):
                    if j == 0:
                        print(int(Q/2)+1+i*Q, b+1+i*int(Q/2))
                    else:
                        print(b+i*int(Q/2)+j, b+i*int(Q/2)+j+1)
    
    
    
    if __name__ == "__main__":
        P, Q = map(int, input().split())
    
        if Q % 2 == 0:
            """Case 1: Q is even"""    
            m = int((3/2)*P*Q)
            n = m+1
            biggest = P*Q+1
    
            print(n,m)
            printTopRow(biggest-1)
            printLowerRows(P,Q,biggest)
    
        else:
            """Case 2: Q is odd"""
            pass
    
    0|
    Permalink
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature