Related Articles

Related Articles

Minimize increments or decrements by 2 to convert given value to a perfect square
  • Last Updated : 20 Nov, 2020

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:
Explanation: N – 2 = 16( = 42). Therefore, a single decrement operation is required.

Input: N = 15
Output:
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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 square
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 = 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 Code
int main()
{
 
    int N = 15;
    cout << MinimumOperationReq(N);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to implement
// the above approach
class GFG{
 
// Function to find the minimum number
// of operations required to make
// N a perfect square
static 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 code
public static void main (String args[])
{
    int N = 15;
     
    System.out.print(MinimumOperationReq(N)); 
}
}
 
// This code is contributed by ajaykr00kj

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to implement
# the above approach
 
# Function to find the minimum number
# of operations required to make
# N a perfect square
def 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 code
if __name__ == '__main__':
   
    N = 15;
    print(MinimumOperationReq(N));
 
# This code is contributed by Rajput-Ji

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to implement
// the above approach
using System;
class GFG{
 
// Function to find the minimum number
// of operations required to make
// N a perfect square
static 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 code
public static void Main(String []args)
{
  int N = 15;
  Console.Write(MinimumOperationReq(N)); 
}
}
 
// This code is contributed by shikhasingrajput

chevron_right


Output: 

3











 

Time Complexity: O(N * log2(N))
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :