List Problems: 1 Union
List Problems: 1 Union
Zadid Hasan
February 2020
1 Union
1.1 Array Union
Suppose you are given two arrays A and B. You have to create another array C
such that C = A ∪ B. For example, A = 1, 2, 2, 5, 8 and B = 1, 3, 4, 6, 8. Here
all of the numbers in the arrays will be less than 1000000. Here C will contain
any value that occurs in either of the arrays. So, C = 1, 2, 3, 4, 5, 6, 8.
int [ ] Union(int A[ ], int B[ ])
2 Intersection
2.1 Array Intersection
Suppose you are given two arrays A and B. You have to create another array C
such that C = A ∩ B. For example, A = 1,2,2,5,8 and B = 1,3,4,6,8. Here all
of the numbers in the arrays will be less than 1000000. Here C will contain any
value that occurs in both of the arrays. So, C = 1,8.
int [ ] Intersection(int A[ ], int B[ ])
1
2.2 Circular Array Intersection
Solve the same problem for full circular arrays.
int [ ] Intersection(int A[ ], int startA, int B[ ], int startB)
3 Sort
3.1 Array Sort
You are given an array A. For every 0 ≤ i < A.length, the following holds,
0 ≤ A[i] < 1000000. Sort the array as efficiently as possible.
int [ ] Sort(int A[ ])
4 ZigZag
Suppose you are given two arrays A and B of the same size. You have to create
another new array C such that the 1st element of C is the 1st element of A and
the 2nd element of C is the 1st element of B and the 3rd element of C is the
2nd element of A and the 4th element of C is the 2nd element of B and so on.
For example, if A = 1, 3, 5, 7 and B = 2, 4, 6, 8, then C will be 1, 2, 3, 4, 5, 6,
7, 8.
int [ ] ZigZag(int A[ ], int B[ ])
Also, solve the same problem for circular arrays and linked list.
2
5 Product
You will be given two linked lists of the same size. They will contain integers.
You will find the product list of the two lists. For example if A = 1,2,3 and B
= 4,5,6, then C will be 4, 10, 18.
Node Product(Node A, Node B)
6 Sum
You will be given two linked lists of the same size. They will contain integers.
You will find the sum list of the two lists. For example if A = 1,2,3 and B =
4,5,6, then C will be 5, 7, 9.
Node Sum(Node A, Node B)
7 Difference
You will be given two linked lists of the same size. They will contain integers.
You will find the difference list of the two lists. For example if A = 4,8,2 and B
= 3,5,6, then C will be 1, 3, -4.
Node Difference(Node A, Node B)
8 Dot Product
You will be given two linked lists of the same size. They will contain integers.
You will find the dot-product of the two lists. For example if A = 1,2,3 and B
= 4,5,6, then dot product will be 4+10+18 = 32. Dot product for two lists is
defined as follows, Pn
DP (A, B) = i=1 Ai Bi
int DotProduct(Node A, Node B)
9 Reverse Concatenation
Suppose you are given two lists A and B. First reverse B and then reverse A
and append reversed A after reversed B.
Node RC(Node A, Node B)
3
11 Frequency of Numbers in a List
Suppose you are given an array A where all the elements are positive and smaller
than 1000000. For every unique number, print it’s frequency.
void Frequency(int A[ ])
13 Median Of a List
Suppose you are given an array of numbers. This array has odd numbers of
integers. The median is the value in the middle of the array after the array is
sorted. Find the median of that array.
int Median(int A[ ])
14 toList
Suppose you are given an array. Turn it into a linked list.
Node toList(int A[ ])
15 toArray
Suppose you are given a linked list. Turn this into an array.
int [ ] toArray(Node A)