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
-
Economics & Finance
C++ Articles
Page 565 of 597
Minimize 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 MoreFlip-flop types and their Conversion in C++
Flip-Flops are sequential digital circuits. There are few different types of flip-flops. Here we will see the types of flip-flops and the conversion rules from one flip-flop to another.There are basically four types of flip-flops −SR Flip-FlopD Flip-FlopJK Flip-FlopT Flip-FlopSR Flip-flopSR flip-flop operates with only positive clock transitions or negative clock transitions. Whereas, SR latch operates with enable signal. The circuit diagram of SR flip-flop is shown in the following figure.This circuit has two inputs S & R and two outputs Q(t) & Q(t)’. The operation of SR flipflop is similar to SR Latch. But, this flip-flop affects the outputs ...
Read MoreFind minimum and maximum elements in singly Circular Linked List in C++
Here we will see how to get the minimum and maximum value from one singly circular linked linear list. The basic concept is very simple. The next part of the last node will be pointed to the first node, the first node will also be pointed using start pointer. When we insert some element into the list, after inserting the next part of the newly inserted node will be updated with the address of the start node.Initially the min is assigned with positive infinity, and max is assigned with negative infinity. Now traverse the list from left to right. If ...
Read MoreCheck if a number is multiple of 5 without using / and % operators in C++
Here we will see how to check a number is divisible by 5 or not. One simple approach is that if the number mod 5 = 0, then, the number is divisible by 5. But here we will not use / or % operator. To check whether a number is divisible by 5, we have to see the last number is 0 or 5. If that is 0 or 5, the number is divisible by 5, otherwise not. Here we can use some large numbers also as a string to check.Example#include using namespace std; bool isDiv5(string num){ int ...
Read More