Skip to content
Related Articles
Open in App
Not now

Related Articles

Find Nth number in a sequence which is not a multiple of a given number

Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 20 Mar, 2023
Improve Article
Save Article

Given four integers A, N, L and R, the task is to find the N th number in a sequence of consecutive integers from L to R which is not a multiple of A. It is given that the sequence contain at least N numbers which are not divisible by A and the integer A is always greater than 1.

Examples:  

Input: A = 2, N = 3, L = 1, R = 10 
Output:
Explanation: 
The sequence is 1, 2, 3, 4, 5, 6, 7, 8, 9 and 10. Here 5 is the third number which is not a multiple of 2 in the sequence.

Input: A = 3, N = 6, L = 4, R = 20 
Output: 11 
Explanation : 
11 is the 6th number which is not a multiple of 3 in the sequence. 

Naive Approach: The naive approach is to iterate over the range [L, R] in a loop to find the Nth number. The steps are:  

  1. Initialize the count of non-multiple number and current number to 0.
  2. Iterate over the range [L, R] until the count of the non-multiple number is not equal to N.
  3. Increment the count of the non-multiple number by 1, If the current number is not divisible by A.

Below is the implementation of the above approach:  

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find Nth number not a
// multiple of A in the range [L, R]
void count_no (int A, int N, int L, int R)
{
     
    // To store the count
    int count = 0;
    int i = 0;
 
    // To check all the nos in range
    for(i = L; i < R + 1; i++)
    {
    if (i % A != 0)
        count += 1;
         
    if (count == N)
        break;
    }
    cout << i;
}
 
// Driver code
int main()
{
     
    // Given values of A, N, L, R
    int A = 3, N = 6, L = 4, R = 20;
     
    // Function Call
    count_no (A, N, L, R);
    return 0;
}
 
// This code is contributed by mohit kumar 29


Java




// Java program for the above approach
import java.util.*;
import java.io.*;
 
class GFG{
     
// Function to find Nth number not a
// multiple of A in the range [L, R]
static void count_no (int A, int N,
                      int L, int R)
{
 
    // To store the count
    int count = 0;
    int i = 0;
 
    // To check all the nos in range
    for(i = L; i < R + 1; i++)
    {
        if (i % A != 0)
            count += 1;
     
        if (count == N)
            break;
    }
    System.out.println(i);
}
 
// Driver code
public static void main(String[] args)
{
     
    // Given values of A, N, L, R
    int A = 3, N = 6, L = 4, R = 20;
 
    // Function call
    count_no (A, N, L, R);
}
}
 
// This code is contributed by sanjoy_62


Python3




# Python3 program for the above approach
 
# Function to find Nth number not a
# multiple of A in the range [L, R]
def count_no (A, N, L, R):
 
    # To store the count
    count = 0
 
    # To check all the nos in range
    for i in range ( L, R + 1 ):
        if ( i % A != 0 ):
            count += 1
 
        if ( count == N ):
            break
    print ( i )
     
# Given values of A, N, L, R
A, N, L, R = 3, 6, 4, 20
 
# Function Call
count_no (A, N, L, R)


C#




// C# program for the above approach
using System;
 
class GFG{
     
// Function to find Nth number not a
// multiple of A in the range [L, R]
static void count_no (int A, int N,
                      int L, int R)
{
     
    // To store the count
    int count = 0;
    int i = 0;
 
    // To check all the nos in range
    for(i = L; i < R + 1; i++)
    {
        if (i % A != 0)
            count += 1;
     
        if (count == N)
            break;
    }
    Console.WriteLine(i);
}
 
// Driver code
public static void Main()
{
     
    // Given values of A, N, L, R
    int A = 3, N = 6, L = 4, R = 20;
 
    // Function call
    count_no (A, N, L, R);
}
}
 
// This code is contributed by sanjoy_62


Javascript




<script>
 
// Javascript Program to implement
// the above approach
 
// Function to find Nth number not a
// multiple of A in the range [L, R]
function count_no (A, N, L, R)
{
  
    // To store the count
    let count = 0;
    let i = 0;
  
    // To check all the nos in range
    for(i = L; i < R + 1; i++)
    {
        if (i % A != 0)
            count += 1;
      
        if (count == N)
            break;
    }
   document.write(i);
}
 
// Driver Code
 
    // Given values of A, N, L, R
    let A = 3, N = 6, L = 4, R = 20;
  
    // Function call
    count_no (A, N, L, R);
     
    // This code is contributed by chinmoy1997pal.
</script>


Output: 

11

 

Time Complexity: O(R – L) 
Auxiliary Space: O(1) 

Efficient Approach: 
The key observation is that there are A – 1 numbers that are not divisible by A in the range [1, A – 1]. Similarly, there are A – 1 numbers not divisible by A in range [A + 1, 2 * A – 1], [2 * A + 1, 3 * A – 1] and so on. 
With the help of this observation, the Nth number which is not divisible by A will be: 

N + \left \lfloor \frac{N - 1}{A - 1} \right \rfloor

To find the value in the range [ L, R ], we need to shift the origin from ‘0’ to ‘L – 1’, thus we can say that the Nth number which is not divisible by A in the range will be : 

(L - 1) + \left \lfloor \frac{N - 1}{A - 1} \right \rfloor

However there is an edge case, when the value of ( L – 1 ) + N + floor( ( N – 1 ) / ( A – 1 ) ) itself turns out to be multiple of a ‘A’, in that case Nth number will be : 

(L - 1) + \left \lfloor \frac{N - 1}{A - 1} \right \rfloor + 1

Below is the implementation of the above approach: 

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find Nth number
// not a multiple of A in range [L, R]
void countNo(int A, int N, int L, int R)
{
     
    // Calculate the Nth no
    int ans = L - 1 + N + floor((N - 1) /
                                (A - 1));
     
    // Check for the edge case
    if (ans % A == 0)
    {
        ans = ans + 1;
    }
    cout << ans << endl;
}
 
// Driver Code
int main()
{
     
    // Input parameters
    int A = 5, N = 10, L = 4, R = 20;
     
    // Function Call
    countNo(A, N, L, R);
     
    return 0;
}
 
// This code is contributed by avanitrachhadiya2155


Java




// Java program for the above approach
import java.io.*;
 
class GFG
{
 
  // Function to find Nth number
  // not a multiple of A in range [L, R]
  static void countNo(int A, int N, int L, int R)
  {
 
    // Calculate the Nth no
    int ans = L - 1 + N + (int)Math.floor((N - 1) / (A - 1));
 
    // Check for the edge case
    if (ans % A == 0)
    {
      ans = ans + 1;
    }
    System.out.println(ans);   
  }
 
  // Driver Code
  public static void main (String[] args)
  {
 
    // Input parameters
    int A = 5, N = 10, L = 4, R = 20;
 
    // Function Call
    countNo(A, N, L, R);   
  }
}
 
//  This code is contributed by rag2127


Python3




# Python3 program for the above approach
import math
 
# Function to find Nth number
# not a multiple of A in range [L, R]
def countNo (A, N, L, R):
 
    # Calculate the Nth no
    ans = L - 1 + N \
          + math.floor( ( N - 1 ) / ( A - 1 ) )
     
    # Check for the edge case
    if ans % A == 0:
        ans = ans + 1;
    print(ans)
 
# Input parameters
A, N, L, R = 5, 10, 4, 20
 
# Function Call
countNo(A, N, L, R)


C#




// C# program for the above approach
using System;
class GFG {
 
  // Function to find Nth number
  // not a multiple of A in range [L, R]
  static void countNo(int A, int N, int L, int R)
  {
 
    // Calculate the Nth no
    int ans = L - 1 + N + ((N - 1) / (A - 1));
 
    // Check for the edge case
    if (ans % A == 0)
    {
      ans = ans + 1;
    }
    Console.WriteLine(ans);
  }
 
  // Driver code
  static void Main()
  {
 
    // Input parameters
    int A = 5, N = 10, L = 4, R = 20;
 
    // Function Call
    countNo(A, N, L, R);
  }
}
 
// This code is contributed by divyesh072019.


Javascript




<script>
 
// Javascript program for
// the above approach
 
// Function to find Nth number
// not a multiple of A in range [L, R]
function countNo(A, N, L, R)
{
 
    // Calculate the Nth no
    var ans = L - 1 + N +
      Math.floor((N - 1) / (A - 1));
     
    // Check for the edge case
    if (ans % A == 0)
    {
        ans = ans + 1;
    }
    document.write(ans);   
}
 
// Driver code
 
// Input parameters
var A = 5, N = 10, L = 4, R = 20;
 
// Function Call
countNo(A, N, L, R); 
 
// This code is contributed by Khushboogoyal499
    
</script>


Output: 

16

 

Time Complexity: O(1) 
Auxiliary Space: O(1)

Another Approach:

1. We initialize n to the position of the required number in the sequence and x to the given number which should not be a multiple of the required number. In this example, we have taken n as 10 and x as 3.
2. We initialize a variable count to 0, which will keep track of the number of non-multiples encountered so far.
3. We use a for loop that starts from 1 and continues until we find the nth non-multiple of x. Inside the for loop, we check whether the current number i is a multiple of x.
4. If i is not a multiple of x, we set the variable num to the value of i and increment the count variable.
5. When count becomes equal to n, we exit the for loop and print the value of num as the nth number in the sequence which is not a multiple of x.

C++




#include <bits/stdc++.h>
using namespace std;
int main(){
    int n = 10; // for example
    int x = 3; // for example
    int count = 0;
    int i, num;
 
    for (i = 1; count < n; i++) {
        if (i % x != 0) {
            num = i;
            count++;
        }
    }
    cout<< "The " << n << "th number in the sequence which is not a multiple of "
      << x << " is " << num << endl;
    return 0;
}


C




#include <stdio.h>
 
int main()
{
    int n = 10; // for example
    int x = 3; // for example
    int count = 0;
    int i, num;
 
    for (i = 1; count < n; i++) {
        if (i % x != 0) {
            num = i;
            count++;
        }
    }
 
    printf("The %dth number in the sequence which is not a multiple of %d is %d\n", n, x, num);
 
    return 0;
}


Output

The 10th number in the sequence which is not a multiple of 3 is 14

Time Complexity: O(N), where N is the position of the required number in the sequence.
Space Complexity: O(1), as we are not using any extra space.

 


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!