Found 7197 Articles for C++

Maximize number of continuous Automorphic numbers in C++

Sunidhi Bansal
Updated on 17-Aug-2020 09:05:49

305 Views

Given the task is to maximize the number of continuous Automorphic elements in a given array with N number of elements.An automorphic number is a number whose square ends with the same digits as the number itself. For example 5 is an automorphic number as 5*5 = 25 and 25 ends with 5.Let’s now understand what we have to do using an example −Input − arr[]={5, 3, 625, 6, 8, 1}Output − 2Explanation − Automorphic numbers present in the above array are 5, 625, 6 and 1 but the maximum continuous automorphic numbers are {625, 6} which makes the output ... Read More

Maximize the size of array by deleting exactly k sub-arrays to make array prime in C++

Sunidhi Bansal
Updated on 17-Aug-2020 09:03:00

137 Views

Given the task is to delete exactly K sub-arrays from a given array Arr[] with N positive elements such that all the remaining elements in the array are prime and the size of the remaining array is maximum.Input Arr[]={4, 3, 3, 4, 3, 4, 3} , K=2Output 3Explanation − K=2, this means only 2 sub-arrays must be deleted.The sub-arrays deleted are Arr[0] and Arr[3…5] which leaves the array Arr[]={3, 3, 3} with all the prime elements and the maximum size possible.Input Arr[]={7, 6, 2, 11, 8, 3, 12}, K=2Output 3Explanation − The sub-arrays deleted are Arr[1] and Arr[4…6] and the remaining prime array is ... Read More

Maximize the sum of products of the degrees between any two vertices of the tree in C++

Sunidhi Bansal
Updated on 17-Aug-2020 08:59:08

293 Views

Given the task is to construct a tree with a given integer N such that, the sum of degree(x) * degree(y) for all ordered pairs (x, y) is maximum and x is not equal to y.Input −N=5Output −50Explanation    1     \      2       \        3          \           4             \              5 Degree of 1st node = 1 Degree of 2nd node = 2 Degree of 3rd node = 2 Degree of 4th node ... Read More

Maximum length of subarray such that sum of the subarray is even in C++

Sunidhi Bansal
Updated on 17-Aug-2020 08:52:38

919 Views

We are given with an array Arr[] of integers. The goal is to find longest length subarray of Arr[] , sum of whose elements is even. That is, the sum of elements of a subarray is even and that subarray is longest in length.Input − Arr[] = { 2, 3, 5, 2, 6, 7 }.Output −Maximum length of subarray − 4Explanation −The maximum length subarray is { 5, 2, 6, 7 }. Sum is 20 which is even.Input − Arr[] = { 5, 7, 7, 3, 4 }.Output − Maximum length of subarray − 4Explanation − The maximum length subarray ... Read More

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

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

486 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

774 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

453 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

294 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 01-Jul-2025 15:32:28

4K+ Views

In this article, we are given a mathematical series (1^1 + 2^2 + 3^3 + … + n^n) defined by a number n which defines the nth terms of the series. This series can be represented mathematically as: $$ \displaystyle\sum\limits_{k=1}^n k^k $$ The above series does not have any specific mathematical name but is generally referred to as the power tower series. Below is an example of the power tower series up to n. Example The following example calculates the sum of the given series 1^1 + 2^2 + 3^3 + … ... Read More

Advertisements