Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
C++ Articles
Page 561 of 597
An Insertion Sort time complexity question in C++
What is the time complexity of insertion sort?Time complexity is the amount of time taken by a set of codes or algorithms to process or run as a function of the amount of input.For insertion sort, the time complexity is of the order O(n) i.e. big O of n in best case scenario. And in the average or worst case scenario the complexity is of the order O(n2).What will be the time complexity of sorting when insertion sort algorithm is applied to n sized array of the following form: 6, 5, 8, 7, 10, 9 …… I, i-1The time complexity ...
Read MoreAmortized analysis for increment in counter in C++
Amortized analysis for a sequence of operations is used to determine the run time, the average time required by the sequence. In cannot be treated as an average-case analysis done on the algorithm as it does not always take the average case scenario. There are cases that occur as a worst-case scenario of analysis. So, amortized analysis can be treated as a worst-case analysis for multiple operations in a sequence. Here, the cost of doing each operations in different and for some its high. This problem is a general view using the binary counter.Let’s see the working and implementation in ...
Read MoreMinimize the total number of teddies to be distributed in C++
Problem statementGiven N number of students and an array which represent the mark obtained by students. School has dicided to give them teddy as a price. Hoever, school wants to save money, so they to minimize the total number of teddies to be distrubuted by imposing following constrain −All students must get atleast one teddyIf two students are sitting next to each other then student with the higher marks must get moreIf two students have same marks then they are allowed to get different number of teddiesExampleLet us suppose there are 3 students and marks obtained are represented in array ...
Read MoreMinimize the sum of squares of sum of N/2 paired formed by N numbers in C++
Problem statement Given an array of n elements. The task is to create n/2 pairs in such a way that sum of squares of n/2 pairs is minimal. Example If given array is − arr[] = {5, 10, 7, 4} then minimum sum of squares is 340 if we create pairs as (4, 10) and ( 5, 7) Algorithm 1. Sort the array 2. Take two variables which point to start and end index of an array 3. Calulate sum as follows: sum = arr[start] + arr[end]; sum = sum * sum; ...
Read MoreMinimax Algorithm in Game Theory (Alpha-Beta Pruning) in C++
Description Alpha-Beta pruning is a optimization technique used in minimax algorithm. The idea behind this algorithm is cut off the branches of game tree which need not to be evaluated as better move exists already. This algorithm introduces two new fields − Alpha − This is best value(maximum) that maximizer player can guarantee at current level or its above level Beta − This is the best value(minimum) that minimizer player can guarantee at the current level or its above level. Example If game tree is − arr [] = {13, 8, 24, -5, 23, 15, -14, -20} ...
Read MoreCheck if a pair with given product exists in Linked list in C++
We have a set of elements. And a product K. The task is to check whether there exist two numbers in the linked list, whose product is same as K. If there are two numbers, then print them, if there are more than two numbers, then print any of them. Suppose the linked list is like this {2, 4, 8, 12, 15}, and k value is 16. then it will return (2, 8)We will solve this using the hashing technique. Take one hash table, and mark all elements as 0. Now iteratively mark all elements as 1 in hash table, ...
Read MoreCheck if a given number divides the sum of the factorials of its digits in C++
Suppose, we have an integer, we have to find if the number divides the sum of the factorial of its digits. Suppose a number is 19, the sum of factorial of digits is (1! + 9!) = 362881, this is divisible by 19. To solve this, we will take the number, then calculate factorial of each digit and add the sum, if the sum is divisible by the number itself, then return true, otherwise false. Example #include using namespace std; int factorial(int n){ if(n == 1 || n == 0) return 1; return factorial(n - 1) ...
Read MoreCheck if a cell can be visited more than once in a String in C++
Suppose we have a string with dots (.) and a number, a dot indicates the cell is empty, and if there is a number x in any cell, it indicates that we can move x steps to the right or left within the string. Our task is to check whether we can visit a cell more than once or not. For example, if a string is like "2 . . . 2 . .", then we can visit 4th cell in two different ways. From second cell to two step to right, or from two step left from cell 6. ...
Read MoreCheck if a binary tree is sorted levelwise or not in C++
Here we will see how to check a binary tree is level wise sorted or not. The level wise sorted binary tree will be look like below −In each level, the nodes are sorted from left to right, and each layer contains higher value than its previous level.We can solve this problem by performing level order traversal, and keep track of the minimum and maximum elements of current level. Use another variable prev_max to hold maximum value of the previous level. Then compare the minimum value of current level and maximum value of previous level prev_max. If min value is ...
Read MoreCheck if a binary tree is sorted level-wise or not in C++
Here we will see how to check a binary tree is level wise sorted or not. The level wise sorted binary tree will be look like below −In each level, the nodes are sorted from left to right, and each layer contains higher value than its previous level.We can solve this problem by performing level order traversal, and keep track of the minimum and maximum elements of current level. Use another variable prev_max to hold maximum value of the previous level. Then compare the minimum value of current level and maximum value of previous level prev_max. If min value is ...
Read More