Found 7347 Articles for C++

An Uncommon representation of array elements in C++ program

sudhir sharma
Updated on 24-Oct-2019 08:11:58

74 Views

An array is a linear data structure that stores elements the same data type. To access a single data element of the array, there is a standard way that is commonly used.Syntaxarray_name[index];Example Live Demo#include using namespace std; int main( ){    int arr[2] = {32, 65};    printf("First Element = %d", arr[0]);    printf("Second Element = %d", arr[1]);    return 0; }OutputFirst Element = 32 Second Element = 65Now, there is another method that can provide the same output as the above.Syntaxindex[array_name];Example Live Demo#include using namespace std; int main( ){    int arr[2] = {32, 65};    printf("First Element = ... Read More

An interesting time complexity question in C++

sudhir sharma
Updated on 24-Oct-2019 08:09:38

796 Views

Time complexity can be defined as the time required by the algorithm to run its average case.Let's see and calculate the time complexity of some of the basic functions.Methodvoid counter(int n){    for(int i = 0 ; i < n ; i++){       for(int j = 1 ; j

An interesting method to print reverse of a linked list in C++

sudhir sharma
Updated on 24-Oct-2019 08:05:25

87 Views

A linked list is a data structure that stores data elements in linked form. Each node of the linked list has a data element and a link.Print reverse of a linked list is a common problem that needs to be addressed in problem solving. So, here we will learn an interesting way to print reverse of a linked list in c++ programming language.Generally print a reverse linked list needs modifications in the list or multiple traversing of the list but this method does not require any such things and also traverses the linked list only once.The logic of this method ... Read More

An Insertion Sort time complexity question in C++

sudhir sharma
Updated on 24-Oct-2019 08:01:24

855 Views

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 More

Amortized analysis for increment in counter in C++

sudhir sharma
Updated on 24-Oct-2019 07:54:31

411 Views

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 More

Minimum and Maximum number of pairs in m teams of n people in C++

Arnab Chakraborty
Updated on 23-Oct-2019 09:15:43

216 Views

Problem statementN participants of the competition were split into M teams in some manner so that each team has at least one participant. After the competition each pair of participants from the same team became friends.Your task is to write a program that will find the minimum and the maximum number of pairs of friends that could have formed by the end of the competition.Algorithm1. We can obtain max pairs using below formula: maxPairs = ((n – m) * (n – m + 1)) / 2 2. We can obtain min pairs using below formula: minPairs = m * (((n ... Read More

Minimizing array sum by applying XOR operation on all elements of the array in C++

Arnab Chakraborty
Updated on 23-Oct-2019 09:11:59

169 Views

DescriptionGiven an array of size, N. Find an element X such that the sum of array elements should be minimum when XOR operation is performed with X and each element of an array.If input array is: arr [] = {8, 5, 7, 6, 9} then minimum sum will be 30 Binary representation of array elments are: 8 : 1000 5 : 0101 7 : 0111 6 : 0101 9 : 1001 If X = 5 then after performing XOR sum will be 30: 8 ^ 5 = 13 5 ^ 5 = 0 7 ^ 5 = 2 6 ^ ... Read More

Minimize the total number of teddies to be distributed in C++

Narendra Kumar
Updated on 22-Oct-2019 12:23:51

64 Views

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 More

Minimize the sum of squares of sum of N/2 paired formed by N numbers in C++

Narendra Kumar
Updated on 22-Oct-2019 12:16:10

69 Views

Problem statementGiven 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.ExampleIf 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)Algorithm1. 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; 4. Repeate this procedure till start < end and increment minSum as ... Read More

Minimax Algorithm in Game Theory (Alpha-Beta Pruning) in C++

Narendra Kumar
Updated on 22-Oct-2019 12:09:07

2K+ Views

DescriptionAplha-Beta pruning is a optimization technique used in minimax algorithm. The idea benind 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 guaratee at current level or its above levelBeta − This is the best value(minimum) that minimizer player can guaratee at the current level or its above level.ExampleIf game tree is −arr [] = {13, 8, 24, -5, 23, 15, -14, -20}then optimal value will be 13 if maximizer plays firstAlgorithm1. Start ... Read More

Advertisements