Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Hackerrank:Down to Zero problem

Hackerrank:Down to Zero problem

Scheduled Pinned Locked Moved C / C++ / MFC
cssdatabasehelpquestion
2 Posts 2 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SrinivasaRamanujan
    wrote on last edited by
    #1

    Problem statement: You are given Q queries. Each query consists of a single number N. You can perform any of the 2 operations on in each move: 1: If we take 2 integers a and b where N=aXb(a!=1,b!=1) then we can change N=max(a,b). 2: Decrease the value of N by 1. Determine the minimum number of moves required to reduce the value of N to 0 Input Format The first line contains the integer Q. The next Q lines each contain an integer,N . Output Format Output Q lines. Each line containing the minimum number of moves required to reduce the value of N to 0. Sample Input 2 3 4 Sample Output 3 3 Explanation For test case 1, We only have one option that gives the minimum number of moves. Follow 3->2 -> 1->0 . Hence, 3 moves. For the case 2, we can either go 4->3 ->2 ->1 ->0 or4 -> 2-> 1->0 . The 2nd option is more optimal. Hence, 3 moves. My algo: if a number is N then I do looping until sqrt(N) to find if it is a prime or not. if it is a prime number then N=N-1 if it not a prime number,one of the largest factor(say a) is <=sqrt(N) then other will be b=N/a now b>a then put N=b; increment count(pass by value). then next iteration for N ,until it is greater than 1. return count. algorithms works fine with small value but predicts less optimal solution for large values.why?

    D 1 Reply Last reply
    0
    • S SrinivasaRamanujan

      Problem statement: You are given Q queries. Each query consists of a single number N. You can perform any of the 2 operations on in each move: 1: If we take 2 integers a and b where N=aXb(a!=1,b!=1) then we can change N=max(a,b). 2: Decrease the value of N by 1. Determine the minimum number of moves required to reduce the value of N to 0 Input Format The first line contains the integer Q. The next Q lines each contain an integer,N . Output Format Output Q lines. Each line containing the minimum number of moves required to reduce the value of N to 0. Sample Input 2 3 4 Sample Output 3 3 Explanation For test case 1, We only have one option that gives the minimum number of moves. Follow 3->2 -> 1->0 . Hence, 3 moves. For the case 2, we can either go 4->3 ->2 ->1 ->0 or4 -> 2-> 1->0 . The 2nd option is more optimal. Hence, 3 moves. My algo: if a number is N then I do looping until sqrt(N) to find if it is a prime or not. if it is a prime number then N=N-1 if it not a prime number,one of the largest factor(say a) is <=sqrt(N) then other will be b=N/a now b>a then put N=b; increment count(pass by value). then next iteration for N ,until it is greater than 1. return count. algorithms works fine with small value but predicts less optimal solution for large values.why?

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      See here.

      "One man's wage rise is another man's price increase." - Harold Wilson

      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

      "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups