Sign in

Founder and CEO at EnjoyAlgorithms | Super 30 | IIT BHU | Mentoring Youth for Success



Let’s understand the problem

Given an array X[] of n integers, return true if and only if it is a valid mountain array. The array X[] is a mountain array if and only if n >= 3 and there exists some i with 0 < i < n — 1 such that:

  • X[0] < X[1] <…X[i-1] < X[i]
  • X[i] > X[i+1] > …> X[n — 1]

In other words, we call the array mountain array when the array is strictly increasing and then strictly decreasing.


Input: X[] = [5, 2, 1, 4]
Output: false
Input: X[] = [5, 8, 8]

Difficulty Level

Asked In

Three solutions Discussed

  1. Brute force approach — Using single scan (Increasing the loop by 1)
  2. Divide & Conquer approach — Idea similar to merge sort
  3. Efficient Approach — Using single scan (Increasing the loop by 2)

Key takeaway after reading this blog

  • A good coding interview problem to understand single scan and divide and conquer approach of problem-solving.
  • We are incrementing the loop by two (or constant) to optimize the code further. We can use a similar idea to optimize other coding problems.
  • Base conditions, initialisations, and if statements are intuitive.
  • The time complexities of all three approaches are O(n), but the total comparison count is…

What is Algorithmic Thinking?

Algorithmic Thinking has recently become a buzzword among programmers. It is a method for solving problems based on a clear definition of the steps: logically and repeatedly. The best idea would be to develop this skill independently from learning programming, where proper visualisation can help us understand several efficient problem-solving approaches.

Steps of Algorithmic Thinking

Step1 : Comprehending the problem statement

  • Do we understand every word used within the problem?
  • What data or information are provided as input?
  • What data or results are requested as an output?
  • Understanding and extracting computational and non-computational details relevant to the problem — data structures, particular instances of the problem, the general cases of…

The data structure and algorithm provide a set of approaches to the programmer for handling the data efficiently. If the programmer does not know about data structure and algorithm, they may not write efficient and correct code for their software. It is a fundamental building block of computer science and critical for career success. Let’s understand its importance from two different points of view —

Career point of view — The data structure and algorithm can be used to test both freshers and professionals in the tech industry. …

Difficulty Level

Asked In

Three Solutions Discussed

  1. Brute Force Approach — Using extra space
  2. Using two pointers approach — Double scan
  3. Using two pointers approach — Single scan

Key takeaway after reading this blog

  • The efficient solution is a variation of two pointer approach where pointers are moving in the same direction.
  • A good problem to learn optimisation by improving the space complexity and reducing the extra traversal.
  • We can use similar ideas to solve other coding problems.
  • A good problem for beginners to start coding.

Let’s understand the problem

Given an array X[] of n elements filled with several integers, some of them being zeroes, write a program to…

Let’s discuss something important: how to prepare a learning strategy to achieve interview success? Here is a step-by-step guided strategy:

Preparing the mind

Calculating the big chunk of preparation time

Most programmers need to realize the importance of data structure and algorithms as early as possible in their careers. The knowledge of this subject helps us grow as better programmers with problem-solving in computer science. We should never develop a belief to consider this merely a subject because it is much more than that! Think.

The part of the interview process of Tech Companies

Why tech companies test the knowledge of Data structure and algorithm during the interview process? The answer is obvious — these companies encounter problems daily that are complex and large on the scale of data, and they would like to hire a smart programmer who…

Difficulty Level

Asked in Tech Companies

Merge Sort Introduction

Merge sort is a popular sorting algorithm that uses a divide and conquer approach to sort an array (or list) of integers (or characters or strings). Here are some excellent reasons to learn this algorithm -

  • An excellent algorithm to learn recursion and its analysis using recursion tree method and master theorem.
  • One of the efficient comparison-based sorting algorithms that work in O(nlogn) time complexity in the worst case.
  • The best algorithm for sorting linked lists, i.e., it can sort the linked list in O(nlogn) time complexity in the worst case.
  • We…

Computer science is a field of dream opportunities. All over the world, millions of students are looking forward to pursuing a career in the field of computer science. Though a lot of learning resources are available online, still, most of the students are struggling to become good at it and crack the interview. After working closely with more than 1k students, here I would like to highlight the top 5 learning challenges in computer science.

Solving problems is a practical skill like, let us say, swimming. We acquire any practical skill by imitation and practice. — George Polya

But before…

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store