Skip to content
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Learn everything about programming

HackerRank Stone Division Revisited problem solution

YASH PAL, 31 July 202430 November 2025

In this HackerRank Stone Division, Revisited problem solution we have given Q queries where each query consists of N pile of stones and a set of M distinct integers S. we need to calculate the maximum possible number of moves we can perform and print it on a new line.

HackerRank Stone Division Revisited problem solution

Problem solution in Python.

def solve(n,nums,dp):
    res=0
    for i,v in enumerate(nums):
        if n%v==0 and n>v:
            res=max(res,1+dp[i]*(n//v))
    return res

q=int(input())
for _ in range(q):
    n,m=list(map(int,input().split(" ")))
    nums=list(map(int,input().split(" ")))
    nums.sort()
    dp=[0]*len(nums)
    for i in range(1,len(nums)):
        for j in range(i):
            if nums[i]%nums[j]==0:
                dp[i]=max(dp[i],1+(nums[i]//nums[j])*dp[j])
    print(solve(n,nums,dp))

Problem solution in C++.

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<assert.h>

using namespace std;

long long n,s[1001],s1[1001],ans,dp[1001];
int m,br;
const long long maxi=1e12;
void ocisti()
{
   br=0;
   for (int i=0;i<1001;i++)
    dp[i]=0;
}

void solve()
{
    ocisti();
    scanf("%lld%d",&n,&m);

    assert(1L<=n && 1<=maxi);
    assert(1<=m && m<=1000);

    for (int i=0;i<m;i++)
    {
      scanf("%lld",&s[i]);
      assert(1L<=s[i] && s[i]<=maxi);
      if (s[i]<n && n%s[i]==0)
      {
          br++;
          s1[br-1]=s[i];
      }
    }
    br++;
      s1[br-1]=n;

      sort(s1,s1+br);

   for (int i=0;i<br;i++)
    for (int j=i-1;j>=0;j--)
     if (s1[i]%s1[j]==0)
            dp[i]=max(dp[i],(s1[i]/s1[j])*dp[j]+1);


   printf("%lldn",dp[br-1]);

   return;
}

int main()
{
    int t;
    scanf("%d",&t);
    assert(0<t && t<=10);

  while (t--)
   solve();

 return 0;
}

Algorithms coding problems solutions AlgorithmsHackerRank

Post navigation

Previous post
Next post

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes