Java Array Practice Questions
1. Find the Sum of All Elements
- Input: [2, 4, 6, 8] -> Output: 20
- Input: [1, 3, 5] -> Output: 9
- Input: [10, -2, 3] -> Output: 11
2. Find the Maximum and Minimum in an Array
- Input: [2, 8, 3, 7, 1] -> Output: Max = 8, Min = 1
- Input: [10, 10, 10] -> Output: Max = 10, Min = 10
- Input: [-5, 0, 100, 25] -> Output: Max = 100, Min = -5
3. Search for an Element in an Array
- Input: [5, 10, 15, 20], Search: 10 -> Output: Found at index 1
- Input: [3, 6, 9], Search: 7 -> Output: Not Found
- Input: [1, 2, 3, 4], Search: 4 -> Output: Found at index 3
4. Count Even and Odd Numbers
- Input: [1, 2, 3, 4, 5] -> Output: Even = 2, Odd = 3
- Input: [10, 20, 30] -> Output: Even = 3, Odd = 0
- Input: [7, 11, 13] -> Output: Even = 0, Odd = 3
5. Reverse the Array
- Input: [1, 2, 3, 4] -> Output: [4, 3, 2, 1]
- Input: [10, 20, 30] -> Output: [30, 20, 10]
- Input: [5] -> Output: [5]
6. Print Duplicate Elements
- Input: [1, 2, 3, 2, 4, 1] -> Output: 1, 2
- Input: [5, 6, 7, 8] -> Output: No duplicates
- Input: [9, 9, 9] -> Output: 9
7. Left Rotate an Array by 1 Position
- Input: [1, 2, 3, 4] -> Output: [2, 3, 4, 1]
- Input: [10, 20] -> Output: [20, 10]
- Input: [7] -> Output: [7]
8. Find Pairs with Given Sum
- Input: [1, 2, 3, 4], Target = 5 -> Output: (1, 4), (2, 3)
- Input: [5, 5, 5], Target = 10 -> Output: (5, 5)
- Input: [2, 6, 8], Target = 14 -> Output: (6, 8)
9. Remove Duplicates from an Array
- Input: [1, 2, 2, 3] -> Output: [1, 2, 3]
- Input: [4, 4, 4] -> Output: [4]
- Input: [5, 6, 7] -> Output: [5, 6, 7]
10. Sort the Array
- Input: [3, 1, 2] -> Output: [1, 2, 3]
- Input: [9, 5, 0] -> Output: [0, 5, 9]
- Input: [10] -> Output: [10]
11. Find Second Largest Element
- Input: [10, 20, 30] -> Output: 20
- Input: [5, 5, 5] -> Output: No second largest
- Input: [1, 2] -> Output: 1
12. Count Frequency of Each Element
- Input: [1, 2, 2, 3, 3, 3] -> Output: 1:1, 2:2, 3:3
- Input: [4, 4, 4, 4] -> Output: 4:4
- Input: [5, 6, 7] -> Output: 5:1, 6:1, 7:1
13. Check if Array is Palindrome
- Input: [1, 2, 1] -> Output: Yes
- Input: [1, 2, 3] -> Output: No
- Input: [5, 5, 5] -> Output: Yes
14. Merge Two Arrays
- Input: [1, 2], [3, 4] -> Output: [1, 2, 3, 4]
- Input: [], [5] -> Output: [5]
- Input: [7], [] -> Output: [7]
15. Insert Element at Specific Position
- Input: [1, 2, 4], Insert 3 at index 2 -> Output: [1, 2, 3, 4]
- Input: [10], Insert 5 at index 0 -> Output: [5, 10]
- Input: [], Insert 1 at index 0 -> Output: [1]
16. Delete Element at Specific Index
- Input: [1, 2, 3], Delete at index 1 -> Output: [1, 3]
- Input: [5, 6], Delete at index 0 -> Output: [6]
- Input: [9], Delete at index 0 -> Output: []
17. Find Common Elements in Two Arrays
- Input: [1, 2, 3], [2, 3, 4] -> Output: [2, 3]
- Input: [5, 6], [7, 8] -> Output: []
- Input: [9, 10], [10, 11] -> Output: [10]
18. Separate Even and Odd Elements
- Input: [1, 2, 3, 4] -> Output: Even=[2, 4], Odd=[1, 3]
- Input: [5, 7] -> Output: Even=[], Odd=[5, 7]
- Input: [6, 8] -> Output: Even=[6, 8], Odd=[]
19. Calculate Product of All Elements
- Input: [1, 2, 3, 4] -> Output: 24
- Input: [5, 5] -> Output: 25
- Input: [10] -> Output: 10
20. Move Zeros to End
- Input: [0, 1, 0, 3, 12] -> Output: [1, 3, 12, 0, 0]
- Input: [0, 0, 1] -> Output: [1, 0, 0]
- Input: [1, 2, 3] -> Output: [1, 2, 3]