Minimum steps to reach end of array under constraints - GeeksforGeeks
Given an array containing one digit numbers only, assuming we are standing at first index, we need to reach to end of array using minimum number of steps where in one step, we can jump to neighbor indices or can jump to a position with same value.
In other words, if we are at index i, then in one step you can reach to, arr[i-1] or arr[i+1] or arr[K] such that arr[K] = arr[i] (value of arr[K] is same as arr[i])
http://www.geeksforgeeks.org/minimum-step-reach-one/
Given a positive number N, we need to reach to 1 in minimum number of steps where a step is defined as converting N to (N-1) or converting N to its one of the bigger divisor.
Formally, if we are at N, then in 1 step we can reach to (N – 1) or if N = u*v then we can reach to max(u, v) where u > 1 and v > 1.
We can solve this problem using breadth first search because it works level by level so we will reach to 1 in minimum number of steps where next level for N contains (N – 1) and bigger proper factors of N.
Complete BFS procedure will be as follows, First we will push N with steps 0 into the data queue then at each level we will push their next level elements with 1 step more than its previous level elements. In this way when 1 will be popped out from queue, it will contain minimum number of steps with it, which will be our final result.
In below code a queue of a structure of ‘data’ type is used which stores value and steps from N in it, another set of integer type is used to save ourselves from pushing the same element more than once which can lead to an infinite loop. So at each step, we push the value into set after pushing that into the queue so that the value won’t be visited more than once.
Read full article from Minimum steps to reach end of array under constraints - GeeksforGeeks
Given an array containing one digit numbers only, assuming we are standing at first index, we need to reach to end of array using minimum number of steps where in one step, we can jump to neighbor indices or can jump to a position with same value.
In other words, if we are at index i, then in one step you can reach to, arr[i-1] or arr[i+1] or arr[K] such that arr[K] = arr[i] (value of arr[K] is same as arr[i])
Input : arr[] = {5, 4, 2, 5, 0}
Output : 2
Explanation : Total 2 step required.
We start from 5(0), in first step jump to next 5
and in second step we move to value 0 (End of arr[]).
Input : arr[] = [0, 1, 2, 3, 4, 5, 6, 7, 5, 4,
3, 6, 0, 1, 2, 3, 4, 5, 7]
Output : 5
Explanation : Total 5 step required.
0(0) -> 0(12) -> 6(11) -> 6(6) -> 7(7) ->
(18)
(inside parenthesis indices are shown)
This problem can be solved using BFS. We can consider the given array as unweighted graph where every vertex has two edges to next and previous array elements and more edges to array elements with same values. Now for fast processing of third type of edges, we keep 10 vectors which store all indices where digits 0 to 9 are present. In above example, vector corresponding to 0 will store [0, 12], 2 indices where 0 has occurred in given array.
Another Boolean array is used, so that we don’t visit same index more than once. As we are using BFS and BFS proceeds level by level, optimal minimum steps are guaranteed.
Another Boolean array is used, so that we don’t visit same index more than once. As we are using BFS and BFS proceeds level by level, optimal minimum steps are guaranteed.
int getMinStepToReachEnd(int arr[], int N){ // visit boolean array checks whether current index // is previously visited bool visit[N]; // distance array stores distance of current // index from starting index int distance[N]; // digit vector stores indicies where a // particular number resides vector<int> digit[10]; // In starting all index are unvisited memset(visit, false, sizeof(visit)); // storing indices of each number in digit vector for (int i = 1; i < N; i++) digit[arr[i]].push_back(i); // for starting index distance will be zero distance[0] = 0; visit[0] = true; // Creating a queue and inserting index 0. queue<int> q; q.push(0); // loop untill queue in not empty while(!q.empty()) { // Get an item from queue, q. int idx = q.front(); q.pop(); // If we reached to last index break from loop if (idx == N-1) break; // Find value of dequeued index int d = arr[idx]; // looping for all indices with value as d. for (int i = 0; i<digit[d].size(); i++) { int nextidx = digit[d][i]; if (!visit[nextidx]) { visit[nextidx] = true; q.push(nextidx); // update the distance of this nextidx distance[nextidx] = distance[idx] + 1; } } // clear all indices for digit d, because all // of them are processed digit[d].clear(); // checking condition for previous index if (idx-1 >= 0 && !visit[idx - 1]) { visit[idx - 1] = true; q.push(idx - 1); distance[idx - 1] = distance[idx] + 1; } // checking condition for next index if (idx + 1 < N && !visit[idx + 1]) { visit[idx + 1] = true; q.push(idx + 1); distance[idx + 1] = distance[idx] + 1; } } // N-1th position has the final result return distance[N - 1];}http://www.geeksforgeeks.org/minimum-step-reach-one/
Given a positive number N, we need to reach to 1 in minimum number of steps where a step is defined as converting N to (N-1) or converting N to its one of the bigger divisor.
Formally, if we are at N, then in 1 step we can reach to (N – 1) or if N = u*v then we can reach to max(u, v) where u > 1 and v > 1.
We can solve this problem using breadth first search because it works level by level so we will reach to 1 in minimum number of steps where next level for N contains (N – 1) and bigger proper factors of N.
Complete BFS procedure will be as follows, First we will push N with steps 0 into the data queue then at each level we will push their next level elements with 1 step more than its previous level elements. In this way when 1 will be popped out from queue, it will contain minimum number of steps with it, which will be our final result.
In below code a queue of a structure of ‘data’ type is used which stores value and steps from N in it, another set of integer type is used to save ourselves from pushing the same element more than once which can lead to an infinite loop. So at each step, we push the value into set after pushing that into the queue so that the value won’t be visited more than once.
int minStepToReachOne(int N){ queue<data> q; q.push(data(N, 0)); // set is used to visit numbers so that they // won't be pushed in queue again set<int> st; // loop untill we reach to 1 while (!q.empty()) { data t = q.front(); q.pop(); // if current data value is 1, return its // steps from N if (t.val == 1) return t.steps; // check curr - 1, only if it not visited yet if (st.find(t.val - 1) == st.end()) { q.push(data(t.val - 1, t.steps + 1)); st.insert(t.val - 1); } // loop from 2 to sqrt(value) for its divisors for (int i = 2; i*i <= t.val; i++) { // check divisor, only if it is not visited yet // if i is divisor of val, then val / i will // be its bigger divisor if (t.val % i == 0 && st.find(t.val / i) == st.end()) { q.push(data(t.val / i, t.steps + 1)); st.insert(t.val / i); } } } }