Algorithms

What are the good websites to learn data structures and algorithms?

What are the good websites to learn data structures and algorithms?

Well! Programming is fun once you get it and the great part is a decent developer gets an enormous check from Top Tech Giants Like (Google, Amazon, Walmart, Microsoft, Facebook, and Apple). Data Structure and algorithms are needed for breaking interviews in these first-rate organizations. Regardless of whether you are a fledgling or middle in Algorithm abilities for the most part learning complete data structure, required 2-3 months. Likewise, getting ready code without help from anyone else is the fundamental model for the arrangement cycle. The following are a few decent assets for learning Data structure and Algorithms: 1. Geeksforgeeks: Geeksforgeeks has an expanse of issues.…
Read More
Search an element in a sorted and rotated array

Search an element in a sorted and rotated array

An element in a sorted array can be found in O(log n) time via binary search. But suppose we rotate an ascending order sorted array at some pivot unknown to you beforehand. So for instance, 1 2 3 4 5 might become 3 4 5 1 2. Devise a way to find an element in the rotated array in O(log n) time. Example:   Input : arr[] = {5, 6, 7, 8, 9, 10, 1, 2, 3}; key = 3 Output : Found at index 8 Input : arr[] = {5, 6, 7, 8, 9, 10, 1, 2, 3}; key = 30…
Read More
Program to cyclically rotate an array by one

Program to cyclically rotate an array by one

Given an array, cyclically rotate the array clockwise by one.  Examples: Input: arr[] = {1, 2, 3, 4, 5} Output: arr[] = {5, 1, 2, 3, 4} Following are steps for Array Rotation.  Store last element in a variable say x. Shift all elements one position ahead. Replace first element of array with x. #include <stdio.h> void rotate(int arr[], int n) { int x = arr[n-1], i; for (i = n-1; i > 0; i--) arr[i] = arr[i-1]; arr[0] = x; } int main() { int arr[] = {1, 2, 3, 4, 5}, i; int n = sizeof(arr)/sizeof(arr[0]); printf("Given array is\n"); for…
Read More
Program to find the pair with a given sum in an array

Program to find the pair with a given sum in an array

We are given an array that is sorted and then rotated around an unknown point. Find if the array has a pair with a given sum ‘x’. It may be assumed that all elements in the array are distinct. Examples :  Input: arr[] = {11, 15, 6, 8, 9, 10}, x = 26 Output: true There is a pair (11, 15) with sum 26 Input: arr[] = {11, 15, 26, 38, 9, 10}, x = 35 Output: true There is a pair (26, 9) with sum 35 Input: arr[] = {11, 15, 26, 38, 9, 10}, x = 52 Output:…
Read More
C Program to implement Binary Search

C Program to implement Binary Search

We are given a sorted array of size n. We have to write program to find an element x in arr[]. In the previous post we have implemented linear search. Now we are going to implement another approach to search it. This approach is called Binary Search. Binary Search - Search an element in sorted array by repeatedly dividing the search interval in half. Begin with an interval covering the whole array. If the value of the search key is less than the item in the middle of the interval, narrow the interval to the lower half. Otherwise, narrow it…
Read More
C Program to search an element in array

C Program to search an element in array

Write a C program to input elements in array and search whether an element exists in array or not. How to search element in array linearly in C programming. Logic to search element in array sequentially in C program. Example Input Input size of array: 10 Input elements in array: 10, 12, 20, 25, 13, 10, 9, 40, 60, 5 Output Element to search is: 25 Element found at index 3 Algorithm to search an element in the array There are two ways to search element in an array :- 1) Linear Search 2) Binary Search. In this post you'll…
Read More
C Program to delete an element from the array

C Program to delete an element from the array

In this Post we're going to learn how to delete an element from the array in 2 conditions Position of the element to be deleted is given.Elements to be deleted is given. Position of the element to be deleted is given. This C program is to delete an element from an array from a specified location/ position. For example, if an array a consists of elements a={7,8,12,3,9} and if we want to delete element at position 3 then the new array would be a={7,8,12,9} (as array starts from index 0). Logic Take input array ‘a’ and no of elements(n) as 5 Let us take…
Read More
Linear Search

Linear Search

Given an array of n elements. We have to write a function to search an item X in the given array Arr[n] Examples : Input : arr[] = {10, 20, 80, 30, 60, 50,110, 100, 130, 170}x = 110;Output : 6Element x is present at index 6 Input : arr[] = {10, 20, 80, 30, 60, 50,110, 100, 130, 170}x = 175;Output : -1Element x is not present in arr[]. A simple approach is to do a linear search , i.e   Start from the leftmost element of arr[] and one by one compare x with each element of arr[]If x matches…
Read More
Tower of Hanoi Program in C

Tower of Hanoi Program in C

Tower of Hanoi is a mathematical puzzle where we have three towers and n disks. The objective of the puzzle is to move the entire stack to another tower, obeying the following simple rules:  Only one disk can be moved at a time.Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack i.e. a disk can only be moved if it is the uppermost disk on a stack.No disk may be placed on top of a smaller disk. Approach Take an example for 2 disks : Let tower 1…
Read More
Program to find Roots of a Quadratic Equation

Program to find Roots of a Quadratic Equation

In this post You'll be learning how to write a program for finding out the roots of a quadratic equation in various programming language. Standard form a quadratic equation is ax2 +bx + c=0 where a, b, c are real numbers and a!=0 Shridhara's Formula According to Shridhara's formula b2-4ac is the value of the term discriminant in quadratic equation. Value of discriminant defines nature of roots. If value of discriminant is greater than 0 then equation has real roots.Discriminant is 0 then equation has equal and real roots.If the discriminant is less than 0, the roots are complex and different.…
Read More