// 1.
Two Sum
import java.util.*;
class TwoSum {
public static void main(String[] args) {
int[] nums = {2, 7, 11, 15};
int target = 9;
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int diff = target - nums[i];
if (map.containsKey(diff)) {
System.out.println(map.get(diff) + " " + i);
return;
map.put(nums[i], i);
// 2. Best Time to Buy and Sell Stock
class BestTimeToBuyAndSellStock {
public static void main(String[] args) {
int[] prices = {7, 1, 5, 3, 6, 4};
int min = prices[0], profit = 0;
for (int i = 1; i < prices.length; i++) {
if (prices[i] < min) min = prices[i];
else profit = Math.max(profit, prices[i] - min);
System.out.println("Max Profit: " + profit);
}
// 3. Rotate Array
class RotateArray {
static void reverse(int[] a, int l, int r) {
while (l < r) {
int t = a[l]; a[l++] = a[r]; a[r--] = t;
public static void main(String[] args) {
int[] a = {1, 2, 3, 4, 5, 6, 7};
int k = 3, n = a.length;
k = k % n;
reverse(a, 0, n - 1);
reverse(a, 0, k - 1);
reverse(a, k, n - 1);
for (int i : a) System.out.print(i + " ");
// 4. Reverse a Linked List
class ReverseLinkedList {
static class Node {
int data;
Node next;
Node(int d) { data = d; }
public static void main(String[] args) {
Node head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head = reverse(head);
while (head != null) {
System.out.print(head.data + " ");
head = head.next;
static Node reverse(Node head) {
Node prev = null;
while (head != null) {
Node next = head.next;
head.next = prev;
prev = head;
head = next;
return prev;
// 5. Merge Two Sorted Lists
class MergeSortedLists {
static class Node {
int data;
Node next;
Node(int d) { data = d; }
public static void main(String[] args) {
Node a = new Node(1);
a.next = new Node(3);
a.next.next = new Node(5);
Node b = new Node(2);
b.next = new Node(4);
b.next.next = new Node(6);
Node result = merge(a, b);
while (result != null) {
System.out.print(result.data + " ");
result = result.next;
static Node merge(Node l1, Node l2) {
Node dummy = new Node(0), tail = dummy;
while (l1 != null && l2 != null) {
if (l1.data < l2.data) {
tail.next = l1;
l1 = l1.next;
} else {
tail.next = l2;
l2 = l2.next;
tail = tail.next;
tail.next = (l1 != null) ? l1 : l2;
return dummy.next;
// 6. Detect Cycle in a Linked List
class DetectCycle {
static class Node {
int data;
Node next;
Node(int d) { data = d; }
public static void main(String[] args) {
Node head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = head.next; // cycle
System.out.println(hasCycle(head));
static boolean hasCycle(Node head) {
Node slow = head, fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast) return true;
return false;
// 7. Reverse String (User input without inbuilt functions)
import java.util.Scanner;
class ReverseString {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String input = sc.nextLine();
char[] chars = input.toCharArray();
for (int i = chars.length - 1; i >= 0; i--) {
System.out.print(chars[i]);
System.out.println();
sc.close();
}
// 8. Valid Parentheses
import java.util.*;
class ValidParentheses {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
Stack<Character> stack = new Stack<>();
for (char c : s.toCharArray()) {
if (c == '(' || c == '{' || c == '[') stack.push(c);
else if (stack.isEmpty() ||
(c == ')' && stack.pop() != '(') ||
(c == '}' && stack.pop() != '{') ||
(c == ']' && stack.pop() != '[')) {
System.out.println(false);
return;
System.out.println(stack.isEmpty());
// 9. Longest Common Prefix
class LongestCommonPrefix {
public static void main(String[] args) {
String[] strs = {"flower", "flow", "flight"};
if (strs.length == 0) {
System.out.println("");
return;
String prefix = strs[0];
for (int i = 1; i < strs.length; i++) {
while (!strs[i].startsWith(prefix)) {
prefix = prefix.substring(0, prefix.length() - 1);
if (prefix.isEmpty()) {
System.out.println("");
return;
System.out.println(prefix);
// 10. LRU Cache
import java.util.*;
class LRUCache {
private final int capacity;
private final LinkedHashMap<Integer, Integer> map;
public LRUCache(int capacity) {
this.capacity = capacity;
this.map = new LinkedHashMap<>(capacity, 0.75f, true) {
protected boolean removeEldestEntry(Map.Entry<Integer, Integer> eldest) {
return size() > capacity;
};
public int get(int key) {
return map.getOrDefault(key, -1);
public void put(int key, int value) {
map.put(key, value);
}
public static void main(String[] args) {
LRUCache cache = new LRUCache(2);
cache.put(1, 1);
cache.put(2, 2);
System.out.println(cache.get(1));
cache.put(3, 3);
System.out.println(cache.get(2));
// 11. Group Anagrams
import java.util.*;
class GroupAnagrams {
public static void main(String[] args) {
String[] strs = {"eat", "tea", "tan", "ate", "nat", "bat"};
Map<String, List<String>> map = new HashMap<>();
for (String s : strs) {
char[] chars = s.toCharArray();
Arrays.sort(chars);
String key = new String(chars);
map.computeIfAbsent(key, k -> new ArrayList<>()).add(s);
System.out.println(map.values());
// 12. Inorder Traversal
class InorderTraversal {
static class TreeNode {
int val;
TreeNode left, right;
TreeNode(int x) { val = x; }
public static void main(String[] args) {
TreeNode root = new TreeNode(1);
root.right = new TreeNode(2);
root.right.left = new TreeNode(3);
inorder(root);
static void inorder(TreeNode root) {
if (root == null) return;
inorder(root.left);
System.out.print(root.val + " ");
inorder(root.right);
// 13. Validate Binary Search Tree
class ValidateBST {
static class TreeNode {
int val;
TreeNode left, right;
TreeNode(int x) { val = x; }
public static void main(String[] args) {
TreeNode root = new TreeNode(2);
root.left = new TreeNode(1);
root.right = new TreeNode(3);
System.out.println(isValidBST(root));
static boolean isValidBST(TreeNode root) {
return validate(root, Long.MIN_VALUE, Long.MAX_VALUE);
static boolean validate(TreeNode node, long min, long max) {
if (node == null) return true;
if (node.val <= min || node.val >= max) return false;
return validate(node.left, min, node.val) && validate(node.right, node.val, max);
// 15. Coin Change
import java.util.*;
class CoinChange {
public static void main(String[] args) {
int[] coins = {1, 2, 5};
int amount = 11;
int[] dp = new int[amount + 1];
Arrays.fill(dp, amount + 1);
dp[0] = 0;
for (int coin : coins) {
for (int i = coin; i <= amount; i++) {
dp[i] = Math.min(dp[i], dp[i - coin] + 1);
System.out.println(dp[amount] > amount ? -1 : dp[amount]);
// 17. Merge Sort
import java.util.*;
class MergeSort {
public static void main(String[] args) {
int[] arr = {5, 2, 4, 7, 1};
sort(arr, 0, arr.length - 1);
System.out.println(Arrays.toString(arr));
static void sort(int[] arr, int l, int r) {
if (l < r) {
int m = (l + r) / 2;
sort(arr, l, m);
sort(arr, m + 1, r);
merge(arr, l, m, r);
static void merge(int[] arr, int l, int m, int r) {
int[] left = Arrays.copyOfRange(arr, l, m + 1);
int[] right = Arrays.copyOfRange(arr, m + 1, r + 1);
int i = 0, j = 0, k = l;
while (i < left.length && j < right.length) {
if (left[i] <= right[j]) arr[k++] = left[i++];
else arr[k++] = right[j++];
while (i < left.length) arr[k++] = left[i++];
while (j < right.length) arr[k++] = right[j++];
// Java DSA + Fintech Problem Solutions
// Existing problems 1-13, 15, 17 already included.
// 14. Fibonacci Sequence
class Fibonacci {
public static void main(String[] args) {
int n = 10;
int[] dp = new int[n + 1];
dp[0] = 0;
dp[1] = 1;
for (int i = 2; i <= n; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
System.out.println("Fibonacci(" + n + ") = " + dp[n]);
// 16. Binary Search
class BinarySearch {
public static void main(String[] args) {
int[] arr = {1, 3, 5, 7, 9};
int target = 7;
int low = 0, high = arr.length - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (arr[mid] == target) {
System.out.println("Found at index: " + mid);
return;
} else if (arr[mid] < target) {
low = mid + 1;
} else {
high = mid - 1;
System.out.println("Not found");
}
}
// 18. Simple and Compound Interest
class InterestCalculator {
public static void main(String[] args) {
double principal = 1000, rate = 5, time = 2;
double si = (principal * rate * time) / 100;
double ci = principal * Math.pow(1 + rate / 100, time) - principal;
System.out.println("Simple Interest: " + si);
System.out.println("Compound Interest: " + ci);
// 19. Stock Price Movement
class StockMovement {
public static void main(String[] args) {
double[] prices = {100, 105, 102, 110};
for (int i = 1; i < prices.length; i++) {
double change = ((prices[i] - prices[i - 1]) / prices[i - 1]) * 100;
System.out.printf("Day %d Change: %.2f%%\n", i, change);
// 20. Payment Processing Logic
class PaymentProcessor {
public static void main(String[] args) {
String type = "credit";
double amount = 1000;
double fee = 0;
switch (type) {
case "credit": fee = amount * 0.02; break;
case "debit": fee = amount * 0.01; break;
case "upi": fee = 0; break;
default: fee = amount * 0.03;
System.out.println("Transaction Fee: " + fee);
// 21. Fraud Detection Logic
class FraudDetection {
public static void main(String[] args) {
double[] transactions = {200, 5000, 150, 8000, 50};
for (double t : transactions) {
if (t > 5000) {
System.out.println("Flagged suspicious transaction: " + t);
// 22. Basic Trading Algorithm
class TradingBot {
public static void main(String[] args) {
int[] prices = {100, 180, 260, 310, 40, 535, 695};
for (int i = 1; i < prices.length; i++) {
if (prices[i] > prices[i - 1]) {
System.out.println("Buy at " + prices[i - 1] + ", Sell at " + prices[i]);