C

Data Structure & Algorithms Interview Questions

Data Structure & Algorithms Interview Questions

In this post we will be providing some questions frequently asked in Interview. Comment their answers below Arrays How do you find the missing number in a given integer array of 1 to 100?How do you find the duplicate number on a given integer array?How do you find the largest and smallest number in an unsorted integer array?How do you find all pairs of an integer array whose sum is equal to a given number?How do you find duplicate numbers in an array if it contains multiple duplicates?How are duplicates removed from a given array in Java? Linked List How…
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
Free Popular APIs for your projects

Free Popular APIs for your projects

An application programming interface(APIs) is a connection between computers or between computer programs. It is a type of software interface, offering a service to other pieces of software. A document or standard that describes how to build such a connection or interface is called an API specification. There are a lot of opensource APIs available over internet. Here I'm going to tell you about some of them. Open Weather Map API Open weather map API provides the current weather information for any location including over 200K cities. This API is one of the very useful API for the programming beginners…
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
Block Swap algorithm for array rotation

Block Swap algorithm for array rotation

Write a function rotate(ar[], d, n) that rotates arr[] of size n by d elements using block swap algorithm Algorithm : Initialize A = arr[0..d-1] and B = arr[d..n-1]1) Do following until size of A is equal to size of B a) If A is shorter, divide B into Bl and Br such that Br is of samelength as A. Swap A and Br to change ABlBr into BrBlA. Now Ais at its final place, so recur on pieces of B. b) If A is longer, divide A into Al and Ar such that Al is of samelength as B…
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
C Program to Insert an element in array

C Program to Insert an element in array

An array is a collection of items stored at contiguous memory locations. In this article, we will see how to insert an element in an array in C.Given an array arr of size n, this article tells how to insert an element x in this array arr at a specific position pos. For example we have to insert 100 at 3rd position. Algorithm Here’s how to do it.  First get the element to be inserted, say xThen get the position at which this element is to be inserted, say posThen shift the array elements from this position to one position forward, and do this for all the other elements next to…
Read More