Find maximum possible moves to achieve a target integer using Squares
Last Updated :
12 Oct, 2023
Given an integer X, and A, B having value initially 0, the task is to find the maximum number of possible moves we can make to make A equal to X such that in each move we can choose any positive number whose square is greater than the current value of B and update A to that particular number chosen and add the square of the number to B.
Examples:
Input: X = 5
Output: 4
Explanation: The possible sequence of values of X can be: 0 -> 1 -> 2 -> 3 -> 5, Which takes 4 moves. The moves are as follows:
- A = 0, B = 0
- First Move: Let take positive integer 1, because 1*1 is greater than current value of B (B = 0). So, update A as 1 and B = 0 + (1*1) = 1.
- X = 1, Y = 1
- Second Move: Let take positive integer 2, because 2*2 is greater than current value of B (B = 1). So, update A as 2 and B = 1 + (2*2) = 5.
- X = 2, Y = 5
- Third Move: Let take positive integer 3, because 3*3 is greater than current value of B (B = 5). So, update A as 3 and B = 5 + (3*3) = 14.
- X = 3, Y = 14
- Fourth Move: Let take positive integer 5, because 5*5 is greater than current value of B (B = 14). So, update A as 5 and B = 14 + (5*5) = 39.
- X = 5, Y = 39
Now, We have reached at A = 5, It can be verified that following the given rule of the game, 4 is the maximum number of possible moves we can make from reaching A = 0 to A = 5.
Input: X = 8
Output: 5
Explanation: The possible sequence of values of X can be: 0 -> 1 -> 2 -> 3 -> 5 -> 8, Which takes 5 moves. It can be verified that there is no possible number of moves greater than 5, such that following the above-defined rule, We can reach from X = 0 to X = 8.
Approach: Implement the idea below to solve the problem:
The problem is based on mathematical logic. The idea of the problem is defined as below in the Concept of approach section.
Concept of Approach:
The problem is mathematical logic based and can be solved by using some mathematical observations. The algorithm and observation for solving the problem is defined below for the input value of X in variable N.
Take variables A, B, count = 0
// Observation
while (A ≤ X)
{
A = Math.sqrt(B) + 1
B = B + X * X
count++
}
The maximum number of moves will be count – 1.
Follow the steps to solve the problem:
- Initialize integers A, B, and Count as 0.
- While (A ≤ X) follow below – mentioned steps under the scope of the while loop:
- A = sqrt( B ) + 1
- B = B + (X * X)
- Count = Count + 1
- Return Count – 1.
Below is the code to implement the approach:
C++
#include <bits/stdc++.h>
using namespace std;
int MaxMoves( int n)
{
int x = 0;
int y = 0;
int count = 0;
while (x <= n) {
x = sqrt (y) + 1;
y = y + (x * x);
count++;
}
return count - 1;
}
int main()
{
int N = 5;
cout << MaxMoves(N);
}
|
Java
import java.util.*;
public class GFG {
public static void main(String args[])
{
int N = 5 ;
System.out.println(MaxMoves(N));
}
static int MaxMoves( int n)
{
long x = 0 ;
long y = 0 ;
int count = 0 ;
while (x <= n) {
x = ( long )Math.sqrt(y) + 1 ;
y = y + x * x;
count++;
}
return count - 1 ;
}
}
|
Python3
import math
def max_moves(n):
x = 0
y = 0
count = 0
while x < = n:
x = int (math.sqrt(y)) + 1
y = y + (x * x)
count + = 1
return count - 1
n = 5
print (max_moves(n))
|
C#
using System;
public class Program
{
public static int MaxMoves( int n)
{
int x = 0;
int y = 0;
int count = 0;
while (x <= n)
{
x = ( int )Math.Sqrt(y) + 1;
y = y + (x * x);
count++;
}
return count - 1;
}
public static void Main()
{
int N = 5;
Console.WriteLine(MaxMoves(N));
}
}
|
Javascript
function MaxMoves(n) {
let x = 0;
let y = 0;
let count = 0;
while (x <= n) {
x = Math.floor(Math.sqrt(y)) + 1;
y = y + (x * x);
count++;
}
return count - 1;
}
const N = 5;
console.log(MaxMoves(N));
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Approach: using Binary Search
Concept of Approach:
In this approach, first find the largest sqaure number less than or equal to the current value and update to the
next integer greater than the current value.
Follow the steps to solve the problem:
- Initialize A and B to 0.
- Also, initialize the counter to 0.
- When A < X, do the following:
1. use the binary search and check if (A+mid)^2 is greater than B.
2. If yes, update num and search for a smaller number.
3. If no suitable number is found, break out of the loop.
4. Otherwise, update A and B and increment the move count.
5. Increment the counter.
- Last, print the maximum number of moves.
Below is the code to implement the approach:
C++
#include<bits/stdc++.h>
using namespace std;
int main(){
int X=5, A=0, B=0, count=0;
while (A<X){
int low = 1, high = X-A, mid, num=-1;
while (low<=high){
mid = (low+high)/2;
if ((A+mid)*(A+mid) > B) {
num = mid;
high = mid-1;
}
else low = mid+1;
}
if (num == -1) break ;
A += num;
B += A*A;
count++;
}
cout<<count<<endl;
return 0;
}
|
Java
import java.io.*;
public class Geek {
public static void main(String[] args) {
int X = 5 , A = 0 , B = 0 , count = 0 ;
while (A < X) {
int low = 1 , high = X - A, mid, num = - 1 ;
while (low <= high) {
mid = (low + high) / 2 ;
if ((A + mid) * (A + mid) > B) {
num = mid;
high = mid - 1 ;
} else {
low = mid + 1 ;
}
}
if (num == - 1 ) {
break ;
}
A += num;
B += A * A;
count++;
}
System.out.println(count);
}
}
|
Python3
X = 5
A = 0
B = 0
count = 0
while A < X:
low = 1
high = X - A
num = - 1
while low < = high:
mid = (low + high) / / 2
if (A + mid) * (A + mid) > B:
num = mid
high = mid - 1
else :
low = mid + 1
if num = = - 1 :
break
else :
A + = num
B + = A * A
count + = 1
print (count)
|
C#
using System;
class Program {
static void Main()
{
int X = 5, A = 0, B = 0, count = 0;
while (A < X) {
int low = 1, high = X - A, mid, num = -1;
while (low <= high) {
mid = (low + high) / 2;
if ((A + mid) * (A + mid) > B) {
num = mid;
high = mid - 1;
}
else {
low = mid + 1;
}
}
if (num == -1)
break ;
A += num;
B += A * A;
count++;
}
Console.WriteLine(count);
}
}
|
Javascript
function main() {
let X = 5, A = 0, B = 0, count = 0;
while (A < X) {
let low = 1, high = X - A, mid, num = -1;
while (low <= high) {
mid = Math.floor((low + high) / 2);
if ((A + mid) * (A + mid) > B) {
num = mid;
high = mid - 1;
} else {
low = mid + 1;
}
}
if (num === -1) {
break ;
}
A += num;
B += A * A;
count++;
}
console.log(count);
}
main();
|
Time Complexity: O(X*log(X)), where X is the input integer.
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...