📜  选择访问整个数组的起始元素的最小数量取决于值与位置的比率

📅  最后修改于: 2022-05-13 01:57:49.147000             🧑  作者: Mango

选择访问整个数组的起始元素的最小数量取决于值与位置的比率

给定一个数组arr[],任务是计算可供选择的最小起始元素数,以便可以访问数组的所有其他元素。只有当它们的值的比率与数组中这些元素的位置不成反比时,一个元素才能被另一个元素访问。数组中元素的位置定义为它的索引 + 1
例子:

方法:给定的问题可以通过观察元素可以相互访问来解决,如果arr[i]/arr[j] != j/i ,其中ji是元素的位置。该公式可以重写为arr[i]*i != arr[j]*j 。可以按照以下步骤进行:

  • 迭代数组并将每个元素乘以它的位置(索引 + 1)
  • 从索引 1 再次遍历数组并检查公式是否适用于每个元素及其前一个元素
  • 如果公式满足任何元素,则返回 1,因为将访问所有元素
  • 否则,返回N ,因为没有元素可以相互访问,并且所有元素都需要最初访问

下面是上述方法的实现:

C++
// C++ implementation for the above approach
#include 
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
// 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


Python3
# 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#
// 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


Javascript


输出
3

时间复杂度: O(N)
辅助空间: O(1)