Count the numbers which can convert N to 1 using given operation

Given a positive integer N (N ≥ 2), the task is to count the number of integers X in the range [2, N] such that X can be used to convert N to 1 using the following operation:

  • If N is divisible by X, then update N’s value as N / X.
  • Else, update N’s value as N – X.

Examples:

Input: N = 6 
Output:
Explanation: 
The following integers can be used to convert N to 1: 
X = 2 => N = 6 -> N = 3 -> N = 1 
X = 5 => N = 6 -> N = 1 
X = 6 => N = 6 -> N = 1

Input: N = 48 
Output: 4

Naive Approach: The naive approach for this problem is to iterate through all the integers from 2 to N and count the number of integers that can convert N to 1.



Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to count the numbers
// which can convert N to 1
// using the given operation
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to count the numbers
// which can convert N to 1
// using the given operation
int countValues(int n)
{
  
    int answer = 0;
  
    // Iterate through all the integers
    for (int i = 2; i <= n; i++) {
        int k = n;
  
        // Check if N can be converted
        // to 1
        while (k >= i) {
            if (k % i == 0)
                k /= i;
            else
                k -= i;
        }
  
        // Incrementing the count if it can
        // be converted
        if (k == 1)
            answer++;
    }
    return answer;
}
  
// Driver code
int main()
{
    int N = 6;
  
    cout << countValues(N);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to count the numbers
// which can convert N to 1
// using the given operation
import java.io.*;
import java.util.*;
class GFG{
      
// Function to count the numbers
// which can convert N to 1
// using the given operation
static int countValues(int n)
{
    int answer = 0;
  
    // Iterate through all the integers
    for (int i = 2; i <= n; i++) 
    {
        int k = n;
  
        // Check if N can be converted
        // to 1
        while (k >= i) 
        {
            if (k % i == 0)
                k /= i;
            else
                k -= i;
        }
  
        // Incrementing the count if it can
        // be converted
        if (k == 1)
            answer++;
    }
    return answer;
}
  
// Driver code
public static void main(String args[])
{
    int N = 6;
  
    System.out.print(countValues(N));
}
}
  
// This code is contributed by shivanisinghss2110

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to count the numbers
# which can convert N to 1
# using the given operation
  
# Function to count the numbers
# which can convert N to 1
# using the given operation
def countValues(n):
    answer = 0
  
    # Iterate through all the integers
    for i in range(2, n + 1, 1):
        k = n
  
        # Check if N can be converted
        # to 1
        while (k >= i):
            if (k % i == 0):
                k //= i
            else:
                k -= i
  
        # Incrementing the count if it can
        # be converted
        if (k == 1):
            answer += 1
    return answer
  
# Driver code
if __name__ == '__main__':
      
    N = 6
    print(countValues(N))
  
# This code is contributed by Samarth

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to count the numbers
// which can convert N to 1
// using the given operation
using System;
class GFG{
      
// Function to count the numbers
// which can convert N to 1
// using the given operation
static int countValues(int n)
{
    int answer = 0;
  
    // Iterate through all the integers
    for (int i = 2; i <= n; i++) 
    {
        int k = n;
  
        // Check if N can be converted
        // to 1
        while (k >= i) 
        {
            if (k % i == 0)
                k /= i;
            else
                k -= i;
        }
  
        // Incrementing the count if it can
        // be converted
        if (k == 1)
            answer++;
    }
    return answer;
}
  
// Driver code
public static void Main()
{
    int N = 6;
  
    Console.Write(countValues(N));
}
}
  
// This code is contributed by Nidhi_biet

chevron_right


Output: 

3

Time Complexity: O(N), where N is the given number.

Efficient Approach: The idea is to observe that if N is not divisible by X initially, then only the subtraction will be carried throughout as after each subtraction N still wouldn’t be divisible by N. Also these operations will stop when N ≤ X, and the final value of N will be equal to N mod X.
So, for all the numbers from 2 to N, there are only two possible cases :

  1. No division operation occurs: For all these numbers, the final value will be equal to N mod X. N will become one in the end only if N mod X = 1. Clearly, for X = N – 1, and all divisors of N – 1, N mod X = 1 holds true.
  2. Division operation occurs more than once: Division operation will only occur for divisors on N. For each divisor of N say d, perform the division till N mod d != 0. If finally N mod d = 1, then this will be included in the answer else not (using the property derived from Case 1).

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to count the numbers
// which can convert N to 1
// using the given operation
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to count the numbers
// which can convert N to 1
// using the given operation
int countValues(int N)
{
  
    vector<int> div;
  
    // Store all the divisors of N
    for (int i = 2; i * i <= N; i++) {
  
        // If i is a divisor
        if (N % i == 0) {
            div.push_back(i);
  
            // If i is not equal to N / i
            if (N != i * i) {
                div.push_back(N / i);
            }
        }
    }
  
    int answer = 0;
  
    // Iterate through all the divisors of 
    // N - 1 and count them in answer
    for (int i = 1; i * i <= N - 1; i++) {
  
        // Check if N - 1 is a divisor
        // or not
        if ((N - 1) % i == 0) {
            if (i * i == N - 1)
                answer++;
            else
                answer += 2;
        }
    }
  
    // Iterate through all divisors and check
    // for N mod d = 1 or (N-1) mod d = 0
    for (auto d : div) {
        int K = N;
        while (K % d == 0)
            K /= d;
        if ((K - 1) % d == 0)
            answer++;
    }
    return answer;
}
  
// Driver code
int main()
{
    int N = 6;
  
    cout << countValues(N);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to count the numbers
// which can convert N to 1
// using the given operation
import java.util.*;
  
class GFG{
  
// Function to count the numbers
// which can convert N to 1
// using the given operation
static int countValues(int N)
{
    Vector<Integer> div = new Vector<>();
  
    // Store all the divisors of N
    for(int i = 2; i * i <= N; i++) 
    {
          
        // If i is a divisor
        if (N % i == 0)
        {
            div.add(i);
  
            // If i is not equal to N / i
            if (N != i * i)
            {
                div.add(N / i);
            }
        }
    }
  
    int answer = 0;
  
    // Iterate through all the divisors of 
    // N - 1 and count them in answer
    for(int i = 1; i * i <= N - 1; i++)
    {
          
        // Check if N - 1 is a divisor
        // or not
        if ((N - 1) % i == 0)
        {
            if (i * i == N - 1)
                answer++;
            else
                answer += 2;
        }
    }
  
    // Iterate through all divisors and check
    // for N mod d = 1 or (N-1) mod d = 0
    for(int d : div)
    {
        int K = N;
        while (K % d == 0)
            K /= d;
              
        if ((K - 1) % d == 0)
            answer++;
    }
    return answer;
}
  
// Driver code
public static void main(String[] args)
{
    int N = 6;
  
    System.out.print(countValues(N));
}
}
  
// This code is contributed by gauravrajput1

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to count the numbers
// which can convert N to 1
// using the given operation
using System;
using System.Collections.Generic;
  
class GFG{
  
// Function to count the numbers
// which can convert N to 1
// using the given operation
static int countValues(int N)
{
    List<int> div = new List<int>();
  
    // Store all the divisors of N
    for(int i = 2; i * i <= N; i++) 
    {
          
        // If i is a divisor
        if (N % i == 0)
        {
            div.Add(i);
  
            // If i is not equal to N / i
            if (N != i * i)
            {
                div.Add(N / i);
            }
        }
    }
  
    int answer = 0;
  
    // Iterate through all the divisors of 
    // N - 1 and count them in answer
    for(int i = 1; i * i <= N - 1; i++)
    {
          
        // Check if N - 1 is a divisor
        // or not
        if ((N - 1) % i == 0)
        {
            if (i * i == N - 1)
                answer++;
            else
                answer += 2;
        }
    }
  
    // Iterate through all divisors and check
    // for N mod d = 1 or (N-1) mod d = 0
    foreach(int d in div)
    {
        int K = N;
        while (K % d == 0)
            K /= d;
              
        if ((K - 1) % d == 0)
            answer++;
    }
    return answer;
}
  
// Driver code
public static void Main(String[] args)
{
    int N = 6;
  
    Console.Write(countValues(N));
}
}
  
// This code is contributed by Amit Katiyar

chevron_right


Output: 

3

Time Complexity: O(\sqrt{N})

competitive-programming-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.