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

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Graph Theory
  4. Diameter Minimization
  5. Discussions

Diameter Minimization

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 10 Discussions, By:

recency

Please Login in order to post a comment

  • mineman1012221
    2 months ago+ 0 comments

    Here is the solution of Diameter Minimization Click Here

    0|
    Permalink
  • thecodingsoluti2
    4 months ago+ 0 comments

    Here is problem solution - https://programs.programmingoneonone.com/2021/07/hackerrank-diameter-minimization-problem-solution.html

    0|
    Permalink
  • aislandiego
    2 years ago+ 1 comment

    Holy Jesus, I'm not that smart... I can't even figure out how to start...

    0|
    Permalink
  • TChalla
    2 years ago+ 1 comment

    Python solution

    #!/bin/python3
    
    import sys
    import math
    
    def opt_diameter(n, m):
        count = 1
        depth = 0
        while count < n:
            depth += 1
            count = m ** depth
        return depth
        
    def diameter(n, m):
        count = 1
        depth = 0
        while count < n:
            depth += 1
            count += m ** depth
        left_over = m ** depth - (count - n)
        limit = m ** (depth - 1)
        discount = 1
        if left_over > limit:
            discount = 0
        return max(depth, 2 * depth - discount)
    
    def solve(in_file, out_file):
        n, m = (int(raw) for raw in in_file.readline().strip().split(' '))
        out_file.write("{}\n".format(opt_diameter(n, m)))
        count = 0
        for _ in range(n):
            ret = []
            for _ in range(m):
                val = count % n
                count += 1
                ret.append(str(val))
            out_file.write("{}\n".format(" ".join(ret)))
    
    if __name__ == '__main__':
        from_file = False
        if from_file:
            path = 'Data\\'
            name = 'mega_prime'
            file_input = open(path + name + '.in', 'r')
            file_output = open(path + name + '.out','w')
            solve(file_input, file_output)
            file_input.close()
            file_output.close()
        else:
            solve(sys.stdin, sys.stdout)
    
    1|
    Permalink
  • rajit404
    2 years ago+ 0 comments

    python3

    it is working fine while running the code but it fails all the test case!! can anyone tell me the problem.

    !/bin/python3

    import math import os import random import re import sys

    if name == 'main': nm = list(map(int,input().split()))

    n = int(nm[0])
    
    m = int(nm[1])
    
    list1 = [x for x in range(n)]
    print(m)
    for i in range(n):
        if i < (len(list1)-1):
            print(list1[i+1],list1[i-1]) 
        elif i >= (len(list1)-1): 
            i = -1
            print(list1[i+1],list1[i-1])
    
    0|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy