Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


C++ Programming Code Examples

C++ > Sorting Searching Code Examples

Find the maximum subarray sub using Divide and Conquer Approach

/* Find the maximum subarray sub using Divide and Conquer Approach - This algorithm implements the divide and conquer approach to find the sub-array having a maximum sum. - The worst case time complexity of the algorithm is O(n*log(n)). - Take the input of the integer array. - Using divide and conquer approach break the array. - Compute the individual sum and combine them, to get the global maximum sum. - Exit. */ #include<iostream> using namespace std; // A function to find the maximum of two integers. int max(int a, int b) { return (a > b)? a:b; } // A function to find the maximum sum sub-array which includes mid of the sub-array. int MaxCrossingSum(int arr[], int low, int mid, int high) { // Include elements having index value less than or equal to the mid. int sum = 0; int leftpartsum = -1; for (int i = mid; i >= low; i--) { sum = sum + arr[i]; if (sum > leftpartsum) leftpartsum = sum; } // Include elements having index value greater mid. sum = 0; int rightpartsum = -1; for (int i = mid+1; i <= high; i++) { sum = sum + arr[i]; if (sum > rightpartsum) rightpartsum = sum; } // Return sum of elements on left and right of mid. return leftpartsum + rightpartsum; } // Returns sum of maxium subarray sum. int MaxSubArraySum(int arr[], int low, int high) { int mid; // If low index is equal to the high index h then the subarray contains only one element. if (low == high) return arr[low]; // Otherwise find the mid index and proceed. mid = (low + high)/2; // Maximum sum sub-array can be either in the left part, right part or covering elements from both parts. return max(max(MaxSubArraySum(arr, low, mid), MaxSubArraySum(arr, mid+1, high)), MaxCrossingSum(arr, low, mid, high)); } int main() { int n, i; cout<<"Enter the number of data element in the array: "; cin>>n; int a[n]; for(i = 0; i < n; i++) { cout<<"Enter element "<<i+1<<": "; cin>>a[i]; } // Print the maximum sub-array sum. cout<<"\nMaximum sub-array sum is: "<<MaxSubArraySum(a, 0, n-1); return 0; }

#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program.

Consider a situation, when we have two persons with the same name, jhon, in the same class. Whenever we need to differentiate them definitely we would have to use some additional information along with their name, like either the area, if they live in different area or their mother's or father's name, etc. Same situation can arise in your C++ applications. For example, you might be writing some code that has a function called xyz() and there is another library available which is also having same function xyz(). Now the compiler has no way of knowing which version of xyz() function you are referring to within your code.

The cin object is used to accept input from the standard input device i.e. keyboard. It is defined in the iostream header file. C++ cin statement is the instance of the class istream and is used to read input from the standard input device which is usually a keyboard. The extraction operator(>>) is used along with the object cin for reading inputs. The extraction operator extracts the data from the object cin which is entered using the keyboard. The "c" in cin refers to "character" and "in" means "input". Hence cin means "character input". The cin object is used along with the extraction operator >> in order to receive a stream of characters.

Compute natural logarithm. Returns the natural logarithm of x. The natural logarithm is the base-e logarithm: the inverse of the natural exponential function (exp). For common (base-10) logarithms, see log10. The C/C++ library function double log(double x) returns the natural logarithm (base-e logarithm) of x. Function returns natural logarithm of x. If x is negative, it causes a domain error. If x is zero, it may cause a pole error (depending on the library implementation).

A relational operator is used to check the relationship between two operands. C++ Relational Operators are used to relate or compare given operands. Relational operations are like checking if two operands are equal or not equal, greater or lesser, etc. Relational Operators are also called Comparison Operators.

In computer programming, we use the if statement to run a block code only when a certain condition is met. An if statement can be followed by an optional else statement, which executes when the boolean expression is false. There are three forms of if...else statements in C++: • if statement, • if...else statement, • if...else if...else statement, The if statement evaluates the condition inside the parentheses ( ). If the condition evaluates to true, the code inside the body of if is executed. If the condition evaluates to false, the code inside the body of if is skipped.

In computer programming, loops are used to repeat a block of code. For example, when you are displaying number from 1 to 100 you may want set the value of a variable to 1 and display it 100 times, increasing its value by 1 on each loop iteration. When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop. A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.

An array is defined as the collection of similar type of data items stored at contiguous memory locations. Arrays are the derived data type in C++ programming language which can store the primitive type of data such as int, char, double, float, etc. It also has the capability to store the collection of derived data types, such as pointers, structure, etc. The array is the simplest data structure where each data element can be randomly accessed by using its index number. C++ array is beneficial if you have to store similar elements. For example, if we want to store the marks of a student in 6 subjects, then we don't need to define different variables for the marks in the different subject. Instead of that, we can define an array which can store the marks in each subject at the contiguous memory locations.

The cout is a predefined object of ostream class. It is connected with the standard output device, which is usually a display screen. The cout is used in conjunction with stream insertion operator (<<) to display the output on a console. On most program environments, the standard output by default is the screen, and the C++ stream object defined to access it is cout. The "c" in cout refers to "character" and "out" means "output". Hence cout means "character output". The cout object is used along with the insertion operator << in order to display a stream of characters.

A program shall contain a global function named main, which is the designated start of the program in hosted environment. main() function is the entry point of any C++ program. It is the point at which execution of program is started. When a C++ program is executed, the execution control goes directly to the main() function. Every C++ program have a main() function.




Capacity is the capacity of the "binary heap". Duplicates are allowed. "Throw Overflow" if container is full. Find the smallest item in the "priority queue". Return the smallest item, or





Firstly We need to find k numbers which have a minimum difference with the median of the data set. It includes sorting using 'Quick Sort' and then printing k numbers as a result. Time


For example 6 is Perfect Number since divisor of 6 are 1, 2 and 3. Sum of its divisor is 1 + 2+ 3 =6 and 28 is also a 'Perfect Number' since 1+ 2 + 4 + 7 + 14= 28. Other 'Perfect Numbers': 496