Practice Questions
Practice Questions
Given an array of integers (in a series) and we have to find its missing
elements (there will be a missing element).
Code:
public class Arr1 {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 6, 7};
int n = arr.length + 1;
int expectedSum = n * (n + 1) / 2;
int actualSum = 0;
for (int num : arr) actualSum += num;
System.out.println("Missing element is: " + (expectedSum - actualSum));
}
}
}
Q3. Move all zero at the end of the array Given an integer array with zeros (0’s)
and we have to move all zeros at the end of the array
Code:
public class Arr3 {
public static void main(String[] args) {
int[] arr = {5, 1, 6, 0, 0, 3, 9, 0, 6, 7, 8, 12, 10, 0, 2};
int index = 0;
for (int num : arr) if (num != 0) arr[index++] = num;
while (index < arr.length) arr[index++] = 0;
for (int num : arr) System.out.print(num + " ");
}
Q4. Delete a specific element from a one dimensional array Given an array and
an element to delete and we have to delete it from array
Code:
import java.util.Scanner;
import java.util.Arrays;
public class Arr4 {
Q5. Print EVEN and ODD elements from an array Given a one dimensional array
and we have to print its EVEN and ODD elements separately.
Code:
public class Arr5 {
public static void main(String[] args) {
int[] arr = {10, 11, 12, 13, 14};
System.out.print("Even numbers: ");
for (int num : arr) if (num % 2 == 0) System.out.print(num + " ");
System.out.print("\nOdd numbers: ");
for (int num : arr) if (num % 2 != 0) System.out.print(num + " ");
}
Q6. Merge two one dimensional arrays Given two one-dimensional arrays and we
have to merge them using java program.
Code:
import java.util.Arrays;
public class Arr6 {
Q7. Read and print a two dimensional array Read number of rows and columns,
array elements for two dimensional array and print in matrix format using java
program.
Code:
import java.util.Scanner;
public class Arr7 {
System.out.println("Matrix is:");
for (int[] row : matrix) {
for (int elem : row) System.out.print(elem + " ");
System.out.println();
}
}
}
Q8. Count strings and integers from an array Given an array with strings and
integers and we have to count strings and integers using java program.
Code:
public class Arr8 {
public static void main(String[] args) {
Object[] arr = {"Raj", "77", "101", "99", "Jio"};
int stringCount = 0, intCount = 0;
Q9. Remove duplicate elements from an array Given an array of integers and we
have to remove duplicate elements using java program.
Code:
import java.util.Arrays;
public class Arr9 {
Q10. Find second largest element in an array Given an array of N integers and we
have to find its second largest element using Java program.
Code:
import java.util.Arrays;
public class Arr10 {
Q11. Find second smallest element in an array .Given an array of N integers and
we have to find its second minimum/smallest element using Java program.
Code:
import java.util.Arrays;
public class Arr11 {
Q12. Find smallest element in an array Given an array of N integers and we have
to find its minimum/smallest element using Java program.
Code:
import java.util.Arrays;
public class Arr12 {
Q13. Count total positives, negatives and zeros from an array Given an array of
integers and we have to count total negatives, positives and zeros using java
program.
Code:
public class Arr13 {
Q14. Access elements of character array using for each loop In this program, we
will create an array of characters and access its elements using for each loop and
print it on the screen.
Code:
public class Arr14 {
Q15. Find prime and non-prime numbers in the array Given/input an integer
array, we have to find prime and non-prime numbers in the array.
Code:
public class Arr15 {
Q16. Search an item into the array using linear search The linear searching
algorithm is used to find the item in an array, This algorithm consists the
checking every item in the array until the required item is found or till the last
item is in the array. The linear search algorithm is also known as a sequential
algorithm. In this program, we will create an array of integers then we will search
an item into the array using linear search and print position of the item in the
array.
Code:
import java.util.Scanner;
public class Arr16 {
Q17. Search an item in an array using binary search The binary searching
algorithm is used to find the item in a sorted array with the minimum number of
comparisons, in this algorithm key element compares with the middle index item.
In this program, we will create an array of sorted integers then we will search an
item into an array using binary search and print the position of the item in the
array.
Code:
import java.util.Arrays;
import java.util.Scanner;
public class Arr17 {
Q18. This is the Java Program to Find Repeated Elements and the Frequency of
Repetition. Problem Description Given an array of integers, find and print the
repeated elements and their frequency of repetition.
Code:
import java.util.HashMap;
public class Arr18 {
}
Q19. This is the Java Program to Find the Elements that do Not have Duplicates.
Problem Description Given an array, print all the elements whose frequency is
one, that is they do not have duplicates.
Code:
import java.util.HashMap;
public class Arr19 {
Q20. his is the Java Program to Print Elements Which Occurs Even Number of
Times. Problem Description Given an array of elements, print the elements
whose frequency is even.
Code:
import java.util.HashMap;
public class Arr20 {
Q21. This is the Java Program to Print Elements Which Occur Odd Number of
Times. Problem Description Given an array of integers, print all the elements
whose frequency are odd.
Code:
import java.util.HashMap;
public class Arr21 {
Q22. Given an array of integers, print the even numbers present at even index
numbers.
Code:
public class Arr22 {
Q23. Problem Description Given an array of integers, find the longest contiguous
subarray whose elements are increasing, that is, the elements following the
preceding elements in the subarray must be greater than them.
Code:
public class Arr23 {
public static void main(String[] args) {
int[] arr = {5, 6, 3, 0, 7, 8, 9, 1, 2};
int start = 0, maxLength = 1, maxStart = 0;
Q24. Given an array of integers, find the longest contiguous subarray whose
elements are decreasing, that is, the elements following the preceding elements
in the subarray must be smaller than them.
Code:
public class Arr24 {
public static void main(String[] args) {
int[] arr = {5, 6, 3, 0, 7, 8, 9, 1, 2};
int start = 0, maxLength = 1, maxStart = 0;
Q25. This is a Java Program to Sort Names in an Alphabetical Order. Enter size of
array and then enter all the names in that array. Now with the help of compareTo
operator we can easily sort names in Alphabetical Order.
Code:
import java.util.Arrays;
import java.util.Scanner;
public class Arr25 {
Arrays.sort(names);
System.out.println("Names in sorted order: " + String.join(", ", names));
}
}
Q26. Problem Description Given an array of integers, shift all the zeroes present
in it to the beginning.
Code:
public class Arr26 {
public static void main(String[] args) {
int[] arr = {1, 0, 2, 3, 0, 4};
int index = arr.length - 1;
Q27. Problem Description Given an array of integers, shift all the zeroes present
in it to the end.
Code:
public class Arr27 {
public static void main(String[] args) {
int[] arr = {1, 0, 2, 3, 0, 4};
int index = 0;
Q28. Problem Description Given two arrays of integers, find and print the union
and intersection of the arrays.
Code:
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class Arr28 {
}
Q29. Given an array arr of n elements that is first strictly increasing and then
maybe strictly decreasing, find the maximum element in the array. Note: If the
array is increasing then just print the last element will be the maximum value.
Code:
public class Arr29 {
public static void main(String[] args) {
int[] arr = {10, 20, 15, 2, 23, 90, 67};
int max = arr[0];
Q30. An array contains both positive and negative numbers in random order.
Rearrange the array elements so that all negative numbers appear before all
positive numbers.
Code:
public class Arr30 {
public static void main(String[] args) {
int[] arr = {-12, 11, -13, -5, 6, -7, 5, -3, -6};
int index = 0;
Q32. Find the first repeating element in an array of integers Given an array of
integers arr[], The task is to find the index of first repeating element in it i.e. the
element that occurs more than once and whose index of the first occurrence is
the smallest.
Code:
import java.util.HashMap;
Q33. Maximum Product Subarray Given an array that contains both positive and
negative integers, the task is to find the product of the maximum product
subarray.
Code:
public class Arr33 {
public static void main(String[] args) {
int[] arr = {6, -3, -10, 0, 2};
int maxProduct = arr[0], minProduct = arr[0], result = arr[0];
Q35. Program to find the smallest and largest number in an integer array.
Code:
public class Arr35 {
public static void main(String[] args) {
int[] arr = {-20, 34, 21, -87, 92, 2147483647};
int min = arr[0], max = arr[0];
reverseArray(intArray);
reverseArray(strArray);
if (cols1 != rows2) {
System.out.println("Matrix multiplication not possible.");
return;
}
System.out.println("Product of matrices:");
for (int[] row : result) {
for (int val : row) System.out.print(val + " ");
System.out.println();
}
}
}
Q41. Passing Division Program
Code:
import java.util.Scanner;
Q43. Find second smallest element in an array Given an array of N integers and
we have to find its second minimum/smallest element
Code:
import java.util.Arrays;
Q44. Count total positives, negatives and zeros from an array Given an array of
integers and we have to count total negatives, positives and zeros
Code:
public class Arr44 {
public static void main(String[] args) {
int[] arr = {20, -10, 15, 0, -85};
int positives = 0, negatives = 0, zeros = 0;
Q45. Find the length of an array In this program, we will create an array of 10
integers then we will find the length of the array
Code:
public class Arr45 {
public static void main(String[] args) {
int[] intArr = new int[10];
System.out.println("Length of array is: " + intArr.length);
}
}
Q46. Find prime and non-prime numbers in the array Given/input an integer
array, we have to find prime and non-prime numbers in the array.
Code:
public class Arr46 {
public static void main(String[] args) {
int[] arr = {10, 11, 13, 15, 17, 19, 23, 25, 30};
Q48. Program to Find the Largest Two Numbers in a Given Array. Enter size of
array and then enter all the elements of that array.
Code:
import java.util.Scanner;
System.out.println("Enter elements:");
for (int i = 0; i < n; i++) arr[i] = scanner.nextInt();
Q49. Program to Separate Odd and Even Numbers from an Array Program to Put
Even & Odd Elements of an Array in 2 Separate Arrays
Code:
public class Arr49 {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5, 6, 7, 8};
StringBuilder odd = new StringBuilder(), even = new StringBuilder();
Q50. Problem Description Given an array of integers, find the longest contiguous
subarray whose elements are increasing, that is, the elements following the
preceding elements in the subarray must be greater than them.
Code:
public class Arr50 {
public static void main(String[] args) {
int[] arr = {5, 6, 3, 0, 7, 8, 9, 1, 2};
int start = 0, maxLength = 1, maxStart = 0;