Given a number N. At every step, subtract the largest perfect square( ? N) from N. Repeat this step while N > 0. The task is to count the number of steps that can be performed.
Examples:
Input: N = 85
Output: 2
First step, 85 – (9 * 9) = 4
Second step 4 – (2 * 2) = 0
Input: N = 114
Output: 4
First step, 114 – (10 * 10) = 14
Second step 14 – (3 * 3) = 5
Third step 5 – (2 * 2) = 1
Fourth step 1 – (1 * 1) = 0
Brute force approach:
The brute force approach to solve this problem involves repeatedly subtracting the largest perfect square less than or equal to N from N until N becomes zero, and counting the number of steps taken. To find the largest perfect square less than or equal to N, we iterate over the integers from sqrt(N) down to 1, and check if the square of the current integer is less than or equal to N. Once we have found the largest perfect square less than or equal to N, we subtract it from N and increment the number of steps taken. We repeat this process until N becomes zero, and then return the number of steps taken.
Implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countSteps( int n)
{
int steps = 0;
while (n > 0) {
int i;
for (i = sqrt (n); i >= 1; i--) {
if (i*i <= n) {
break ;
}
}
n -= i*i;
steps++;
}
return steps;
}
int main()
{
int n = 85;
cout << countSteps(n);
return 0;
}
|
Java
import java.lang.Math;
class Main
{
static int countSteps( int n)
{
int steps = 0 ;
while (n > 0 ) {
int i = ( int )Math.sqrt(n);
while (i >= 1 ) {
if (i * i <= n) {
break ;
}
i--;
}
n -= i * i;
steps++;
}
return steps;
}
public static void main(String[] args)
{
int n = 85 ;
System.out.println(countSteps(n));
}
}
|
Python3
import math
def countSteps(n):
steps = 0
while n > 0 :
i = int (math.sqrt(n))
while i > = 1 :
if i * i < = n:
break
i - = 1
n - = i * i
steps + = 1
return steps
n = 85
print (countSteps(n))
|
C#
using System;
class GFG {
static int countSteps( int n)
{
int steps = 0;
while (n > 0) {
int i;
for (i = ( int )Math.Sqrt(n); i >= 1; i--) {
if (i * i <= n) {
break ;
}
}
n -= i * i;
steps++;
}
return steps;
}
static void Main()
{
int n = 85;
Console.WriteLine(countSteps(n));
}
}
|
Javascript
function countsteps(n){
let steps = 0;
while (n > 0){
let i = Number.parseInt(Math.sqrt(n));
while (i>=1){
if (i*i <= n){
break ;
}
i--;
}
n -= i*i;
steps++;
}
return steps;
}
let n = 85;
console.log(countsteps(n));
|
Time Complexity: O(N * sqrt(N))
Space Complexity: O(1)
Approach: Iteratively subtract the largest perfect square (? N) from N while N > 0 and count the number of steps.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countSteps( int n)
{
int steps = 0;
while (n) {
int largest = sqrt (n);
n -= (largest * largest);
steps++;
}
return steps;
}
int main()
{
int n = 85;
cout << countSteps(n);
return 0;
}
|
Java
import java.lang.Math;
public class GfG{
static int countSteps( int n)
{
int steps = 0 ;
while (n > 0 ) {
int largest = ( int )Math.sqrt(n);
n -= (largest * largest);
steps++;
}
return steps;
}
public static void main(String []args){
int n = 85 ;
System.out.println(countSteps(n));
}
}
|
Python3
from math import sqrt
def countSteps(n) :
steps = 0 ;
while (n) :
largest = int (sqrt(n));
n - = (largest * largest);
steps + = 1 ;
return steps;
if __name__ = = "__main__" :
n = 85 ;
print (countSteps(n));
|
C#
using System;
class GfG
{
static int countSteps( int n)
{
int steps = 0;
while (n > 0)
{
int largest = ( int )Math.Sqrt(n);
n -= (largest * largest);
steps++;
}
return steps;
}
public static void Main()
{
int n = 85;
Console.WriteLine(countSteps(n));
}
}
|
PHP
<?php
function countSteps( $n )
{
$steps = 0;
while ( $n )
{
$largest = (int)sqrt( $n );
$n -= ( $largest * $largest );
$steps ++;
}
return $steps ;
}
$n = 85;
echo countSteps( $n );
?>
|
Javascript
<script>
function countSteps(n)
{
let steps = 0;
while (n)
{
let largest = Math.floor(Math.sqrt(n));
n -= (largest * largest);
steps++;
}
return steps;
}
let n = 85;
document.write(countSteps(n));
</script>
|
Time Complexity: O(logn) because inbuilt sqrt function has been used
Auxiliary Space: O(1), since no extra space has been taken.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!