Skip to main content

Mountain Array (DSA)

 Finding the peak element in the mountain array using binary search.

(Data Structures and Algorithms)

#include<iostream>

using namespace std;

void MountSearch (int arr[], int size )

{

int start = 0, end = size-1;

int index = 0;

for(int mid = start + (end-start)/2; start <= end; mid = start + (end-start)/2 )

{

if(arr[mid-1]<arr[mid] && arr[mid]>arr[mid+1])

{

index = mid;

break;

}

else if(arr[mid]<arr[mid+1])

{ start = mid+1; }

else if(arr[mid-1]>arr[mid])

{ end = mid; }

}

cout<<"The index of the peak is: "<<index;

}

int main ()

{

int arr[9] = {0,1,2,4,5,3,2,1,0};

MountSearch(arr,9);

return 0;

}

Comments

Popular posts from this blog

What is C++ STL?

 One of the most widely used high-level programming languages is C++. Developers have been using it for a long time, and because of its quicker execution time, all programmers have always enjoyed it, especially competitive programmers. One of C++'s special features that sets it apart from all other programming languages is STL. The term "STL" refers to the standard template library , which is full of pre-defined templates for classes and containers. This makes it very simple for programmers or developers to implement various data structures without having to write all of the code themselves or worry about space-time complexities. If you go a bit further into STL, you will need to grasp the workings of templates, which are among the most powerful tools available for the C++ programming language.

Sum of All Divisors from 1 to N

Introduction In this blog, we will discuss the problem of finding the sum of all divisors from 1 to N. This problem is an observation-based problem with a tint of simple mathematics. We will explore different approaches and algorithms to solve this problem efficiently. Understanding the Problem The problem statement is self-explanatory. Given a number N, we need to find the sum of all its divisors from 1 to N. Brute Force Approach Initially, we can think of solving this problem using a brute force approach. We can iterate from 1 to N and check if each number is a divisor of N. If it is a divisor, we add it to the sum. However, this approach is not optimal for large values of N. Optimized Approach To optimize the solution, we can use a contribution technique. Instead of finding all the divisors individually, we can find the contribution of each number in the sum. Let's understand this technique with an example: For N = 8: 1 can contribute to all numbers from 1 to 8 2 can contribut...

Recursion: Solving Problems through Recursive Functions

  Problem 1: Print Your Name n Times Let's start with a simple problem: printing your name n times using recursion. The generic way to achieve this would be to run a for loop and print your name n times. However, in this case, we want to use recursion. To solve this problem, we will define a recursive function called f(i, n) . The function takes two parameters: i , which represents the current iteration, and n , which represents the total number of times to print your name. We start by taking the input of n from the user. Then, we call the function f(0, n) , which will recursively print your name n times. Here's how the function works: Check if i is greater than n . If it is, return. Print your name. Call the function f(i + 1, n) to recursively print your name n times. By following these steps, we can easily print your name n times using recursion. Problem 2: Print Numbers from 1 to n Next, let's solve a problem where we need to print numbers from 1 to n in...