0% found this document useful (0 votes)
8 views4 pages

C++ Array Practice Problems Guide

Uploaded by

Romessa Ali
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views4 pages

C++ Array Practice Problems Guide

Uploaded by

Romessa Ali
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Array Practice Questions

1. C++ Program To Find Maximum Difference Between Two Elements in an Array.


Example Arr is {10, 15, 90, 200, 110};
Max difference is 190

2. C++ Program to put even & odd elements of an array in 2 separate arrays.
Enter the size of array AR
6
Enter the elements of the array
34
56
78
90
12
39
The elements of OAR are
39
The elements of EAR are
34
56
78
90
12

3. C++ program to Insert an Element in the Sorted Array


Enter how many elements
5
Enter the elements
76
90
56
78
12
Input array elements are
76
90
56
78
12
Sorted list is
12
56
76
78
90
Enter the element to be inserted
61
Final list is
12
56
61
76
78
90
4. C++ program to Delete an Element from an Array from kth index

Runtime Test Cases


Testcase 1: In this case, the index of the element to be deleted is “3”
Enter the size of the array: 5
Enter the elements of the array:
arr[0] = 8
arr[1] = 4
arr[2] = 9
arr[3] = 6
arr[4] = 2
Enter the index of the element to be deleted: 3
The array after deleting the element is: 8 4 9 2

Testcase 2: In this case, the index of the element to be deleted is “6”


Enter the size of the array: 4
Enter the elements of the array:
arr[0] = 10
arr[1] = 4
arr[2] = 6
arr[3] = 1
Enter the index of the element to be deleted: 6
Deletion is not possible in the array.

5. C++ Program to Cyclically Permute the Elements of an Array


Enter the value of the n = 4
Enter the numbers
3
40
100
68
Cyclically permuted numbers are given below
40
100
68
3

6. C++ Program to Print all Non Repeated Elements in an Array


Enter size of the array: 6
Enter 6 elements of an array: 12
10
4
10
12
56

The array after removing duplicates is: 12 10 4 56


7. C++ Program to Find Missing Numbers in Array
Enter elements into array :
1
2
3
5
6
Missing element is : 4

8. C++ Program to Find Missing Numbers in an Array of Size N-1 with Numbers[1,N]
enter the range of array
9
enter a[0]element into the array:1
enter a[1]element into the array:5
enter a[2]element into the array:2
enter a[3]element into the array:7
enter a[4]element into the array:3
enter a[5]element into the array:4
enter a[6]element into the array:10
enter a[7]element into the array:9
enter a[8]element into the array:6
The missing number -> 8

9. C++ Program to Find Odd Occurring Elements in an Array


10. C++ Program to Find Mode of an Array
Enter the limit
10
Enter the set of numbers
1 2 2 3 4 5 5 6 7 8

Mode = 2 5

11. C++ Program to Split the Array and Add First Part to the End
Enter the value of n
4
enter the numbers
3
678
345
876
Enter the position of the element to split the array
3
The resultant array is
876
3
678
345
12. C++ Program to Segregate 0s and 1s in an Array
Original array = {0, 1, 0, 1, 1, 0}
segregated array is 0 0 0 1 1 1

13. C++ Program to Find the Largest Sum of Contiguous Subarray of an Array
Type the length of the array
8
type the elements of the array
-1
-5
5
3
-2
5
4
1

The largest contiguous subarray is 5 3 -2 5 4 1


The sum of the largest contiguous subarray is 16

14. C++ Program to Merge Two Sorted Array Elements


Enter the size of the first array: 3
Enter the elements of the first array:
12
18
23
Enter the size of the second array: 3
Enter the elements of the second array:
13
19
27
The merged array is:
12 13 18 19 23 27

15. C++ Program to Print all Repeated Elements with Frequency in an Array
Array {5, 10, 10, 2, 1, 4, 2};
duplicate elements present in the given array are 10 2

Common questions

Powered by AI

Inserting an element into a sorted array involves first finding the correct position where the new element should be placed to maintain the sorted order. This requires a linear search through the array until the insertion point is found. The remaining elements are then shifted one position to the right to make space for the new element. This ensures the array remains sorted after the insertion process .

The approach involves iterating through the original array and checking each element's parity using the modulus operator. Even elements are appended to one array, while odd elements go into another. This separation allows for maintaining two distinct arrays for even and odd numbers, ensuring easy management of parity-separated data .

The procedure involves specifying a position in the array to create two subarrays. The element before this position becomes the splitting point. The elements up to the split are then appended to the end of the elements following the split, effectively rotating the array. This technique is beneficial for algorithms that require certain rotational symmetry or specific ordering of elements .

The program calculates the expected sum of a full array via the formula for the sum of an arithmetic series and then subtracts the actual sum of the given array. The difference is the missing number. This relies on the assumption that the array contains numbers from a consecutive sequence with only one number missing, ensuring its accuracy .

The C++ program finds the maximum difference between two elements by iterating over the array to track the minimum element and then calculating differences with subsequent elements. The max difference is updated whenever a larger difference is found. This logic ensures that the program efficiently computes the maximum difference in linear time complexity .

The logic relies on swapping elements as the array is traversed, grouping all 0s on one side and all 1s on the other, typically using two pointers. This operation is completed in linear time without additional space, making it highly efficient. The negligible increase in complexity implies a practical approach for large binary arrays .

The deletion process involves shifting elements following the specified index one position to the left to fill the gap left by the deleted element. However, if the index is out of bounds, the operation is not possible, highlighting the need for bounds checking before attempting deletion. This prevents runtime errors and maintains data integrity within the array .

The program scans each element and uses a hash table or frequency map to track occurrences, which helps identify unique elements by filtering those with a frequency of one. This method provides a clear distinction between repeated and unique items. However, it requires additional space for frequency tracking, which may not be efficient for very large datasets .

The C++ program uses a rotation algorithm that shifts each element to the left by one position, with the first element moving to the end of the array. This approach requires minimal operations and efficiently permutes elements by leveraging the circular nature of the shift operation, maintaining overall balance in computational resources .

The mode is identified as the element (or elements) with the highest frequency in the array. The program counts occurrences of each element, storing them in a map or array. If multiple elements share the highest frequency, they are all considered modes. This allows for accurate identification of the most frequently occurring elements .

You might also like