Open In App

Minimum count of starting elements chosen to visit whole Array depending on ratio of values to positions

Given an array arr[], the task is to calculate the minimum number of starting elements to choose from such that all other elements of the array can be visited. An element can be visited by another element only if the ratio of their values is not inversely proportional to the position of those elements in the array. The position of an element in the array is defined as its index + 1
Example:

Input: arr = [1, 2, 3, 4]
Output: 1
Explanation: Any element can be chosen to start visiting other elements. For the first two elements, (1/2) is not equal to (2/1). Similarly for second and third element, (2/3) is not equal to (3/2). Similar is the case for other pairs.



Input: arr = [6, 3, 2]
Output: 3
Explanation: Here for every pair, ratio of the elements is equal to inverse ratio of their positions:

  • (6/3) is equal to (2/1)
  • (3/2) is equal to (3/2)
  • (6/2) is equal to (3/1)
    No element can be visited by another element
 

Approach: The given problem can be solved by making the observation that the elements can visit each other if arr[i]/arr[j] != j/i, where j and i are positions of elements. The formula can be re-written as arr[i]*i != arr[j]*j. Below steps can be followed:



Below is the implementation of the above approach:




// C++ implementation for the above approach
#include <iostream>
using namespace std;
 
// Function to calculate minimum number
// of elements to visit initially
int minimumStudentsToInform(int A[], int N)
{
    // Multiply array elements
    // with their indices
    for (int i = 0; i < N; i++) {
        A[i] = A[i] * (i + 1);
    }
    for (int i = 1; i < N; i++) {
 
        // If any two elements can visit
        // each other then all elements
        // in the array can be visited
        if (A[i] != A[i - 1]) {
 
            return 1;
        }
    }
 
    // All elements should be
    // visited initially
    return N;
}
 
int main()
{
    int A[] = { 6, 3, 2 };
    int N = sizeof(A) / sizeof(int);
    cout << minimumStudentsToInform(A, N);
    return 0;
}




// Java implementation for the above approach
 
public class GFG {
     
    // Function to calculate minimum number
    // of elements to visit initially
    static int minimumStudentsToInform(int A[], int N)
    {
        // Multiply array elements
        // with their indices
        for (int i = 0; i < N; i++) {
            A[i] = A[i] * (i + 1);
        }
        for (int i = 1; i < N; i++) {
     
            // If any two elements can visit
            // each other then all elements
            // in the array can be visited
            if (A[i] != A[i - 1]) {
     
                return 1;
            }
        }
     
        // All elements should be
        // visited initially
        return N;
    }
     
    public static void main(String[] args)
    {
        int A[] = { 6, 3, 2 };
        int N = A.length;
        System.out.println(minimumStudentsToInform(A, N));
    }
}
 
// This code is contributed by AnkThon




# Python implementation for the above approach
 
# Function to calculate minimum number
# of elements to visit initially
def minimumStudentsToInform( A, N):
   
  # Multiply array elements
  # with their indices
  for i in range(N):
    A[i] = A[i] * (i + 1)
     
  for i in range(1, N):
     
    # If any two elements can visit
    # each other then all elements
    # in the array can be visited
    if (A[i] != A[i - 1]):
      return 1
 
  # All elements should be
  # visited initially
  return N
 
A = [ 6, 3, 2 ]
N = len(A)
print(minimumStudentsToInform(A, N))
# This code is contributed by rohitsingh07052




// C# implementation for the above approach
using System;
 
public class GFG
{
     
    // Function to calculate minimum number
    // of elements to visit initially
    static int minimumStudentsToInform(int []A, int N)
    {
       
        // Multiply array elements
        // with their indices
        for (int i = 0; i < N; i++) {
            A[i] = A[i] * (i + 1);
        }
        for (int i = 1; i < N; i++) {
     
            // If any two elements can visit
            // each other then all elements
            // in the array can be visited
            if (A[i] != A[i - 1]) {
     
                return 1;
            }
        }
     
        // All elements should be
        // visited initially
        return N;
    }
     
  // Driver code
    public static void Main(string[] args)
    {
        int []A = { 6, 3, 2 };
        int N = A.Length;
        Console.WriteLine(minimumStudentsToInform(A, N));
    }
}
 
// This code is contributed by AnkThon




<script>
// Javascript implementation for the above approach
 
// Function to calculate minimum number
// of elements to visit initially
function minimumStudentsToInform(A, N)
{
 
  // Multiply array elements
  // with their indices
  for (let i = 0; i < N; i++) {
    A[i] = A[i] * (i + 1);
  }
  for (let i = 1; i < N; i++)
  {
   
    // If any two elements can visit
    // each other then all elements
    // in the array can be visited
    if (A[i] != A[i - 1]) {
      return 1;
    }
  }
 
  // All elements should be
  // visited initially
  return N;
}
 
let A = [6, 3, 2];
let N = A.length;
document.write(minimumStudentsToInform(A, N));
 
// This code is contributed by saurabh_jaiswal.
</script>

Output
3

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


Article Tags :