Tuesday, 7 Dec 2021

# 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 + 1Example:Input: arr = [1, 2, 3, 4]Output: 1Explanation: 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.Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.Input: arr = [6, 3, 2]Output: 3Explanation: 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 elementApproach: 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:Iterate the array and multiply every element to its position (index + 1)Traverse the array again from index 1 and check if the formula holds true for every element with its previous elementIf the formula satisfies for any element then return 1 as all elements will be visitedOtherwise, return N as no elements can visit each other and all of them need to be visited initiallyBelow is the implementation of the above approach:C++#include using namespace std;  int minimumStudentsToInform(int A[], int N){            for (int i = 0; i < N; i++) {        A[i] = A[i] * (i + 1);    }    for (int i = 1; i < N; i++) {                                  if (A[i] != A[i - 1]) {              return 1;        }    }              return N;}  int main(){    int A[] = { 6, 3, 2 };    int N = sizeof(A) / sizeof(int);    cout