Last active
June 3, 2024 20:15
-
-
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/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Compititive Programming Questions