Open In App

Index of the array which would be visited in Kth operation

Last Updated : 24 Aug, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given an array A[] of length N such that A[i] = i for all indices. Also, given two integers K and X. The task is to find the index of the array that would be visited in Kth operation. Considering the 0th index visited in first operation, the operation for visiting the rest of the indices which is to be performed (N-1) times is:

  • Let L = index of element visited last time and a variable j = (L+X)%N
  • Keep replacing j by (j+1)%N till such j is found that was not visited. Mark the index j as visited.

Examples :

Input: N=4, A={0, 1, 2, 3}, X=2, K=4
Output: 3
Explanation: In 1st operation, index 0 is visited. 
In operation 2, as 0 is the index visited last time. So, L=0 and hence j = (0+2)%4 = 2. Since index 2 was not visited before. Mark it as visited. 
In 3rd operation, j = (2+2)%4 = 0. Since 0th index is already visited, we update j as (0+1)%4 = 1. We mark 1 as visited.
In 4th operation, j = (1+2)%4 = 3. Mark 3rd index as visited. So, after 3 operations 3rd index would be visited.

Input: N=8, A={0, 1, 2, 3, 4, 5, 6, 7}, X = 8, K = 5
Output:  4

Naive Approach:

A simple approach to solve the problem would be to keep performing the operation as said in the problem.

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

Efficient Approach:

The problem can be solved based on the following observation:

Let X and Y be 2 positive integers having their gcd Z. Let X = xZ and Y=yZ. Then the integers Y%X, 2Y%X … (x-1)Y%X contain 0, Z, 2Z…(x-1)Z each exactly once. For example consider X=12 and Y = 9, Z = 3 and z = 4, y = 3. So, 0, Y%X, 2Y%X, 3Y%X are 0, 9, 6 and 3. i.e. it can be seen all multiples of Z between 0 and X have occurred exactly once.

Hence it can be seen:

  • The index that would be visited in ith operation  = (i-1)X%N. 
  • The index that would be visited in (x+i)th operation  = 1+((i-1)X%N)
  • Following the same pattern, Kth index to be visited would be (K-1)/x + ((K-1)X%N). (x is the number of multiples of gcd of X and N from 0 to N).

Following are the steps for the above approach:

  • Decrement the value of K by 1, as 1st operation is already done i.e. 0th index is already visited.
  • Initialize a variable say gcd and compute the gcd of N and X.
  • Initialize a variable say x and find the value of x which is the number of multiples of gcd of X and N from 0 to N, x = N/gcd.
  • Calculate the answer using the derived formula, answer = X*K%N + K/x.

Below is the implementation of the above approach.

C++




// C++ code to implement the approach
 
#include <bits/stdc++.h>
using namespace std;
#define ll long long
 
// Function to Find index of the array which
// would be visited in K-th operation
ll solve(ll N, ll A[], ll K, ll X)
{
    // Reducing the value of K by 1, as
    // 1st operation is alrady done i.e. 0th
    // index is already visited
    K--;
 
    // Computing gcd of N and X
    ll gcd = __gcd(N, X);
 
    // Calculating the answer using the derived
    // formula
    ll x = N / gcd;
    ll answer = (long long)X * K % N + K / x;
 
    // Returning the answer
    return answer;
}
 
// Driver code
int main()
{
    ll N = 4, X = 2, K = 4;
    ll A[] = { 0, 1, 2, 3 };
 
    // Function call
    ll answer = solve(N, A, K, X);
    cout << answer << endl;
    return 0;
}


Java




import java.util.*;
 
public class Main {
    static long solve(long N, long[] A, long K, long X) {
        // Reducing the value of K by 1, as
        // 1st operation is already done i.e. 0th
        // index is already visited
        K--;
 
        // Computing gcd of N and X
        long gcd = gcd(N, X);
 
        // Calculating the answer using the derived
        // formula
        long x = N / gcd;
        long answer = (long) (X * K % N + K / x);
 
        // Returning the answer
        return answer;
    }
 
    static long gcd(long a, long b) {
        if (b == 0)
            return a;
        return gcd(b, a % b);
    }
 
    public static void main(String[] args) {
        long N = 4, X = 2, K = 4;
        long[] A = { 0, 1, 2, 3 };
 
        // Function call
        long answer = solve(N, A, K, X);
        System.out.println(answer);
    }
}
//contributed by tushar rokade


Python3




import math
 
# Function to Find index of the array which
# would be visited in K-th operation
 
 
def solve(N, A, K, X):
    # Reducing the value of K by 1, as
    # 1st operation is alrady done i.e. 0th
    # index is already visited
    K -= 1
 
    # Computing gcd of N and X
    gcd = math.gcd(N, X)
 
    # Calculating the answer using the derived
    # formula
    x = N // gcd
    answer = (X * K % N) + (K // x)
 
    # Returning the answer
    return answer
 
 
# Driver code
N = 4
X = 2
K = 4
A = [0, 1, 2, 3]
 
# Function call
answer = solve(N, A, K, X)
print(answer)


C#




using System;
 
class GFG {
    // Function to find index of the array which would be
    // visited in K-th operation
    static long Solve(long N, long[] A, long K, long X)
    {
        // Reducing the value of K by 1, as 1st operation is
        // already done i.e. 0th index is already visited
        K--;
 
        // Computing gcd of N and X
        long gcd = GCD(N, X);
 
        // Calculating the answer using the derived formula
        long x = N / gcd;
        long answer = (X * K % N + K / x);
 
        // Returning the answer
        return answer;
    }
 
    // Function to compute the Greatest Common Divisor (GCD)
    // of two numbers
    static long GCD(long a, long b)
    {
        if (b == 0)
            return a;
        return GCD(b, a % b);
    }
 
    static void Main(string[] args)
    {
        long N = 4, X = 2, K = 4;
        long[] A = { 0, 1, 2, 3 };
 
        // Function call
        long answer = Solve(N, A, K, X);
        Console.WriteLine(answer);
    }
}
 
// This code is contributed by rambabuguphka


Javascript




// Function to Find index of the array which
// would be visited in K-th operation
function solve(N, A, K, X) {
    // Reducing the value of K by 1, as
    // 1st operation is already done i.e. 0th
    // index is already visited
    K--;
 
    // Computing gcd of N and X
    function gcd(a, b) {
        if (b === 0) return a;
        return gcd(b, a % b);
    }
 
    let gcdValue = gcd(N, X);
 
    // Calculating the answer using the derived formula
    let x = N / gcdValue;
    let answer = (X * K) % N + Math.floor(K / x);
 
    // Returning the answer
    return answer;
}
 
// Driver code
    let N = 4;
    let X = 2;
    let K = 4;
    let A = [0, 1, 2, 3];
 
    // Function call
    let answer = solve(N, A, K, X);
    console.log(answer);


Output

3



Time Complexity : O(log(min(X, N)))
Auxiliary Space : O(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads