Given an integer N, the task is to count the minimum number of times N needs to be incremented or decremented by 2 to convert it to a perfect square.
Examples:
Input: N = 18
Output: 1
Explanation: N – 2 = 16(= 42). Therefore, a single decrement operation is required.
Input: N = 15
Output: 3
Explanation:
N – 2 * 3 = 15 – 6 = 9 (= 32). Therefore, 3 decrement operations are required.
N + 2 * 5 = 25 (= 52). Therefore, 5 increment operations are required.
Therefore, minimum number of operations required is 3.
Approach: Follow the steps below to solve this problem:
- Count the total number of operations, say cntDecr required to make N as a perfect square number by decrementing the value of N by 2.
- Count the total number of operations, say cntIncr required to make N as a perfect square number by incrementing the value of N by 2.
- Finally, print the value of min(cntIncr, cntDecr).
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int MinimumOperationReq( int N)
{
int cntDecr = 0;
int temp = N;
while (temp > 0) {
int X = sqrt (temp);
if (X * X == temp) {
break ;
}
temp = temp - 2;
cntDecr += 1;
}
int cntIncr = 0;
while ( true ) {
int X = sqrt (N);
if (X * X == N) {
break ;
}
N = N + 2;
cntIncr += 1;
}
return min(cntIncr, cntDecr);
}
int main()
{
int N = 15;
cout << MinimumOperationReq(N);
return 0;
}
|
Java
class GFG{
static int MinimumOperationReq( int N)
{
int cntDecr = 0 ;
int temp = N;
while (temp > 0 )
{
int X = ( int )Math.sqrt(temp);
if (X * X == temp)
{
break ;
}
temp = temp - 2 ;
cntDecr += 1 ;
}
int cntIncr = 0 ;
while ( true )
{
int X = ( int )Math.sqrt(N);
if (X * X == N)
{
break ;
}
N = N + 2 ;
cntIncr += 1 ;
}
return Math.min(cntIncr, cntDecr);
}
public static void main (String args[])
{
int N = 15 ;
System.out.print(MinimumOperationReq(N));
}
}
|
Python3
def MinimumOperationReq(N):
cntDecr = 0 ;
temp = N;
while (temp > 0 ):
X = int ( pow (temp, 1 / 2 ))
if (X * X = = temp):
break ;
temp = temp - 2 ;
cntDecr + = 1 ;
cntIncr = 0 ;
while ( True ):
X = int ( pow (N, 1 / 2 ))
if (X * X = = N):
break ;
N = N + 2 ;
cntIncr + = 1 ;
return min (cntIncr,
cntDecr);
if __name__ = = '__main__' :
N = 15 ;
print (MinimumOperationReq(N));
|
C#
using System;
class GFG{
static int MinimumOperationReq( int N)
{
int cntDecr = 0;
int temp = N;
while (temp > 0)
{
int X = ( int )Math.Sqrt(temp);
if (X * X == temp)
{
break ;
}
temp = temp - 2;
cntDecr += 1;
}
int cntIncr = 0;
while ( true )
{
int X = ( int )Math.Sqrt(N);
if (X * X == N)
{
break ;
}
N = N + 2;
cntIncr += 1;
}
return Math.Min(cntIncr,
cntDecr);
}
public static void Main(String []args)
{
int N = 15;
Console.Write(MinimumOperationReq(N));
}
}
|
Javascript
<script>
function MinimumOperationReq(N)
{
let cntDecr = 0;
let temp = N;
while (temp > 0)
{
let X = Math.floor(Math.sqrt(temp));
if (X * X == temp)
{
break ;
}
temp = temp - 2;
cntDecr += 1;
}
let cntIncr = 0;
while ( true )
{
let X = Math.floor(Math.sqrt(N));
if (X * X == N)
{
break ;
}
N = N + 2;
cntIncr += 1;
}
return Math.min(cntIncr, cntDecr);
}
let N = 15;
document.write(MinimumOperationReq(N));
</script>
|
Time Complexity: O(N * log2(N))
Auxiliary Space: O(1)
Efficient Approach:-
- If we think that from a odd number we can react at odd squares only by adding 2 or by subtracting 2
- So we will do two cases for odd and even
- In both of the cases we will find out the nearest small and greater square than N.
- And find the difference between then
- As we are taking +2 or -2 steps then the steps will be difference/2.
- At the end we will take minimum steps from +2 or -2
Implementation:-
C++
#include <bits/stdc++.h>
using namespace std;
int MinimumOperationReq( int N)
{
int cntIncr = 0, cntDecr = 0;
if (N % 2) {
int X = sqrt (N);
if (X % 2 == 0)
X--;
int diff = N - X * X;
cntDecr = diff / 2;
X++;
if (X % 2 == 0)
X++;
diff = X * X - N;
cntIncr = diff / 2;
}
else {
int X = sqrt (N);
if (X % 2)
X--;
int diff = N - X * X;
cntDecr = diff / 2;
X++;
if (X % 2)
X++;
diff = X * X - N;
cntIncr = diff / 2;
}
return min(cntIncr, cntDecr);
}
int main()
{
int N = 15;
cout << MinimumOperationReq(N);
return 0;
}
|
Java
import java.lang.Math;
public class Main
{
public static int MinimumOperationReq( int N)
{
int cntIncr = 0 , cntDecr = 0 ;
if (N % 2 != 0 )
{
int X = ( int )Math.sqrt(N);
if (X % 2 == 0 )
X--;
int diff = N - X * X;
cntDecr = diff / 2 ;
X++;
if (X % 2 == 0 )
X++;
diff = X * X - N;
cntIncr = diff / 2 ;
}
else {
int X = ( int )Math.sqrt(N);
if (X % 2 != 0 )
X--;
int diff = N - X * X;
cntDecr = diff / 2 ;
X++;
if (X % 2 != 0 )
X++;
diff = X * X - N;
cntIncr = diff / 2 ;
}
return Math.min(cntIncr, cntDecr);
}
public static void main(String[] args)
{
int N = 15 ;
System.out.println(MinimumOperationReq(N));
}
}
|
Python3
import math
def MinimumOperationReq(N):
cntIncr = 0
cntDecr = 0
if N % 2 :
X = int (math.sqrt(N))
if X % 2 = = 0 :
X - = 1
diff = N - X * X
cntDecr = diff / / 2
X + = 1
if X % 2 = = 0 :
X + = 1
diff = X * X - N
cntIncr = diff / / 2
else :
X = int (math.sqrt(N))
if X % 2 :
X - = 1
diff = N - X * X
cntDecr = diff / / 2
X + = 1
if X % 2 :
X + = 1
diff = X * X - N
cntIncr = diff / / 2
return min (cntIncr, cntDecr)
if __name__ = = "__main__" :
N = 15
print (MinimumOperationReq(N))
|
C#
using System;
public class GFG
{
public static int MinimumOperationReq( int N)
{
int cntIncr = 0;
int cntDecr = 0;
if ((N % 2) != 0)
{
double _X = Math.Sqrt(N);
int X = Convert.ToInt32(_X);
if (X % 2 == 0)
{
X--;
}
int diff = N - X * X;
cntDecr = diff / 2;
X++;
if (X % 2 == 0)
{
X++;
}
diff = X * X - N;
cntIncr = diff / 2;
}
else
{
double _X = Math.Sqrt(N);
int X = Convert.ToInt32(_X);
if ((X % 2) != 0)
{
X--;
}
int diff = N - X * X;
cntDecr = diff / 2;
X++;
if ((X % 2) != 0)
{
X++;
}
diff = X * X - N;
cntIncr = diff / 2;
}
return Math.Min(cntIncr, cntDecr);
}
internal static void Main()
{
int N = 15;
Console.Write(MinimumOperationReq(N));
}
}
|
Javascript
function MinimumOperationReq(N) {
let cntIncr = 0;
let cntDecr = 0;
if (N % 2) {
let X = Math.floor(Math.sqrt(N));
if (X % 2 == 0) {
X -= 1;
}
let diff = N - X * X;
cntDecr = Math.floor(diff / 2);
X += 1;
if (X % 2 == 0) {
X += 1;
}
diff = X * X - N;
cntIncr = Math.floor(diff / 2);
}
else {
let X = Math.floor(Math.sqrt(N));
if (X % 2) {
X -= 1;
}
let diff = N - X * X;
cntDecr = Math.floor(diff / 2);
X += 1;
if (X % 2) {
X += 1;
}
diff = X * X - N;
cntIncr = Math.floor(diff / 2);
}
return Math.min(cntIncr, cntDecr);
}
let N = 15;
console.log(MinimumOperationReq(N));
|
Time Complexity:- O(LogN)
Auxiliary Space:- O(1)
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!
Last Updated :
01 Mar, 2023
Like Article
Save Article