Basic Array Practise questions
1. Find the maximum and minimum element in an array.
Task: Given an array of integers, write a program to find both the maximum and
minimum element in the array.
Example:
Input: [3, 1, 4, 1, 5, 9, 2]
Output: Max: 9, Min: 1
2. Reverse an array.
Task: Given an array, reverse the array without using any built-in reverse
functions.
Example:
Input: [1, 2, 3, 4, 5]
Output: [5, 4, 3, 2, 1]
3. Check if an array is a palindrome.
Task: Given an array of numbers or characters, check if it is a palindrome (reads
the same forward and backward).
Example:
Input: [1, 2, 3, 2, 1]
Output: True
Input: [1, 2, 3, 4]
Output: False
4. Find the second largest element in an array.
Task: Write a program to find the second largest element in an array.
Example:
Input: [10, 5, 30, 20, 15]
Output: 20
5. Move all zeros to the end of the array.
Task: Given an array, move all zeros to the end while maintaining the relative
order of non-zero elements.
Example:
Input: [0, 1, 2, 0, 3, 0, 4]
Output: [1, 2, 3, 4, 0, 0, 0]
6. Find the frequency of each element in an array.
Task: Given an array of integers, return the frequency of each element.
Example:
Input: [4, 5, 6, 4, 7, 6, 4]
Output: {4: 3, 5: 1, 6: 2, 7: 1}
7. Find the sum of all elements in an array.
Task: Write a program to calculate the sum of all elements in an array.
Example:
Input: [10, 20, 30, 40, 50]
Output: 150
8. Find the missing number in a sequence.
Task: Given an array containing n-1 numbers from the range [1, n], find the missing
number.
Example:
Input: [1, 2, 4, 5] (for n = 5)
Output: 3
9. Find the common elements in two arrays.
Task: Given two arrays, find the common elements between them.
Example:
Input: [1, 2, 3, 4], [3, 4, 5, 6]
Output: [3, 4]
10. Check if an array contains duplicate elements.
Task: Write a program to check if any element appears more than once in the array.
Example:
Input: [1, 2, 3, 4, 5, 1]
Output: True
Input: [1, 2, 3, 4, 5]
Output: False
11. Find the largest sum of any two elements.
Task: Write a program to find the largest sum of any two elements in the array.
Example:
Input: [1, 2, 3, 4, 5]
Output: 9 (since 4 + 5 = 9)
11. Find the index of the first occurrence of a given element.
Task: Given an array and a target element, return the index of the first occurrence
of the target. If the target is not in the array, return -1.
Example:
Input: [2, 4, 6, 8, 10], Target = 6
Output: 2
Input: [2, 4, 6, 8, 10], Target = 5
Output: -1
12. Count the number of even and odd numbers in an array.
Task: Given an array, count how many even and odd numbers are present in the array.
Example:
Input: [1, 2, 3, 4, 5, 6]
Output: Even count: 3, Odd count: 3
13. Find the sum of the elements at odd indices.
Task: Given an array, find the sum of the elements that are located at odd indices.
Example:
Input: [1, 2, 3, 4, 5, 6]
Output: 12 (since 2 + 4 + 6 = 12)
14. Check if an array contains only positive numbers.
Task: Given an array, check if all elements are positive numbers.
Example:
Input: [1, 2, 3, 4, 5]
Output: True
Input: [-1, 2, 3, 4, 5]
Output: False
15. Find the first non-repeating element in an array.
Task: Given an array, return the first non-repeating element. If all elements are
repeating, return -1.
Example:
Input: [4, 5, 6, 4, 5, 7]
Output: 6
Input: [4, 4, 5, 5]
Output: -1