C++ Array: Exercises, Practice, Solution
This resource offers a total of 150 C++ Array problems for practice. It includes 30 main exercises, each accompanied by solutions, detailed explanations, and four related problems.
[An Editor is available at the bottom of the page to write and execute the scripts.]
1. Largest Element in Array
Write a C++ program to find the largest element of a given array of integers.
Click me to see the sample solution
2. Largest Three Elements in Array
Write a C++ program to find the largest three elements in an array.
Click me to see the sample solution
3. Second Largest Element in Array
Write a C++ program to find the second largest element in an array of integers.
Click me to see the sample solution
4. k Largest Elements in Array
Write a C++ program to find the k largest elements in a given array of integers.
Click me to see the sample solution
5. Second Smallest Element in Array
Write a C++ program to find the second smallest elements in a given array of integers.
Click me to see the sample solution
6. Elements with At Least Two Significant Neighbors
Write a C++ program to find all elements in an array of integers that have at least two significant elements.
Click me to see the sample solution
7. Most Frequent Element in Array
Write a C++ program to find the most frequent element in an array of integers.
Click me to see the sample solution
8. Next Greater Element for Every Array Element
Write a C++ program to find the next more powerful element of every element of a given array of integers. Ignore those elements that have no greater element.
Click me to see the sample solution
9. Wave Form Sorting of an Array
Write a C++ program to sort a given unsorted array of integers, in wave form.
Note: An array is in wave form when array[0] >= array[1] <= array[2] >= array[3] <= array[4] >= . . . .
Click me to see the sample solution
10. Smallest Missing Element in a Sorted Array
Write a C++ program to find the smallest element missing from a sorted array.
Click me to see the sample solution
11. Update Array Elements by Multiplying Neighbors
Write a C++ program to update every array element by multiplication of the next and previous values of a given array of integers.
Click me to see the sample solution
12. Zig-Zag Array Rearrangement
Write a C++ program to rearrange the elements of a given array of integers in a zig-zag pattern.
Note: The format zig-zag array in form a < b > c < d > e < f.
Click me to see the sample solution
13. Separate Even and Odd Numbers in Array
Write a C++ program to separate even and odd numbers in an array of integers. Put all even numbers first, and then odd numbers.
Click me to see the sample solution
14. Separate 0s and 1s from an Array
Write a C++ program to separate 0s and 1s from a given array of values 0 and 1.
Click me to see the sample solution
15. Rearrangement of Sorted Array in Max-Min Order
Write a C++ program to rearrange a given sorted array of positive integers .
Note: In final array, first element should be maximum value, second minimum value, third second maximum value , fourth second minimum value, fifth third maximum and so on.
Click me to see the sample solution
16. Sort Array of 0s, 1s, and 2s
Write a C++ program to sort a given array of 0s, 1s and 2s. In the final array put all 0s first, then all 1s and all 2s last.
Click me to see the sample solution
17. Descending Order Sorting by Absolute Difference
Write a C++ program to sort (in descending order) an array of distinct elements according to the absolute difference of array elements and with a given value.
Click me to see the sample solution
18. Move Negative Elements to the End
Write a C++ program to move all negative elements of an array of integers to the end of the array. This is done without changing the order of the positive and negative elements of the array.
Click me to see the sample solution
19. Find Element Occurring Odd Number of Times
Write a C++ program to find a number that occurs an odd number of times in a given array of positive integers. In the array, all numbers occur an even number of times.
Click me to see the sample solution
20. Count Occurrences of a Given Number in Sorted Array
Write a C++ program to count the number of occurrences of a given number in a sorted array of integers.
Click me to see the sample solution
21. Find Two Repeating Elements in Array
Write a C++ program to find the two repeating elements in a given array of integers.
Click me to see the sample solution
22. Find Missing Element from Two Arrays
Write a C++ program to find the missing element from two given arrays of integers except one element.
Click me to see the sample solution
23. Find Unique Element Where Others Appear Twice
Write a C++ program to find the element that appears once in an array of integers and every other element appears twice.
Click me to see the sample solution
24. Find the First Repeating Element in Array
Write a C++ program to find the first repeating element in an array of integers.
Click me to see the sample solution
25. Common Elements in Three Sorted Arrays
Write a C++ program to find and print all common elements in three sorted arrays of integers.
Click me to see the sample solution
26. Distinct Elements in Array
Write a C++ program to find and print all distinct elements of a given array of integers.
Click me to see the sample solution
27. Count Pairs with Sum Equal to Specified Number
Write a C++ program to find the number of pairs of integers in a given array of integers whose sum is equal to a specified number.
Click me to see the sample solution
28. Arrange Numbers so that Sum of Some Equals Largest Number
Write a C++ program to arrange the numbers in a given array in a way that the sum of some numbers equals the largest number in the array.
Click me to see the sample solution
29. Second Lowest and Highest Numbers in Array
Write a C++ program to find the second lowest and highest numbers in a given array.
Click me to see the sample solution
30. Third Largest String in an Array
Write a C++ program to find the third largest string in a given array of strings.
Click me to see the sample solution
CPP Code Editor:
More to Come !
Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.