Skip to content

Instantly share code, notes, and snippets.

@pethaniakshay
Last active June 3, 2024 20:15
Show Gist options
  • Save pethaniakshay/ec98221055fdbcb644a5160e408e5a77 to your computer and use it in GitHub Desktop.
Save pethaniakshay/ec98221055fdbcb644a5160e408e5a77 to your computer and use it in GitHub Desktop.
InterviewBit Programming Solution [Min steps Infinite Grid] https://www.codepuran.com/java/min-steps-infinite-grid-interviewbit/
package interviewbitProgramming;
import java.util.ArrayList;
import java.util.Scanner;
public class MinStepsInInfiniteGrid {
public static void main(String args[]){
System.out.println("Enter the No of Points: ");
Scanner sc = new Scanner(System.in);
ArrayList<Integer> X = new ArrayList<Integer>();
ArrayList<Integer> Y = new ArrayList<Integer>();
int size = sc.nextInt();
System.out.println("Enter the Points: ");
for(int i=0 ; i<size; i++){
System.out.print("X: ");
X.add(sc.nextInt());
System.out.print("Y: " );
Y.add(sc.nextInt());
System.out.println();
}
MinStepsInInfiniteGrid obj = new MinStepsInInfiniteGrid();
size = obj.coverPoints(X,Y);
System.out.println("The minimum required steps are: "+ size);
}
public int coverPoints(ArrayList<Integer> X, ArrayList<Integer> Y) {
int Steps = 0;
for(int i = 1; i < X.size(); i++){
Steps += Math.max( Math.abs(X.get(i) - X.get(i-1)), Math.abs(Y.get(i) - Y.get(i-1)) );
}
return Steps;
}
}
@pethaniakshay
Copy link
Author

pethaniakshay commented May 27, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment