Minimum number of jumps
PROBLEM :
Given an array of integers where each element represents the max number of steps that can be made forward from that element. Write a function to return the minimum number of jumps to reach the end of the array (starting from the first element). If an element is 0, then cannot move through that element.
Input:
The first line contains an integer T, depicting total number of test cases.
Then following T lines contains a number n denoting the size of the array.
Next line contains the sequence of integers a1,?a2,?...,?an.
Output:
Each seperate line showing the minimum number of jumps. If answer is not possible print -1.
Constraints:
1 = T = 30
1 = N = 100
0<=a[N]<=100
Example:
Input:
1
11
1 3 5 8 9 2 6 7 6 8 9
Output:
3
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#include<limits.h>
int Minimum_number_of_jumps(int[] ,int ) ;
int main()
{
int t,no,arr[100],i ;
cin>>t ;
while(t--)
{
cin>>no ;
for(i=0;i<no;i++)
cin>>arr[i] ;
no=Minimum_number_of_jumps(arr,no) ;
cout<<no<<endl ;
}
return 0;
}
int Minimum_number_of_jumps(int arr[],int no)
{
int temp[no] ;
int i,j ;
if(arr[0]==0)
return -1 ;
temp[0]=0 ;
for(i=1;i<no;i++)
{
temp[i]=INT_MAX ;
for(j=0;j<i;j++)
{
if(arr[j]+j<i)
continue ;
if(temp[j]+1<temp[i])
temp[i]=temp[j]+1 ;
}
}
return temp[no-1] ;
}
---------------------------------------------------------------------------------
Given an array of integers where each element represents the max number of steps that can be made forward from that element. Write a function to return the minimum number of jumps to reach the end of the array (starting from the first element). If an element is 0, then cannot move through that element.
Input:
The first line contains an integer T, depicting total number of test cases.
Then following T lines contains a number n denoting the size of the array.
Next line contains the sequence of integers a1,?a2,?...,?an.
Output:
Each seperate line showing the minimum number of jumps. If answer is not possible print -1.
Constraints:
1 = T = 30
1 = N = 100
0<=a[N]<=100
Example:
Input:
1
11
1 3 5 8 9 2 6 7 6 8 9
Output:
3
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#include<limits.h>
int Minimum_number_of_jumps(int[] ,int ) ;
int main()
{
int t,no,arr[100],i ;
cin>>t ;
while(t--)
{
cin>>no ;
for(i=0;i<no;i++)
cin>>arr[i] ;
no=Minimum_number_of_jumps(arr,no) ;
cout<<no<<endl ;
}
return 0;
}
int Minimum_number_of_jumps(int arr[],int no)
{
int temp[no] ;
int i,j ;
if(arr[0]==0)
return -1 ;
temp[0]=0 ;
for(i=1;i<no;i++)
{
temp[i]=INT_MAX ;
for(j=0;j<i;j++)
{
if(arr[j]+j<i)
continue ;
if(temp[j]+1<temp[i])
temp[i]=temp[j]+1 ;
}
}
return temp[no-1] ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment