Search
 
SCRIPT & CODE EXAMPLE
 

CPP

minimum no of jump required to reach end of arry


 int minJumps(int a[], int n){
       
        if(n<=1) return 0;
        if (n==0) return -1;
        if(a[0]==0) return -1;
       
        int jump=1;  int maxReach=a[0];  int step= a[0];
        for(int i=1; i<n; i++){
      
        if(i==n-1) return jump;
        
        maxReach= max(maxReach,i+a[i]);
        step--;
        if(step==0){
         jump++;
         if (i >= maxReach)
				return -1;
         step= maxReach-i;   
        } }
        return -1;
    }  
Comment

PREVIOUS NEXT
Code Example
Cpp :: C++: Methods of code shortening in competitive programming 
Cpp :: In-range Adder 
Cpp :: how to list directory in c++ 
Cpp :: can derived class access base class non-static members without object of the base class 
Cpp :: How to assign two dimensional initializer list in c++ 
Cpp :: & before function arg in cpp 
Cpp :: Chef and IPC Certificates codechef solution in c++ 
Cpp :: hwo to send token on redirection in passport 
Cpp :: check if string in vector c++ 
Cpp :: qt/c++ exception handler 
Cpp :: Change Font ImGui 
Cpp :: frc limelight tracking 
Cpp :: onactorbeginoverlap c++ 
Cpp :: delete node in a linked list leetcode 
Cpp :: convert ros time to double 
Cpp :: operator = overloading c++ 
Cpp :: assignment operator 
Cpp :: Determine if map contains a value for a key c++ 
Cpp :: C++ Area and Circumference of a Circle 
Cpp :: use of strtok 
Cpp :: pointer to constant 
Cpp :: no match for ‘operator=’ (operand types are ‘std::basic_ostream’ and ‘int’) 
C :: powershell search files for string 
C :: install gitk mac 
C :: C overwrite last line 
C :: Reduce fractions in C 
C :: Prime Number Check Program in C 
C :: Call by reference to pass an array to the function in C- 
C :: c float to string 
C :: c binary search 
ADD CONTENT
Topic
Content
Source link
Name
8+8 =