Problem: Throw Exception when you method hit goes above a certain limit.
Approach: Try to implement the leaky bucket algorithm Here.
Solution:
"Success = (DSA + System Design) × Consistency × Problem-Solving + Mindfulness" 💡💻🚀
package hackerrankQuestion.fidelity; | |
import java.util.Scanner; | |
public class Solution3 | |
{ | |
public static void main(String[] args) | |
{ | |
Scanner sc = new Scanner(System.in); | |
int numberOfElements= sc.nextInt(); | |
int ar[]= new int[numberOfElements]; | |
for(int i=0;i<numberOfElements;i++) | |
{ | |
ar[i]=sc.nextInt(); | |
} | |
int maxDifference=getMaxDifference(ar); | |
System.out.println("The resultant maximum difference is "+maxDifference); | |
} | |
public static int getMaxDifference(int ar[]) | |
{ | |
int curMin = ar[0]; | |
int resMin = -1; | |
for (int i = 1; i < ar.length; i++) | |
{ | |
if (ar[i] < curMin) | |
{ | |
curMin = ar[i]; | |
continue; | |
} else if (ar[i] > curMin) | |
{ | |
int res = ar[i] - curMin; | |
if (res > resMin) | |
{ | |
resMin = res; | |
} | |
} | |
} | |
return resMin; | |
} | |
} |
package hackerrankQuestion.fidelity; | |
import java.util.Scanner; | |
public class Solution2 | |
{ | |
public static void main(String[] args) | |
{ | |
Scanner sc = new Scanner(System.in); | |
int numberOfElements = sc.nextInt(); | |
int ar[] = new int[numberOfElements]; | |
for (int i = 0; i < numberOfElements; i++) | |
{ | |
ar[i] = sc.nextInt(); | |
} | |
int numberOfMoves = minMoves(ar); | |
System.out.println("The minimum number of moves is " + numberOfMoves); | |
for (int i : ar) | |
{ | |
System.out.println(i); | |
} | |
} | |
public static int minMoves(int ar[]) | |
{ | |
int beg = 0; | |
int end = ar.length - 1; | |
int count = 0; | |
while (beg <= end) | |
{ | |
if (ar[beg] % 2 != 0 && ar[end] % 2 == 0) | |
{ | |
int x = ar[beg]; | |
ar[beg] = ar[end]; | |
ar[end] = x; | |
beg++; | |
end--; | |
count++; | |
} | |
if (ar[beg] % 2 == 0) | |
{ | |
beg++; | |
} | |
if (ar[end] % 2 != 0) | |
{ | |
end--; | |
} | |
} | |
return count; | |
} | |
} |
package hackerrankQuestion.fidelity; | |
import java.util.Scanner; | |
public class Solution | |
{ | |
// The following code is already present. | |
public static void main(String[] args) | |
{ | |
Scanner sc = new Scanner(System.in); | |
int x1 = sc.nextInt(); | |
int y1 = sc.nextInt(); | |
int z1 = sc.nextInt(); | |
int x2 = sc.nextInt(); | |
int y2 = sc.nextInt(); | |
int z2 = sc.nextInt(); | |
Point2D p1 = new Point2D(x1, y1); | |
Point2D p2 = new Point2D(x2, y2); | |
Point3D p3D1 = new Point3D(x1, y1, z1); | |
Point3D p3D2 = new Point3D(x2, y2, z2); | |
double twoDdistance = p1.distanceFrom(p2); | |
double threeDdistance = p3D1.distanceFrom(p3D2); | |
p1.printDistance(twoDdistance); | |
p3D1.printDistance(threeDdistance); | |
} | |
} | |
//we need to implement the following classes | |
class Point2D | |
{ | |
int x, y; | |
public Point2D(int x, int y) | |
{ | |
this.x = x; | |
this.y = y; | |
} | |
public double distanceFrom(Point2D p) | |
{ | |
int subx = p.x - this.x; | |
int suby = p.y - this.y; | |
return Math.sqrt(((subx * subx) + (suby * suby))); | |
} | |
public void printDistance(double d) | |
{ | |
System.out.println("2D distance=" + (int) Math.ceil(d)); | |
} | |
} | |
class Point3D extends Point2D | |
{ | |
int z; | |
public Point3D(int x, int y, int z) | |
{ | |
super(x, y); | |
this.z = z; | |
} | |
public double distanceFrom(Point3D p) | |
{ | |
int subx = p.x - this.x; | |
int suby = p.y - this.y; | |
int subz = p.z - this.z; | |
return Math.sqrt(((subx * subx) + (suby * suby) + (subz * subz))); | |
} | |
public void printDistance(double d) | |
{ | |
System.out.println("3D distance=" + (int) Math.ceil(d)); | |
} | |
} |