Minimize increments or decrements by 2 to convert given value to a perfect square
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++
// C++ program to implement// the above approach#include <bits/stdc++.h>using namespace std;// Function to find the minimum number// of operations required to make// N a perfect squareint MinimumOperationReq(int N){ // Stores count of operations // by performing decrements int cntDecr = 0; // Stores value of N int temp = N; // Decrement the value of temp while (temp > 0) { // Stores square root of temp int X = sqrt(temp); // If temp is a perfect square if (X * X == temp) { break; } // Update temp temp = temp - 2; cntDecr += 1; } // Store count of operations // by performing increments int cntIncr = 0; // Increment the value of N while (true) { // Stores sqrt of N int X = sqrt(N); // If N is a perfect square if (X * X == N) { break; } // Update temp N = N + 2; cntIncr += 1; } // Return the minimum count return min(cntIncr, cntDecr);}// Driver Codeint main(){ int N = 15; cout << MinimumOperationReq(N); return 0;} |
Java
// Java program to implement// the above approachclass GFG{// Function to find the minimum number// of operations required to make// N a perfect squarestatic int MinimumOperationReq(int N){ // Stores count of operations // by performing decrements int cntDecr = 0; // Stores value of N int temp = N; // Decrement the value of temp while (temp > 0) { // Stores square root of temp int X = (int)Math.sqrt(temp); // If temp is a perfect square if (X * X == temp) { break; } // Update temp temp = temp - 2; cntDecr += 1; } // Store count of operations // by performing increments int cntIncr = 0; // Increment the value of N while (true) { // Stores sqrt of N int X = (int)Math.sqrt(N); // If N is a perfect square if (X * X == N) { break; } // Update temp N = N + 2; cntIncr += 1; } // Return the minimum count return Math.min(cntIncr, cntDecr);}// Driver codepublic static void main (String args[]){ int N = 15; System.out.print(MinimumOperationReq(N)); }}// This code is contributed by ajaykr00kj |
Python3
# Python3 program to implement# the above approach# Function to find the minimum number# of operations required to make# N a perfect squaredef MinimumOperationReq(N): # Stores count of operations # by performing decrements cntDecr = 0; # Stores value of N temp = N; # Decrement the value of # temp while (temp > 0): # Stores square root of # temp X = int(pow(temp, 1 / 2)) # If temp is a perfect # square if (X * X == temp): break; # Update temp temp = temp - 2; cntDecr += 1; # Store count of operations # by performing increments cntIncr = 0; # Increment the value of N while (True): # Stores sqrt of N X = int(pow(N, 1 / 2)) # If N is a perfect # square if (X * X == N): break; # Update temp N = N + 2; cntIncr += 1; # Return the minimum # count return min(cntIncr, cntDecr);# Driver codeif __name__ == '__main__': N = 15; print(MinimumOperationReq(N));# This code is contributed by Rajput-Ji |
C#
// C# program to implement// the above approachusing System;class GFG{// Function to find the minimum number// of operations required to make// N a perfect squarestatic int MinimumOperationReq(int N){ // Stores count of operations // by performing decrements int cntDecr = 0; // Stores value of N int temp = N; // Decrement the value of // temp while (temp > 0) { // Stores square root of temp int X = (int)Math.Sqrt(temp); // If temp is a perfect square if (X * X == temp) { break; } // Update temp temp = temp - 2; cntDecr += 1; } // Store count of operations // by performing increments int cntIncr = 0; // Increment the value of N while (true) { // Stores sqrt of N int X = (int)Math.Sqrt(N); // If N is a perfect square if (X * X == N) { break; } // Update temp N = N + 2; cntIncr += 1; } // Return the minimum count return Math.Min(cntIncr, cntDecr);}// Driver codepublic static void Main(String []args){ int N = 15; Console.Write(MinimumOperationReq(N)); }}// This code is contributed by shikhasingrajput |
Javascript
<script>// Javascript program to implement// the above approach// Function to find the minimum number// of operations required to make// N a perfect squarefunction MinimumOperationReq(N){ // Stores count of operations // by performing decrements let cntDecr = 0; // Stores value of N let temp = N; // Decrement the value of temp while (temp > 0) { // Stores square root of temp let X = Math.floor(Math.sqrt(temp)); // If temp is a perfect square if (X * X == temp) { break; } // Update temp temp = temp - 2; cntDecr += 1; } // Store count of operations // by performing increments let cntIncr = 0; // Increment the value of N while (true) { // Stores sqrt of N let X = Math.floor(Math.sqrt(N)); // If N is a perfect square if (X * X == N) { break; } // Update temp N = N + 2; cntIncr += 1; } // Return the minimum count return Math.min(cntIncr, cntDecr);} // Driver Code let N = 15; document.write(MinimumOperationReq(N));</script> |
3
Time Complexity: O(N * log2(N))
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.


