0% found this document useful (0 votes)
7 views7 pages

Array: Array Data Structure Same Data Type

dsa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views7 pages

Array: Array Data Structure Same Data Type

dsa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Array

An array is a data structure that stores a collection of elements (values) of the same data type in a
contiguous block of memory. It allows efficient access to its elements using an index.

Features of an Array:

1. Data Type: All elements in an array must be of the same type (e.g., int, float, char).

2. Indexing: Each element is identified by its index, starting from 0.

3. Contiguous Memory: All elements are stored in consecutive memory locations.

4. Efficient Access: Any element can be accessed in O(1) time using its index.

2D Array:
A 2D array is a data structure that stores data in rows and columns, resembling a matrix. It is essentially
an array of arrays.

Features of a 2D Array:

1. Data Type: All elements in a 2D array must be of the same type.

2. Indexing: Elements are accessed using two indices: one for the row and another for the column.

3. Contiguous Memory: Though conceptually a matrix, the elements are stored linearly in
memory.

• Linear Search

• Largest Element

• Second Largest

• Largest Three

• Leaders in an array

• Check if Sorted

• Remove Duplicates from Sorted

• Reverse an Array

* সময় পাইলে কলয়কটা প্রবলেম করিস।


Recursion
Recursion is a programming technique where a function calls itself directly or indirectly to solve a
problem. It is often used to break down a complex problem into smaller, more manageable subproblems
that are of the same type. A recursive function typically consists of two main parts:

1. Base Case: A condition that stops the recursion when met.

2. Recursive Case: The part of the function where it calls itself to solve a smaller instance of the
problem.

Example of Recursion

To calculate the factorial of a number n, recursion can be used:

int factorial(int n) {

if (n == 0) { // Base case

return 1;

return n * factorial(n - 1); // Recursive case

Avoiding Infinite Recursion


Infinite recursion occurs when a recursive function never reaches its base case. This can lead to stack
overflow errors because each function call consumes stack memory.

To avoid infinite recursion:

1. Ensure a Base Case: Always define a condition that stops further recursive calls.

For example: if (n == 0) return 1;

2. Progress Towards the Base Case: Modify the arguments in the recursive call so they move
closer to the base case. For example:

return n * factorial(n - 1);

Here, n decreases with each call.

Example of Infinite Recursion


This function will cause infinite recursion because it never progresses towards a base case:

void infiniteRecursion() {

infiniteRecursion(); // No base case

What is the base condition in recursion?


In the recursive program, the solution to the base case is provided and the solution to the
bigger problem is expressed in terms of smaller problems.

How are recursive functions stored in memory?


Recursion uses more memory, because the recursive function adds to the stack with each
recursive call, and keeps the values there until the call is finished. The recursive function uses
LIFO (LAST IN FIRST OUT) Structure just like the stack data structure.

How a particular problem is solved using recursion?


The idea is to represent a problem in terms of one or more smaller problems, and add one or
more base conditions that stop the recursion. For example, we compute factorial n if we know
the factorial of (n-1). The base case for factorial would be n = 0. We return 1 when n = 0.

Why Stack Overflow error occurs in recursion?


If the base case is not reached or not defined, then the stack overflow problem may arise. Let
us take an example to understand this.

int fact(int n)
{
// wrong base case (it may cause
// stack overflow).
if (n == 100)
return 1;
else
return n*fact(n-1);
}

If fact(10) is called, it will call fact(9), fact(8), fact(7), and so on but the number will never reach
100. So, the base case is not reached. If the memory is exhausted by these functions on the
stack, it will cause a stack overflow error.
How memory is allocated to different function calls in recursion?
When any function is called from main(), the memory is allocated to it on the stack. A recursive
function calls itself, the memory for a called function is allocated on top of memory allocated to
the calling function and a different copy of local variables is created for each function call.
When the base case is reached, the function returns its value to the function by whom it is
called and memory is de-allocated and the process continues.

Recursion VS Iteration

SR
Recursion Iteration
No.

Terminates when the base case becomes Terminates when the condition
1)
true. becomes false.

2) Used with functions. Used with loops.

Every recursive call needs extra space in the Every iteration does not require any
3)
stack memory. extra space.

4) Smaller code size. Larger code size.

# রিকািশন এি উপি ববরসক প্রবলেম কলয়কটা করিস, বে কয়টা বেওয়া আলে।


Divide and Conquer Algorithm
Divide and Conquer Algorithm is a problem-solving technique used to solve problems by
dividing the main problem into subproblems, solving them individually and then merging them
to find solution to the original problem.

Divide and Conquer Algorithm can be divided into three steps: Divide, Conquer and Merge .

1. Divide:
Break down the original problem into smaller subproblems.

Each subproblem should represent a part of the overall problem.

The goal is to divide the problem until no further division is possible.

2. Conquer:
Solve each of the smaller subproblems individually.

If a subproblem is small enough (often referred to as the “base case”), we solve it directly
without further recursion.

The goal is to find solutions for these subproblems independently.

3. Merge:
Combine the sub-problems to get the final solution of the whole problem.

Once the smaller subproblems are solved, we recursively combine their solutions to get the
solution of larger problem.

The goal is to formulate a solution for the original problem by merging the results from the
subproblems.

3 ta problem practice koris:


1. Finding the maximum element in the array.

2. Finding the minimum element in the array.

3. Merge Sort:
Some Question:
1. What is the Divide and Conquer algorithm?
Divide and Conquer is a problem-solving technique where a problem is divided into smaller,
more manageable subproblems. These subproblems are solved recursively, and then their
solutions are combined to solve the original problem.

2. What are the key steps involved in the Divide and Conquer algorithm?
The main steps are:

Divide: Break the problem into smaller subproblems.

Conquer: Solve the subproblems recursively.

Combine: Merge or combine the solutions of the subproblems to obtain the solution to the
original problem.

3. What are some examples of problems solved using Divide and Conquer?
Divide and Conquer Algorithm is used in sorting algorithms like Merge Sort and Quick Sort,
finding closest pair of points, Strassen’s Algorithm, etc.

4. How does Merge Sort use the Divide and Conquer approach?
Merge Sort divides the array into two halves, recursively sorts each half, and then merges the
sorted halves to produce the final sorted array.

5. What is the time complexity of Divide and Conquer algorithms?


The time complexity varies depending on the specific problem and how it’s implemented.
Generally, many Divide and Conquer algorithms have a time complexity of O(n log n) or
better.

## একটাও কমন পড়লব না ইনশাআল্লাহ । আরমন 🤲

You might also like