C++ Array and List Operations Code
C++ Array and List Operations Code
The `ensureCapacity` method checks whether the requested capacity `cap` is greater than or equal to the current `capacity`. If so, it calculates a new capacity by multiplying the current capacity by 1.5. It then allocates a new array with this new capacity and copies existing elements from the original array into this new one. After copying the data, it deletes the old array, updates the pointer to reference the new array, and sets the new `capacity`. This ensures that the `ArrayList` can accommodate new elements without frequent resizing .
The `removeAt` method maintains array integrity by shifting elements to the left after removing an element at a specified index. The element that follows the removed element takes its place, and this process continues until the last element in the array. The `count` of elements is decremented by 1. This ensures that there are no gaps in the array and maintains a continuous and correctly indexed sequence of elements .
The `clear` method in the `ArrayList` class effectively resets the array to its initial state. It deletes the existing array data and creates a new array with a capacity of 5, setting the `count` to 0. This operation removes all elements, thus maintaining data protection and integrity by eliminating obsolete data and reallocating the structure. Furthermore, by setting a fixed initial capacity, it prepares the `ArrayList` for future use without excessive memory allocation initially .
The `longestSublist` function updates its longest sublist record based on changes in the initial character of the words it iterates through. When the initial character of the current word does not match that of the previous word, the function compares the current sublist length (`current`) to the recorded longest (`longest`). If `current` is greater, it updates `longest`. This ensures that the longest contiguous sublist starting with the same letter is recorded before resetting to count a new potential sublist .
The `updateArrayPerRange` function iteratively processes a list of range update operations on an array of integers. For each operation, defined by a pair of indices `[L, R]` and a value `X`, it increments each element in the array from index `L` to `R` by `X`. After applying all operations, the function adds the incremental results to the original array values to produce the final modified array. This ensures each range modification is correctly applied to the respective segments of the array .
When attempting to insert an element at an invalid index, the `ArrayList` template class throws a `std::out_of_range` exception with the message "the input index is out of range!". This is done by checking if the index is less than 0 or greater than the current number of elements (`count`). If either condition is true, the exception is raised, preventing further execution .
The `ensureCapacity` method has a time complexity of O(n) where n is the number of elements, due to the need to copy existing elements to a new array. Its space complexity is O(n) as well, since new memory allocation is required for the resized array. This resizing process, though amortized over multiple additions, can lead to high time costs during individual resize operations, making element addition potentially costly at times when capacity is exhausted .
The `longestSublist` function calculates the length of the longest contiguous sublist of words in which all words start with the same letter. It maintains two counters, `longest` and `current`. As it iterates through the list of words, the `current` counter increments for each consecutive word starting with the same letter as the previous word. If it encounters a word beginning with a different letter, it updates `longest` with the maximum value between `longest` and `current`, and resets `current` to 1 to begin counting a new sublist. The function returns the maximum of `longest` and `current` at the end to ensure the longest sublist is accounted for .
The `removeItem` method returns false if the specified item does not exist within the array. This return value is significant as it indicates the method's inability to remove any elements, confirming to any calling function or process that the item sought for removal is absent in the current data set. This explicit feedback is crucial for error checking and logic flow in higher-level operations or algorithms relying on potential item presence .
The `buyCar` function sorts the array of car prices in ascending order. It then iterates through the sorted prices, adding each price to a cumulative sum (`sum`). If the cumulative sum with the current car's price does not exceed the budget `k`, it increments a counter (`result`) that tracks how many cars can be purchased. This process continues until adding another car price would exceed the budget, at which point the loop breaks and the function returns the `result` as the number of cars purchased .