You are on page 1of 4

Study Material for Contest

1D array
An array is a collection of items stored at contiguous memory locations. The idea is to store
multiple items of the same type together. This makes it easier to calculate the position of
each element by simply adding an offset to a base value, i.e., the memory location of the first
element of the array (generally denoted by the name of the array). The base value is index 0
and the difference between the two indexes is the offset.
For simplicity, we can think of an array as a fleet of stairs where on each step is placed a
value (let’s say one of your friends). Here, you can identify the location of any of your friends
by simply knowing the count of the step they are on.
Remember: “Location of next index depends on the data type we use”.

Array’s size
In C language, array has a fixed size meaning once the size is given to it, it cannot be
changed i.e. you can’t shrink it neither can you expand it. The reason was that for
expanding, if we change the size we can’t be sure ( it’s not possible every time) that we get
the next memory location to us as free. The shrinking will not work because the array, when
declared, gets memory statically allocated, and thus compiler is the only one can destroy it.
Types of indexing in an array:
● 0 (zero-based indexing): The first element of the array is indexed by a subscript of 0.
● 1 (one-based indexing): The first element of the array is indexed by the subscript of
● n (n-based indexing): The base index of an array can be freely chosen. Usually,
programming languages allowing n-based indexing also allow negative index values,
and other scalar data types like enumerations, or characters may be used as an
array index.

Question 1
https://www.geeksforgeeks.org/reversal-algorithm-right-rotation-array/

Question 2
An element is called a peak element if its value is not smaller than the value of its adjacent
elements(if they exists).
Given an array arr[] of size N, find the index of any one of its peak elements.
Note: The generated output will always be 1 if the index that you return is correct. Otherwise
output will be 0.

Example 1:

Input:
N = 3
arr[] = {1,2,3}
Output: 2
Explanation: index 2 is 3.
It is the peak element as it is
greater than its neighbour 2.

Example 2:

Input:
N = 2
arr[] = {3,4}
Output: 1
Explanation: 4 (at index 1) is the
peak element as it is greater than
its only neighbour element 3.

2-D arrays

Question1

Given a boolean 2D array, where each row is sorted. Find the row with the maximum
number of 1s.

Example:

Input matrix

0 1 1 1

0 0 1 1

1 1 1 1 // this row has maximum 1s


0 0 0 0

Output: 2

A simple method is to do a row wise traversal of the matrix, count the number of 1s in each
row and compare the count with max. Finally, return the index of row with maximum 1s. The
time complexity of this method is O(m*n) where m is number of rows and n is number of
columns in matrix.

Time Complexity: O(m*n)

Space Complexity: O(1)

Question 2:
Given a matrix, the task is to find the maximum element of each row.
Examples:

Input : [1, 2, 3]
[1, 4, 9]
[76, 34, 21]

Output :
3
9
76

Input : [1, 2, 3, 21]


[12, 1, 65, 9]
[1, 56, 34, 2]
Output :
21
65
56
Approach : Approach is very simple. The idea is to run the loop for no_of_rows. Check each
element inside the row and find for the maximum element. Finally, print the element.

Quick Sort:
Question 1:
Given an array A of size n, and an integer k. Find the maximum force by involving only k
elements. Force of an element is the square of its value.

Elements are not needed to be continuous.

Approach: Sort the elements and then find the sum of square of last K elements.

Question 2:
Given an array of N integers, You have to find the maximum product of two integers.

Sample Input:-
5
-1 -2 3 4 -5

Sample Output:-
12

Explanation:-
4*3 = 12

Sample Input:-
4
-1 -1 1 1

Sample Output:-
1

Approach: Sort the given array and print the product of last two integers.

You might also like