Found 7347 Articles for C++

Maximum no. of contiguous Prime Numbers in an array in C++

Sunidhi Bansal
Updated on 17-Aug-2020 08:46:30

361 Views

We are given with an array of prime numbers, arranged in random order. The size of the array is N. The goal is to find the longest sequence of contiguous prime numbers in the array.Prime number is the one which has only two factors, 1 and the number itself. 1, 2, 3, 5, 7, 11, 13….are prime numbers whereas 4, 6, 8, 9, 10….20 are non-prime. Every non-prime number has more than 2 factors. Let’s understand with examples.Input − Arr[] = { 1, 3, 5, 2, 6, 7, 13, 4, 9, 10Output − 3Explanation − The prime numbers in the ... Read More

Maximum consecutive repeating character in string in C++

Sunidhi Bansal
Updated on 17-Aug-2020 08:44:50

2K+ Views

We are given a string of alphabets. The task is to find the character which has the longest consecutive repetitions occurring in string. Let’s understand with examples.Input− String[] = “abbbabbbbcdd”Output − bExplanation − In the above string, the longest consecutive sequence is of character ‘b’. Count of consecutive b’s is 4.Input− String[] = “aabbcdeeeeed”Output − bExplanation − In the above string, the longest consecutive sequence is of character ‘e’. Count of consecutive e’s is 5.Approach used in the below program is as followsThe character array string1[] is used to store the string of alphabets.Function maxRepeating(char str[], int n) takes two ... Read More

Maximum consecutive one’s (or zeros) in a binary circular array in C++

Sunidhi Bansal
Updated on 17-Aug-2020 08:43:21

566 Views

We are given with a circular array. Circular array is the array for which we consider the case that the first element comes next to the last element. It is used to implement queues. So we have to count the maximum no. of consecutive 1’s or 0’s in that array.Let’s understand with examples.Input − Arr[] = { 1, 1, 0, 1, 0, 1, 0, 1, 1, 1 }Output − Maximum consecutive 1’s are 5. Or Maximum consecutive 0’s is 1.Explanation − From Arr[] index 7 to 9 and then indexes 0 and 1. 1’s are 5. No consecutive 0’s but ... Read More

Maximum bishops that can be placed on N*N chessboard in C++

Sunidhi Bansal
Updated on 17-Aug-2020 08:39:16

290 Views

We are given an input N which denotes the size of the chessboard. The task here is to find for any value of N, how many bishops can be placed on the NXN chessboard such that no two bishops can attack each other. Let’s understand with examples.Input − N=2Output− Maximum bishops that can be placed on N*N chessboard − 2 ( as shown above )Explanation − As depicted above the only non-contradictory positions are where the bishops are placed. Bishops at-most for 2X2 chessboard.Input − N=5Output− Maximum bishops that can be placed on N*N chessboard: 8 ( as shown above ... Read More

Sum of the series 2 + (2+4) + (2+4+6) + (2+4+6+8) + ... + (2+4+6+8+...+2n) in C++

sudhir sharma
Updated on 14-Aug-2020 14:16:51

186 Views

In this problem, we are given a number n which defines the nth term of the series 2 + (2+4) + (2+4+6) + (2+4+6+8) + ... + (2+4+6+8+...+2n). Our task is to create a program to find the sum of the series.Let’s take an example to understand the problem, Input n = 3OutputExplanation − sum = (2) + (2+4) + (2+4+6) = 2 + 6 + 12 = 20A simple solution to the problem is to use a nested loop. The inner loop finds the ith element of the series and then add up all elements to the sum variable.ExampleProgram to illustrate ... Read More

Sum of the series 1^1 + 2^2 + 3^3 + ... + n^n using recursion in C++

sudhir sharma
Updated on 14-Aug-2020 14:09:35

3K+ Views

In this problem, we are given a number n which defines the nth terms of the series 1^1 + 2^2 + 3^3 + … + n^n. Our task is to create a program that will find the sum of the series.Let’s take an example to understand the problem, Input n = 4Output30Explanation −sum = (1^1) + (2^2) + (3^3) + (4^4) = 1 + 4 + 9 + 16 = 30.To solve this problem, we will loop from 1 to n. Find the square of each number. And add each to the sum variable.AlgorithmInitialize sum = 0 Step 1: Iterate from ... Read More

Sum of the Series 1/(1*2) + 1/(2*3) + 1/(3*4) + 1/(4*5) + ... in C++

sudhir sharma
Updated on 14-Aug-2020 14:04:06

522 Views

In this problem, we are given a number n which is the nth term of the series 1/(1*2) + 1/(2*3) +…+ 1/(n*(n+1)). Our task is to create a program to find the sum of the series.Let’s take an example to understand the problem, Input n = 3Output 0.75Explanation − sum = 1/(1*2) + 1/(2*3) + 1/(3*4) = ½ + ⅙+ 1/12 = (6+2+1)/12 = 9/12 = ¾ = 0.75A simple solution to the problem is using the loop. And commuting value for each element of the series. Then add them to the sum value.AlgorithmInitialize sum = 0 Step 1: Iterate from i = ... Read More

Sum of the series 1, 3, 6, 10… (Triangular Numbers) in C++

sudhir sharma
Updated on 14-Aug-2020 14:00:04

1K+ Views

In this problem, we are given a number n which is given the n of elements of the series 1, 3, 6, 10 … (triangular number). Our task is to create a program to calculate the sum of the series.Let’s brush up about triangular numbers before calculating the sum.Triangular numbers are those numbers that can be represented in the form of a triangle.A triangle is formed in such a way that the first row has one point, second has two, and so on.ExampleLet’s take an example to understand the problem, Inputn = 4OutputExplanation − sum = T1 + T2 + T3 ... Read More

Sum of the Series 1 + x/1 + x^2/2 + x^3/3 + .. + x^n/n in C++

sudhir sharma
Updated on 14-Aug-2020 13:53:23

1K+ Views

In this problem, we are given two numbers X and n, which denote a mathematical series. Our task is to create a program to find the sum of the series 1 + x/1 + x^2/2 + x^3/3 + .. + x^n/n.Let’s take an example to understand the problem, Inputx = 2 , n = 4OutputExplanation −sum= 1 + 2/1 + (2^2)/2 + (2^3)/3 + (2^4)/4 = 1 + 2 + 4/2 + 8/3 + 16/4 = 1 + 2 + 2 + 8/3 + 4 = 9 + 8/3 ... Read More

Sum of the series 1 + (1+3) + (1+3+5) + (1+3+5+7) + + (1+3+5+7+....+(2n-1)) in C++

sudhir sharma
Updated on 14-Aug-2020 13:50:53

393 Views

In this problem, we are given an integer n. Our task is to create a program to find the sum of the series 1 + (1+3) + (1+3+5) + (1+3+5+7) + + (1+3+5+7+....+(2n-1)).From this series, we can observe that ith term of the series is the sum of first i odd numbers.Let’s take an example to understand the problem, Inputn = 3Output 14Explanation −(1) + (1+3) + (1+3+5) = 14A simple solution to this problem is using a nested loop and then add all odd numbers to a sum variable. Then return the sum.ExampleProgram to illustrate the working of our solution, ... Read More

Advertisements