Summary: In this program, I developed different void functions to reconfigure or merge the data given by the elements of the array.
Method: For questions 1-3 and 5, I created 2 void functions that gives random values to the array by using srand() from the #ctime and #cstdlib libraries. For question 4, I placed the array inside of int main() because this method allowed me to merge 2 arrays with less difficulty. Note that each question contains an additional and unique void or int function that is referenced in the int main().
Questions Answered:
-
Write a C++ function rotate(int arr[], int d, int n) that rotates arr[] of size n to the left by d elements.
-
Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), write a C++ function maxProfit(int prices[], int size) to find the maximum profit.
-
Given an array of random numbers, write a C++ function pushZerosToEnd(int arr[], int n) to push all the zero’s of a given array to the end of the array.
-
Given two sorted arrays, write a C++ function mergeArrays(int arr1[], int arr2[], int n1, int n2, int arr3[]) to merge them in a sorted manner.
-
Given an array of size n, write a C++ function findMajority(int arr[], int size) to find the majority element. The majority element is the element that appears more than ⌊ n/2⌋ times. You may assume that the array is non-empty and the majority element always exist in the array.