Minimum jumps to cover given elements from circular sequence of 1 to n
Given a circular connected array consisting of elements from 1 to n. Starting from 1st element, you can jump from any element to any other element which is x distance apart and in the right direction, the task is to find the minimum jumps required to cover all the given elements of list m if you can choose any value of x.
Examples:
Input: n = 5, m = { 1, 2, 4 }
Output: 2
Explanation: circular array will be like this 1, 2, 3, 4, 5, 1, 2, 3, 4, ….
for x = 3, u can jump from 1 -> 4 and 4 -> 2 thus minimum jumps required will be 2.Input: n = 6, m = { 3, 4 }
Output: 3
Explanation: 1 -> 2 -> 3 -> 4
Approach: The given problem can be solved by using the Greedy Approach. Follow the steps below to solve the problem:
- Initialize an array m[] which consists of elements to cover.
- Take a variable mx to store the maximum element that needs to be covered. mx – 1 also denotes the maximum possible jumps required to cover all given elements.
- check if m.size() = 1 ?
- if yes then check if m[0] = 1 ? if yes then output “0” else output “1”.
- Run a loop from i = 1 to n, where i = size of jump
- j = 1, stores current position
- For every i: calculate the number of jumps required to cover all elements and store minimum jumps.
- Return minimum jumps.
Below is the implementation of the above approach:
C++
// C++ code to implement the approach #include <bits/stdc++.h> using namespace std; // Function to calculate minimum // number of jumps int Minimum_jumps( int N, int m[], int n) { if (n == 1) { if (m[0] == 1) return 0; else return 1; } // Set to store copy of elements // of array m[] unordered_set< int > s; // Store maximum element that need to // be cover also equal to maximum // possible required jumps int mx = 0; for ( int i = 0; i < n; i++) { s.insert(m[i]); mx = max(mx, m[i]); } // Variable to store final ans. int mnans = N; // Loop from jump size 1 to N-1 for ( int i = 0; i < N; i++) { unordered_set< int > s1 = s; // Store no. of jumps int ans = 0; // Current position int j = 1; int z = n; while (z > 0 && ans <= mx) { if (j > N) j = j % N; // To check whether element // j need to be cover or not if (s1.find(j) != s1.end()) { z--; s1.erase(j); } j += i; ans++; } ans--; // Store minimum number of jumps mnans = min(mnans, ans); } return mnans; } // Driver code int main() { int N = 6; // Elements to visit int m[] = { 3, 4 }; int n = sizeof (m) / sizeof (m[0]); // Function call cout << Minimum_jumps(N, m, n) << endl; return 0; } |
Java
// Java code to implement the approach import java.util.HashSet; class GFG { // Function to calculate minimum // number of jumps static int Minimum_jumps( int N, int [] m, int n) { if (n == 1 ) { if (m[ 0 ] == 1 ) return 0 ; else return 1 ; } // Set to store copy of elements // of array m[] HashSet<Integer> s = new HashSet<>(); // Store maximum element that need to // be cover also equal to maximum // possible required jumps int mx = 0 ; for ( int i = 0 ; i < n; i++) { s.add(m[i]); mx = Math.max(mx, m[i]); } // Variable to store final ans. int mnans = N; // Loop from jump size 1 to N-1 for ( int i = 0 ; i < N; i++) { HashSet<Integer> s1 = (HashSet<Integer>) s.clone(); // Store no. of jumps int ans = 0 ; // Current position int j = 1 ; int z = n; while (z > 0 && ans <= mx) { if (j > N) j = j % N; // To check whether element // j need to be cover or not if (s1.contains(j)) { z--; s1.remove(j); } j += i; ans++; } ans--; // Store minimum number of jumps mnans = Math.min(mnans, ans); } return mnans; } // Driver code public static void main(String[] args) { int N = 6 ; // Elements to visit int [] m = { 3 , 4 }; int n = m.length; // Function call System.out.println(Minimum_jumps(N, m, n)); } } |
Python3
# Function to calculate minimum # number of jumps def Minimum_jumps(N, m, n): if n = = 1 : if m[ 0 ] = = 1 : return 0 else : return 1 # Set to store copy of elements # of array m[] s = set () # Store maximum element that need to # be cover also equal to maximum # possible required jumps mx = 0 for i in range (n): s.add(m[i]) mx = max (mx, m[i]) # Variable to store final ans. mnans = N # Loop from jump size 1 to N-1 for i in range (N): s1 = set (s) # Store no. of jumps ans = 0 # Current position j = 1 z = n while z > 0 and ans < = mx: if j > N: j = j % N # To check whether element # j need to be cover or not if j in s1: z - = 1 s1.remove(j) j + = i ans + = 1 ans - = 1 # Store minimum number of jumps mnans = min (mnans, ans) return mnans # Driver code N = 6 # Elements to visit m = [ 3 , 4 ] n = len (m) # Function call print (Minimum_jumps(N, m, n)) |
C#
// C# code to implement the approach using System; using System.Collections.Generic; public class GFG{ // Function to calculate minimum // number of jumps static int Minimum_jumps( int N, int [] m, int n) { if (n == 1) { if (m[0] == 1) return 0; else return 1; } // Set to store copy of elements // of array m[] HashSet< int > s = new HashSet< int >(); // Store maximum element that need to // be cover also equal to maximum // possible required jumps int mx = 0; for ( int i = 0; i < n; i++) { s.Add(m[i]); mx = Math.Max(mx, m[i]); } // Variable to store final ans. int mnans = N; // Loop from jump size 1 to N-1 for ( int i = 0; i < N; i++) { HashSet< int > s1 = new HashSet< int >(); s1.UnionWith(s); // Store no. of jumps int ans = 0; // Current position int j = 1; int z = n; while (z > 0 && ans <= mx) { if (j > N) j = j % N; // To check whether element // j need to be cover or not if (s1.Contains(j)) { z--; s1.Remove(j); } j += i; ans++; } ans--; // Store minimum number of jumps mnans = Math.Min(mnans, ans); } return mnans; } // Driver code static public void Main (){ int N = 6; // Elements to visit int [] m = { 3, 4 }; int n = m.Length; // Function call Console.Write(Minimum_jumps(N, m, n)); } } |
Javascript
// Function to calculate minimum // number of jumps function Minimum_jumps(N, m, n) { if (n == 1) { if (m[0] == 1) { return 0; } else { return 1; } } // Set to store copy of elements // of array m[] let s = new Set(); // Store maximum element that need to // be cover also equal to maximum // possible required jumps let mx = 0; for (let i = 0; i < n; i++) { s.add(m[i]); mx = Math.max(mx, m[i]); } // Variable to store final ans. let mnans = N; // Loop from jump size 1 to N-1 for (let i = 0; i < N; i++) { let s1 = new Set(s); // Store no. of jumps let ans = 0; // Current position let j = 1; let z = n; while (z > 0 && ans <= mx) { if (j > N) { j = j % N; } // To check whether element // j need to be cover or not if (s1.has(j)) { z -= 1; s1. delete (j); } j += i; ans += 1; } ans -= 1; // Store minimum number of jumps mnans = Math.min(mnans, ans); } return mnans; } // Driver code let N = 6; // Elements to visit let m = [3, 4]; let n = m.length; // Function call console.log(Minimum_jumps(N, m, n)); |
3
Time Complexity: O(NlogN), where N = size of given circular array
Auxiliary space: O(n), where n = no. of elements need to cover
Please Login to comment...