Minimum jumps to cover given elements from circular sequence of 1 to n
Last Updated :
10 Mar, 2023
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++
#include <bits/stdc++.h>
using namespace std;
int Minimum_jumps( int N, int m[], int n)
{
if (n == 1) {
if (m[0] == 1)
return 0;
else
return 1;
}
unordered_set< int > s;
int mx = 0;
for ( int i = 0; i < n; i++) {
s.insert(m[i]);
mx = max(mx, m[i]);
}
int mnans = N;
for ( int i = 0; i < N; i++) {
unordered_set< int > s1 = s;
int ans = 0;
int j = 1;
int z = n;
while (z > 0 && ans <= mx) {
if (j > N)
j = j % N;
if (s1.find(j) != s1.end()) {
z--;
s1.erase(j);
}
j += i;
ans++;
}
ans--;
mnans = min(mnans, ans);
}
return mnans;
}
int main()
{
int N = 6;
int m[] = { 3, 4 };
int n = sizeof (m) / sizeof (m[0]);
cout << Minimum_jumps(N, m, n) << endl;
return 0;
}
|
Java
import java.util.HashSet;
class GFG
{
static int Minimum_jumps( int N, int [] m, int n) {
if (n == 1 ) {
if (m[ 0 ] == 1 )
return 0 ;
else
return 1 ;
}
HashSet<Integer> s = new HashSet<>();
int mx = 0 ;
for ( int i = 0 ; i < n; i++) {
s.add(m[i]);
mx = Math.max(mx, m[i]);
}
int mnans = N;
for ( int i = 0 ; i < N; i++) {
HashSet<Integer> s1 = (HashSet<Integer>) s.clone();
int ans = 0 ;
int j = 1 ;
int z = n;
while (z > 0 && ans <= mx) {
if (j > N)
j = j % N;
if (s1.contains(j)) {
z--;
s1.remove(j);
}
j += i;
ans++;
}
ans--;
mnans = Math.min(mnans, ans);
}
return mnans;
}
public static void main(String[] args)
{
int N = 6 ;
int [] m = { 3 , 4 };
int n = m.length;
System.out.println(Minimum_jumps(N, m, n));
}
}
|
Python3
def Minimum_jumps(N, m, n):
if n = = 1 :
if m[ 0 ] = = 1 :
return 0
else :
return 1
s = set ()
mx = 0
for i in range (n):
s.add(m[i])
mx = max (mx, m[i])
mnans = N
for i in range (N):
s1 = set (s)
ans = 0
j = 1
z = n
while z > 0 and ans < = mx:
if j > N:
j = j % N
if j in s1:
z - = 1
s1.remove(j)
j + = i
ans + = 1
ans - = 1
mnans = min (mnans, ans)
return mnans
N = 6
m = [ 3 , 4 ]
n = len (m)
print (Minimum_jumps(N, m, n))
|
C#
using System;
using System.Collections.Generic;
public class GFG{
static int Minimum_jumps( int N, int [] m, int n) {
if (n == 1) {
if (m[0] == 1)
return 0;
else
return 1;
}
HashSet< int > s = new HashSet< int >();
int mx = 0;
for ( int i = 0; i < n; i++) {
s.Add(m[i]);
mx = Math.Max(mx, m[i]);
}
int mnans = N;
for ( int i = 0; i < N; i++) {
HashSet< int > s1 = new HashSet< int >();
s1.UnionWith(s);
int ans = 0;
int j = 1;
int z = n;
while (z > 0 && ans <= mx) {
if (j > N)
j = j % N;
if (s1.Contains(j)) {
z--;
s1.Remove(j);
}
j += i;
ans++;
}
ans--;
mnans = Math.Min(mnans, ans);
}
return mnans;
}
static public void Main (){
int N = 6;
int [] m = { 3, 4 };
int n = m.Length;
Console.Write(Minimum_jumps(N, m, n));
}
}
|
Javascript
function Minimum_jumps(N, m, n) {
if (n == 1) {
if (m[0] == 1) {
return 0;
} else {
return 1;
}
}
let s = new Set();
let mx = 0;
for (let i = 0; i < n; i++) {
s.add(m[i]);
mx = Math.max(mx, m[i]);
}
let mnans = N;
for (let i = 0; i < N; i++) {
let s1 = new Set(s);
let ans = 0;
let j = 1;
let z = n;
while (z > 0 && ans <= mx) {
if (j > N) {
j = j % N;
}
if (s1.has(j)) {
z -= 1;
s1. delete (j);
}
j += i;
ans += 1;
}
ans -= 1;
mnans = Math.min(mnans, ans);
}
return mnans;
}
let N = 6;
let m = [3, 4];
let n = m.length;
console.log(Minimum_jumps(N, m, n));
|
Time Complexity: O(NlogN), where N = size of given circular array
Auxiliary space: O(n), where n = no. of elements need to cover
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...