Skip to content

Commit f2873c1

Browse files
committed
Binary Search and Recursion concepts added
1 parent f5ce6ae commit f2873c1

File tree

9 files changed

+280
-35
lines changed

9 files changed

+280
-35
lines changed

BankingApplication/src/Main.java

Lines changed: 0 additions & 5 deletions
This file was deleted.

RecursionPractice/src/Main.java

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
public class Main {
2+
public static void main(String[] args) {
3+
// System.out.println(factorial(3));
4+
// for (int i = 0; i < 10; i++) {
5+
// System.out.print(findFibonacciSeries(i) + " ");
6+
// }
7+
// System.out.println(reverseString("Hello"));
8+
// System.out.println(reverseNumber(456));
9+
int[] array = {1, 2, 3, 4, 5};
10+
// BinarySearch(array, 10);
11+
// printAllPossibleCombinations(array, 3);
12+
}
13+
14+
static void combinationUtil(
15+
int[] arr,
16+
int arrayLength,
17+
int numberOfRegs,
18+
int currentIndex,
19+
int[] data,
20+
int startingIndex) {
21+
if (currentIndex == numberOfRegs) {
22+
for (int j = 0; j < numberOfRegs; j++) System.out.print(data[j] + " ");
23+
System.out.print("\n");
24+
return;
25+
}
26+
27+
if (startingIndex >= arrayLength) return;
28+
29+
data[currentIndex] = arr[startingIndex];
30+
combinationUtil(arr, arrayLength, numberOfRegs, currentIndex + 1, data, startingIndex + 1);
31+
combinationUtil(arr, arrayLength, numberOfRegs, currentIndex, data, startingIndex + 1);
32+
}
33+
34+
static void printAllPossibleCombinations(int[] arr, int numberOfIndex) {
35+
int[] data = new int[numberOfIndex];
36+
37+
combinationUtil(arr, arr.length, numberOfIndex, 0, data, 0);
38+
}
39+
40+
private static void BinarySearch(int[] array, int number) {
41+
int firstData = 0;
42+
int lastData = array.length - 1;
43+
int i = 0;
44+
while (firstData <= lastData) {
45+
System.out.println(i++);
46+
int middleData = (firstData + lastData) / 2;
47+
if (array[middleData] == number) {
48+
System.out.println(number + " found at " + middleData);
49+
return;
50+
} else if (array[middleData] < number) {
51+
firstData = middleData + 1;
52+
} else if (array[middleData] > number) {
53+
lastData = middleData - 1;
54+
}
55+
}
56+
}
57+
58+
private static int reverseNumber(int integer) {
59+
int number = 0;
60+
while (integer != 0) {
61+
number *= 10;
62+
number += (integer % 10);
63+
integer /= 10;
64+
}
65+
return number;
66+
}
67+
68+
private static String reverseString(String text) {
69+
String reverseString = "";
70+
int StringLength = text.length();
71+
while (StringLength > 0) reverseString += text.charAt(--StringLength);
72+
return reverseString;
73+
}
74+
75+
private static int findFibonacciSeries(int number) {
76+
return (number == 1 || number == 2)
77+
? 1
78+
: (number == 0) ? 0 : findFibonacciSeries(number - 1) + findFibonacciSeries(number - 2);
79+
}
80+
81+
private static int factorial(int number) {
82+
if (number == 0 || number == 1) return 1;
83+
return number * factorial(number - 1);
84+
}
85+
}

SortingAlgorithm/src/Main.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,21 @@ public class Main {
44
public static void main(String[] args) {
55
int[] intArray = {5, 7, 1, 15, 10, 3, 9};
66
insertionSort(intArray);
7+
findNextGreaterNumber();
8+
}
9+
10+
private static void findNextGreaterNumber() {
11+
int[] inputArray = {4, 5, 2, 25};
12+
for (int i = 0; i < inputArray.length; i++) {
13+
int currentValue = inputArray[i], j = i, nextGreaterValues = -1;
14+
while (++j < inputArray.length) {
15+
if (currentValue < inputArray[j]) {
16+
nextGreaterValues = inputArray[j];
17+
break;
18+
}
19+
}
20+
System.out.print(nextGreaterValues + " ");
21+
}
722
}
823

924
private static void insertionSort(int[] intArray) {

TaxiBookingApp/src/BookTaxi.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import java.util.List;
2+
3+
public class BookTaxi {
4+
5+
public static void BookNewTaxi(
6+
int customerId,
7+
char pickUpLocation,
8+
char dropLocation,
9+
int pickUpTime,
10+
List<Taxi> freeTaxies) {
11+
int min = 999;
12+
int DistanceBetweenTaxiAndCustomer;
13+
int DistanceBetweenPickupAndDrop;
14+
int dropTime = 0;
15+
int tripEarning = 0;
16+
String tripDetails = null;
17+
Taxi BookedTaxi = null;
18+
19+
for (Taxi currentTaxi : freeTaxies) {
20+
DistanceBetweenTaxiAndCustomer =
21+
Math.abs((currentTaxi.CurrentLocation - '0') - (pickUpLocation - '0')) * 15;
22+
DistanceBetweenPickupAndDrop = Math.abs((dropLocation - '0') - (pickUpLocation - '0')) * 15;
23+
if (DistanceBetweenTaxiAndCustomer < min) {
24+
BookedTaxi = currentTaxi;
25+
dropTime = pickUpTime + DistanceBetweenPickupAndDrop / 15;
26+
tripEarning = ((DistanceBetweenPickupAndDrop - 5) * 10 + 100);
27+
tripDetails =
28+
customerId
29+
+ " "
30+
+ customerId
31+
+ " "
32+
+ pickUpLocation
33+
+ " "
34+
+ dropLocation
35+
+ " "
36+
+ pickUpTime
37+
+ " "
38+
+ dropTime
39+
+ " "
40+
+ tripEarning;
41+
min = DistanceBetweenTaxiAndCustomer;
42+
}
43+
}
44+
assert BookedTaxi != null;
45+
BookedTaxi.BookingId = customerId;
46+
BookedTaxi.CurrentLocation = dropLocation;
47+
BookedTaxi.TotalEarnings += tripEarning;
48+
BookedTaxi.FreeTime = dropTime;
49+
BookedTaxi.TaxiDetails.add(tripDetails);
50+
System.out.println("Taxi -> " + BookedTaxi.TaxiNumber + " Booked !");
51+
}
52+
53+
public static void ShowTaxiDetails(Taxi taxi) {
54+
System.out.println(
55+
"BookingId CustomerId From To PickUpTime DropTime TripEarning\n");
56+
for (String detail : taxi.TaxiDetails) System.out.println(detail);
57+
System.out.println(
58+
"Taxi " + taxi.TaxiNumber + " : Earning -> Rs. " + taxi.TotalEarnings);
59+
System.out.println("==================================================================================");
60+
}
61+
}

TaxiBookingApp/src/Taxi.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
4+
public class Taxi {
5+
static int id = 1;
6+
int TaxiNumber;
7+
int BookingId;
8+
char CurrentLocation;
9+
int FreeTime;
10+
int TotalEarnings;
11+
List<String> TaxiDetails;
12+
13+
public Taxi() {
14+
this.TaxiNumber = id++;
15+
this.CurrentLocation = 'A';
16+
this.FreeTime = 6;
17+
this.TotalEarnings = 0;
18+
this.BookingId = 0;
19+
this.TaxiDetails = new ArrayList<>();
20+
}
21+
22+
public String toString() {
23+
return "";
24+
}
25+
}

TaxiBookingApplication/src/Main.java renamed to TaxiBookingApp/src/TaxiBookingApplication.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,12 @@
3434
*===================================================================================================
3535
*/
3636

37-
import java.util.*;
37+
import java.util.ArrayList;
38+
import java.util.Comparator;
39+
import java.util.List;
40+
import java.util.Scanner;
3841

39-
public class Main {
42+
public class TaxiBookingApplication {
4043
static int customerId = 1;
4144

4245
public static void main(String[] args) {
@@ -48,7 +51,7 @@ public static void main(String[] args) {
4851
Scanner input = new Scanner(System.in);
4952
boolean userWantsToContinue = true;
5053
// Create No of Taxies
51-
List<Taxi> allTaxies = createTaxiList(4);
54+
List<Taxi> allTaxies = createTaxiList();
5255

5356
while (userWantsToContinue) {
5457
System.out.println(
@@ -93,13 +96,13 @@ private static List<Taxi> getAllFreeTaxies(
9396
availableFreeTaxies.add(currentTaxi);
9497
}
9598
}
96-
Collections.sort(availableFreeTaxies, (a, b) -> a.TotalEarnings - b.TotalEarnings);
99+
availableFreeTaxies.sort(Comparator.comparingInt(a -> a.TotalEarnings));
97100
return availableFreeTaxies;
98101
}
99102

100-
private static List<Taxi> createTaxiList(int noOfTaxi) {
103+
private static List<Taxi> createTaxiList() {
101104
List<Taxi> createdTaxiList = new ArrayList<>();
102-
for (int i = 0; i < noOfTaxi; i++) {
105+
for (int i = 0; i < 4; i++) {
103106
createdTaxiList.add(new Taxi());
104107
}
105108
return createdTaxiList;

TaxiBookingApplication/src/BookTaxi.java

Lines changed: 0 additions & 1 deletion
This file was deleted.

TaxiBookingApplication/src/Taxi.java

Lines changed: 0 additions & 23 deletions
This file was deleted.
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import java.util.LinkedList;
2+
3+
public class countJumps {
4+
public static void main(String[] args) {
5+
int[][] inputArray = {
6+
{1, 0, 1, 1},
7+
{1, 1, 1, 1},
8+
{0, 0, 1, 0}
9+
};
10+
findMinJumps(inputArray);
11+
}
12+
13+
private static void findMinJumps(int[][] inputArray) {
14+
int rows = inputArray.length, cols = inputArray[0].length, sx = -1, sy = -1;
15+
if ((rows == 0) && (cols == 0)) {
16+
System.out.println("No Path Found");
17+
return;
18+
}
19+
for (int i = 0; i < inputArray[0].length; i++) {
20+
if (inputArray[0][i] == 1) {
21+
sx = 0;
22+
sy = i;
23+
tryToFindPath(inputArray, sx, sy);
24+
}
25+
}
26+
}
27+
28+
private static void tryToFindPath(int[][] inputArray, int sx, int sy) {
29+
Cell StartCell = new Cell(sx, sy);
30+
boolean isValidPath = false;
31+
String pathFound = "Path Found from (" + sx + ", " + sy + ")";
32+
StartCell.jumpCount = 0;
33+
Cell[][] board = new Cell[inputArray.length][inputArray[0].length];
34+
for (int i = 0; i < inputArray.length; i++) {
35+
for (int j = 0; j < inputArray[0].length; j++) {
36+
if (inputArray[i][j] != 0) board[i][j] = new Cell(i, j);
37+
}
38+
}
39+
40+
LinkedList<Cell> Queue = new LinkedList<>();
41+
Queue.add(StartCell);
42+
Cell tempCell;
43+
while ((tempCell = Queue.poll()) != null) {
44+
if (tempCell.x == inputArray.length - 1) {
45+
isValidPath = true;
46+
break;
47+
}
48+
49+
startMoving(board, Queue, tempCell.x + 1, tempCell.y, tempCell);
50+
startMoving(board, Queue, tempCell.x, tempCell.y + 1, tempCell);
51+
}
52+
53+
if (isValidPath) System.out.println(pathFound + " JumpCounts = " + tempCell.jumpCount);
54+
else if (!isValidPath) System.out.println(pathFound + " and is not valid path");
55+
}
56+
57+
private static void startMoving(
58+
Cell[][] board, LinkedList<Cell> queue, int x, int y, Cell parentCell) {
59+
if (x < 0 || y < 0 || x >= board.length || y >= board[0].length || board[x][y] == null) return;
60+
int distance = parentCell.jumpCount + 1;
61+
Cell currentCell = board[x][y];
62+
if (distance < currentCell.jumpCount) {
63+
currentCell.jumpCount = distance;
64+
currentCell.nextCell = parentCell;
65+
queue.add(currentCell);
66+
}
67+
}
68+
69+
public static class Cell {
70+
int x, y, jumpCount;
71+
Cell nextCell;
72+
73+
public Cell(int x, int y) {
74+
this.x = x;
75+
this.y = y;
76+
this.jumpCount = Integer.MAX_VALUE;
77+
this.nextCell = null;
78+
}
79+
80+
@Override
81+
public String toString() {
82+
return "(" + x + ", " + y + ")";
83+
}
84+
}
85+
}

0 commit comments

Comments
 (0)