21. Implement a function to return the sum of diagonal elements in a square matrix.
Required Input:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Expected Output:
15
Code In Java
import java.util.*;
public class Main {
public static int diagonalSum() {
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
// Your code here
return 0;
}
}
Output
Click Run Button to view compiled output
22. Move all zeros to the end of an array, keeping the order of other elements.
Required Input:
[0, 1, 0, 3, 12]
Expected Output:
[1, 3, 12, 0, 0]
Code In Java
import java.util.*;
public class Main {
public static int[] moveZeros() {
int[] arr = {0, 1, 0, 3, 12};
// Your code here
return arr;
}
}
Output
Click Run Button to view compiled output
23. Find the largest product of three numbers in an array.
Required Input:
[1, 2, 3, 4]
Expected Output:
24
Code In Java
import java.util.*;
public class Main {
public static int largestProductOfThree() {
int[] arr = {1, 2, 3, 4};
// Your code here
return 0;
}
}
Output
Click Run Button to view compiled output
24. Implement a function to check if two strings are rotations of each other.
Required Input:
"abcd", "dabc"
Expected Output:
true
Code In Java
import java.util.*;
public class Main {
public static boolean areRotations() {
String str1 = "abcd";
String str2 = "dabc";
// Your code here
return false;
}
}
Output
Click Run Button to view compiled output
25. Find all subarrays of an array that sum to a target value.
Required Input:
[1, 2, 3, 4, 5], target = 5
Expected Output:
[[2, 3], [5]]
Code In Java
import java.util.*;
public class Main {
public static List
- > findSubarrays() {
int[] arr = {1, 2, 3, 4, 5};
int target = 5;
// Your code here
return new ArrayList<>();
}
}
Output
Click Run Button to view compiled output
26. Implement a function to reverse the words in a string.
Required Input:
"Java is fun"
Expected Output:
fun is Java
Code In Java
import java.util.*;
public class Main {
public static String reverseWords() {
String str = "Java is fun";
// Your code here
return str;
}
}
Output
Click Run Button to view compiled output
27. Write a function to check if two arrays have the same elements (ignoring the order).
Required Input:
[1, 2, 3], [3, 2, 1]
Expected Output:
true
Code In Java
import java.util.*;
public class Main {
public static boolean haveSameElements() {
int[] arr1 = {1, 2, 3};
int[] arr2 = {3, 2, 1};
// Your code here
return false;
}
}
Output
Click Run Button to view compiled output
28. Find the intersection of two arrays (common elements).
Required Input:
[1, 2, 3, 4], [3, 4, 5, 6]
Expected Output:
[3, 4]
Code In Java
import java.util.*;
public class Main {
public static List findIntersection() {
int[] arr1 = {1, 2, 3, 4};
int[] arr2 = {3, 4, 5, 6};
// Your code here
return new ArrayList<>();
}
}
Output
Click Run Button to view compiled output
29. Implement a function to calculate the power of a number using recursion.
Required Input:
2, 3
Expected Output:
8
Code In Java
import java.util.*;
public class Main {
public static int power(int base, int exponent) {
// Your code here
return 0;
}
}
Output
Click Run Button to view compiled output
30. Find the longest increasing subsequence in an array.
Required Input:
[10, 22, 9, 33, 21, 50, 41, 60]
Expected Output:
[10, 22, 33, 50, 60]
Code In Java
import java.util.*;
public class Main {
public static List longestIncreasingSubsequence() {
int[] arr = {10, 22, 9, 33, 21, 50, 41, 60};
// Your code here
return new ArrayList<>();
}
}
Output
Click Run Button to view compiled output