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
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
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
Types of Variables in C

Types of Variables in C

A variable is a name of the memory location. It is used to store data. Its value can be changed, and it can be reused many times. It is a way to represent memory location through symbol so that it can be easily identified. Let's see the syntax to declare a variable: type variable_name; or for multiple variables: type variable1_name, variable2_name, variable3_name; There are 5 types of variables in C. 1. Local Variable A local variable is a variable which is defined and used inside a particular block of code. There's no existence of it outside of the block. Like all variables,…
Read More
5 Algorithms Every Programmer Should Know

5 Algorithms Every Programmer Should Know

 Programming algorithms are computer procedures that is a lot like a recipe (called a procedure) and tells your computer precisely what steps to take to solve a problem or reach a goal. Algorithms play an important role in the career of a software developer. Wondering why algorithm are important? Gives you an idea if your program actually solves the problem. Gives you an idea about under what circumstances your program will perform well. Gives you an idea what parts of the program are important and what parts can be done any old way. Got an idea about the importance, Now…
Read More

C/C++ Program to Check if a Given String is Palindrome

Palindrome:  A string is called palindrome if its reverse of the string is the same as the original string. For Example: "GEEG", "LOOL","AABBBAA". The most asked questions in TCS/ Wipro/Infosys and other MNC. Program in C/C++ to check the string is Palindrome or Not. #include <iostream> #include <string> using namespace std; int main () { string st; cout<<"Enter String "; cin>>st; int i=0; int l=st.size()-1; while(l>i) { if(st[i++]!=st[l--]) { cout<<"It is not Pallindrome"; return 0; } } cout<<"It is Pallindrome"; return 0; } Input:geeg Output:It is Pallindrome Input:aabbb Output:It is not Pallindrome Compile Program Here  Most Asked Question in Interview.
Read More