11. Implement a function to calculate the maximum sum of a contiguous subarray (Kadane's Algorithm)
Required Input:
[-2,1,-3,4,-1,2,1,-5,4]
Expected Output:
6
Code In Java
import java.util.*;
public class Main {
public static int maxSubArray() {
int[] nums = {-2,1,-3,4,-1,2,1,-5,4};
// Your code here
return 0;
}
}
Output
Click Run Button to view compiled output
12. Implement a function to calculate the edit distance between two strings
Required Input:
"kitten", "sitting"
Expected Output:
3
Code In Java
import java.util.*;
public class Main {
public static int editDistance() {
String str1 = "kitten";
String str2 = "sitting";
// Your code here
return 0;
}
}
Output
Click Run Button to view compiled output
13. Implement a function to solve the 'Longest Common Subsequence' problem
Required Input:
"abcde", "ace"
Expected Output:
3
Code In Java
import java.util.*;
public class Main {
public static int longestCommonSubsequence() {
String str1 = "abcde";
String str2 = "ace";
// Your code here
return 0;
}
}
Output
Click Run Button to view compiled output
14. Implement a function to find the smallest positive integer missing from an unsorted array
Required Input:
[3, 4, -1, 1]
Expected Output:
2
Code In Java
import java.util.*;
public class Main {
public static int firstMissingPositive() {
int[] nums = {3, 4, -1, 1};
// Your code here
return 0;
}
}
Output
Click Run Button to view compiled output
15. Implement a function to calculate the maximum profit from buying and selling stock given multiple transactions
Required Input:
[7, 1, 5, 3, 6, 4]
Expected Output:
7
Code In Java
import java.util.*;
public class Main {
public static int maxProfit() {
int[] prices = {7, 1, 5, 3, 6, 4};
// Your code here
return 0;
}
}
Output
Click Run Button to view compiled output
16. Implement a function to reverse nodes in k-group in a linked list
Required Input:
Given linked list: 1 → 2 → 3 → 4 → 5, k = 2
Expected Output:
2 1 4 3 5
Code In Java
class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
public class Main {
public static ListNode reverseKGroup() {
// Create the linked list here
// Your code here
return null;
}
}
Output
Click Run Button to view compiled output
17. Implement a function to flatten a binary tree into a linked list in-place
Required Input:
Tree: 1 → 2 → 5 → 3 → 4 → 6
Expected Output:
1 2 3 4 5 6
Code In Java
class TreeNode {
int val;
TreeNode left, right;
TreeNode(int x) { val = x; }
}
public class Main {
public static TreeNode flattenTree() {
// Create the binary tree here
// Your code here
return null;
}
}
Output
Click Run Button to view compiled output
18. Write a function to perform the N-Queens Problem (find all valid configurations for placing N queens on an NxN chessboard)
Required Input:
N = 4
Expected Output:
[[.Q.., ...Q, Q..., ..Q.], [..Q., Q..., ...Q, .Q..]]
Code In Java
import java.util.*;
public class Main {
public static List
- > solveNQueens() {
int n = 4;
// Your code here
return new ArrayList<>();
}
}
Output
Click Run Button to view compiled output
19. Write a function to check if a given string is valid according to the rules of a balanced parentheses sequence
Required Input:
"({[]})"
Expected Output:
true
Code In Java
import java.util.*;
public class Main {
public static boolean isValidParentheses() {
String s = "({[]})";
// Your code here
return false;
}
}
Output
Click Run Button to view compiled output
20. Implement a function to perform heap sort on an array
Required Input:
[12, 11, 13, 5, 6, 7]
Expected Output:
[5, 6, 7, 11, 12, 13]
Code In Java
import java.util.*;
public class Main {
public static void heapSort() {
int[] arr = {12, 11, 13, 5, 6, 7};
// Your code here
}
}
Output
Click Run Button to view compiled output