https://www.algopedia.ro/wiki/index.php?title=Problems_Involving_Arrays&feed=atom&action=historyProblems Involving Arrays - Revision history2021-08-03T21:23:28ZRevision history for this page on the wikiMediaWiki 1.34.0https://www.algopedia.ro/wiki/index.php?title=Problems_Involving_Arrays&diff=7375&oldid=prevCristian at 06:09, 29 September 20132013-09-29T06:09:01Z<p></p>
<p><b>New page</b></p><div>= Problems with Arrays =<br />
<br />
This is a small list of exercises involving sequences of numbers, for beginners. However, they require storage, thus they have solutions using arrays. Some of the very introductory problems may be solved without using arrays. They are here to familiarize you with arrays, so solve them using arrays, please.<br />
<br />
== Problems with arrays ==<br />
<br />
=== Array Sum ===<br />
Calculate the sum of all elements in an array.<br />
=== Search Array ===<br />
Given an array with <code>n</code> elements and an extra element <code>e</code> find the index in the array where element <code>e</code> appears first.<br />
=== Minimum, Maximum ===<br />
Find the value and position of the minimum and maximum elements of an array. For extra credit do it with <code>3 * n / 2</code> comparisons (worst case).<br />
=== Minimum, Maximum Multiple ===<br />
Find the value of the minimum and maximum elements in an array and the number of times they appear. Try to solve it using just one pass over the array.<br />
=== Insert in Array ===<br />
Given an array of <code>n</code> integers, another integer <code>e</code> and a position <code>k</code>, insert integer <code>e</code> in the array at position <code>k</code>.<br />
=== Delete from Array ===<br />
Given an array of <code>n</code> integers and a position <code>k</code> delete element at position <code>k</code>.<br />
=== Reverse Array ===<br />
Given an array with <code>n</code> elements reverse the order of elements. That means change the order of the elements in the array such that the last element is now first, the second to last is now second, and so on.<br />
=== Rotate Array ===<br />
Given an array with <code>n</code> elements, rotate the array to the left by one position. That means that, in the end, the second element should be the first, the third should be the second, and so on. The first element goes to the last position. Example: <code>[1, 6, 2, 7, 4, 3, 2] goes to [6, 2, 7, 4, 3, 2, 1]</code>.<br />
<br />
=== Rotate Array Multiple <sup>h</sup> ===<br />
The same problem, but you have to rotate the array by k positions. The problem is hard(ish) when solved without an additional array and in linear time.<br />
=== Binary Search ===<br />
Given an array of numbers sorted in ascending order find the position of a given element.<br />
=== Prime Numbers Generation ===<br />
Given a positive integer <code>n</code> generate and display all prime numbers less than or equal to <code>n</code>.<br />
=== Select Sort ===<br />
Given an array of <code>n</code> numbers sort the array using selection. Sorting means that in the end the numbers should be in ascending order in the array. Using selection means going through the array, finding the maximum number, then swapping the maximum with the last element. Then reiterate with the array minus the last element. Repeat with smaller and smaller parts of the array.<br />
=== Insert Sort ===<br />
Sort an array using insertion. Take second element and insert it in <code>A[1..1]</code>, making <code>A[1..2]</code> sorted. Then insert third element in <code>A[1..2]</code>. Keep inserting element on position <code>k</code> into sorted array <code>A[1..k-1]</code> up to <code>k = n</code>.<br />
=== Moving Zeroes ===<br />
Change an array of integers such that in the end all zero elements are the last ones in the array.<br />
=== Set Array ===<br />
Change an array into a Set array. Meaning, eliminate all duplicates, in place, without using another array.<br />
=== GCD on Array ===<br />
Compute the GCD (greatest common divider) of all elements in an array.<br />
=== Conversion to another base ===<br />
Given number <code>n</code> and base <code>b, 1 < b < 17</code>, convert and display its conversion to base <code>b</code>.<br />
=== Eval Poly ===<br />
Given a polynomial stored in an array, least significant coefficent first, and a number <code>x</code> evaluate the value of the polynomial in point <code>x</code>.<br />
=== Substring Search <sup>h</sup>===<br />
Given two arrays, <code>s</code> (search array) and <code>p</code> (pattern array) find out how many time p appears in s. For instance, if <code>s = [1, 2, 1, 2, 1, 3, 1, 2, 1]</code> and <code>p = [1, 2, 1]</code> then p <br />
appears three times in s. The problem is hard only when solved in linear time.<br />
=== Counting Overlaps <sup>h</sup> ===<br />
Given two bracelets made of <code>n</code> black and white beads each, find the number of overlaps bead to bead such that overlapping beads have the same color. You can rotate the bracelets when setting them on top of each other. The problem is only hard when solved in linear time.<br />
=== Array Comparison ===<br />
Given two arrays of different length find their lexicographical order (meaning which one should appear first in the dictionary). <br />
=== Set operations ===<br />
Do the basic operations on sets: intersection, union, difference. Sets are stored as arrays with unique elements, in no particular order.<br />
=== Sorted Sets Operations ===<br />
The same requirement as the previous exercise, but this time the sets are stored as arrays with unique sorted elements<br />
=== Bit Sets Operations ===<br />
The same requirement as the previous exercise, but this time the sets are stored as arrays with binary elements. Array[i] is 1 if element i is part of the set, 0 otherwise.<br />
=== Merge Arrays ===<br />
Given two sorted arrays, build a third sorted array consisting of all the elements in the first two arrays. Duplicates are allowed.<br />
=== Big Number Addition, Subtraction, Multiplication ===<br />
Given two big numbers stored as arrays, one decimal digit per element, compute the sum, difference and product of these numbers.<br />
=== Selection <sup>h</sup>===<br />
Given an array and a position in the array find the number that sits in that position when the array is sorted. The problem is hard when solved in worst case linear time.<br />
=== Quicksort <sup>h</sup> ===<br />
Sort an array using Quicksort method. Considered hard because it needs recursion.<br />
=== Merge Sort ===<br />
Sort an array using Merge Sort method.<br />
=== Bicriterial Sort ===<br />
Given two arrays of integers, <code>E</code> and <code>W</code>, where <code>E[i]</code> is just a number and <code>W[i]</code> is the weight of <code>E[i]</code>, sort the arrays such that numbers in E are in ascending order and if two numbers in E are equal then the one with higher weight should come first.<br />
=== Majority Element <sup>h</sup> ===<br />
A majority element in an array a[] of size n is an element that appears more than n/2 times (and hence there is at most one such element). Given an array find if it has a majority element. The problem is somewhat hard when solved in linear time.<br />
<br />
<br />
<br />
<br />
''Legend:''<br />
* '''<sup>h</sup>''' means '''h'''ard. The problem is difficult.</div>Cristian