Skip to main content

What is DSA?

Data structure and Algorithm (DSA) is that the building block of the software development process and essential for getting a placement because it helps one solve programming-related problems and helps us in cracking campus placements in no time. No, data structures don’t seem to be hard. It just takes your regular practice to master data structures. Let me explain in simple term,

Data Structure: a knowledge structure could be a way within which you organize your data. Algorithm: An algorithm could be a step-by-step procedure for solving a controversy or polishing off some task.

Reasons Why you ought to Learn Data Structures and Algorithms(DSA)

  • It is fun
  • Solve real-world problems
  • Writing Optimized Code
  • Helps to create your life easier
  • Learn at your own pace
  • Improves Adaptability to Emerging Tech Stacks
  • Helps you crack the highest tech companies.

Now allow us to try and solve a controversy, during this blog i’m trying to unravel a controversy from an array- to seek out the tiniest and largest value in a very given array.

Finding the smallest and largest value in an array:

  1. Brute force approach
  2. Efficient Approach

BRUTE FORCE SOLUTION:

  • If X[i] < small, it means we’ve got found a worth smaller than small value. We update small with X[i] else if X[i] >large, it means we’ve found a price greater than large value. We update large with X[i].
  • By the tip of the loop, the tiniest and enormousst values of the array are going to be stored within the variables small and large. To return this, we take extra array response[] of size two, where store largest on the primary index and smallest on the second index. We return this array as an output.

Time complexity = O(n), Space complexity = O(1)
EFFICIENT APPROACH:

In the first approach, we do two comparisons for each element within the worst-case scenario.

if (X[i] < X[i+1]), then there are often an opportunity that X[i] is but the littlest and X[i+1] is greater than largest.

if (X[i] > X[i+1]), then there will be an opportunity that X[i] may be greater than largest and X[i+1] is but the tiniest.

Steps:

Now we check for the massive and little values. Note the scale of the array and see it the count is odd or maybe. If odd, initialize small and enormous to the primary element. If even, compare the weather and set small to the smaller value and enormous to the larger value

Compare the larger element with large, update large if required. And then compare the smaller element with small, update small if required.

  • Store large and small in response[2] return it.

Time Complexity = O(n)