Maximum Number of Contiguous Prime Numbers in an Array in C++

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

485 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

772 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

Java Program for Longest Palindromic Subsequence

AmitDiwan
Updated on 17-Aug-2020 08:42:15

237 Views

For longest Palindromic subsequence, the Java code is as follows −Example Live Demopublic class Demo{    static String longest_seq(String str_1, String str_2){       int str_1_len = str_1.length();       int str_2_len = str_2.length();       char str_1_arr[] = str_1.toCharArray();       char str_2_arr[] = str_2.toCharArray();       int L[][] = new int[str_1_len + 1][str_2_len + 1];       for (int i = 0; i 0){          if (str_1_arr[i - 1] == str_2_arr[j - 1]){             longest_seq[my_index - 1] = str_1_arr[i - 1];       ... Read More

Maximum Bishops on an N x N Chessboard in C++

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

450 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

Count Number of 1s in the Array After N Moves in C

Sunidhi Bansal
Updated on 17-Aug-2020 08:37:01

456 Views

We are given an array of size N. The array has all 0’s initially. The task is to count the no. of 1’s in the array after N moves. Each Nth move has a rule associated. The rules are −1st Move − Change element at positions 1, 2, 3, 4…………..2nd Move − Change element at positions 2, 4, 6, 8…………..3rd Move − Change element at positions 3, 6, 9, 12…………..Count the number of 1’s in the last array.Let’s understand with examples.Input Arr[]={ 0, 0, 0, 0 } N=4Output Number of 1s in the array after N moves − 2Explanation − Array after ... Read More

Maximum Binomial Coefficient Term Value in C

Sunidhi Bansal
Updated on 17-Aug-2020 08:33:47

249 Views

We are given with a positive integer ‘N’. We have to find the maximum coefficient term in all binomial coefficients.The binomial coefficient series is nC0, nC1, nC2, …., nCr, …., nCn-2, nCn-1, nCnfind the maximum value of nCr.nCr = n! / r! * (n - r)!Input − N=4Output − Maximum Coefficient − 6Explanation − 4C0= 1, 4C1 = 4, 4C2 = 6, 4C3 = 4, 4C4 = 1Therefore, the maximum coefficient is 6 in this case.Input − N=5Output − Maximum Coefficient − 10Explanation − 5C0= 1, 5C1 = 5, 5C2 =10, 5C3 = 10, 5C4 = 5, 5C5 = 1Therefore, ... Read More

Maximize Difference Between Two Subsets of a Set with Negatives in C

Sunidhi Bansal
Updated on 17-Aug-2020 08:32:06

265 Views

We are given with an array of positive and negative integers. The task is to find the maximum difference between positive and negative subsets of elements present in the array. As we have subsets of positive and negative numbers. Then the difference (sum of positives) - (sum of negatives) will always be maximum. This is because subtracting negatives will add them. Converting all negatives into positive and adding all the elements of the array will produce the desired result. Let us see examples for understanding −Input − Arr[] = { -2, 0, -3, 8, 10, 12, -4 }Output − Maximized ... Read More

Maximum and Minimum of an Array Using Minimum Comparisons in C

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

13K+ Views

We are given with an array of integers. The task is to find the minimum and maximum element of the array in the minimum number of comparisons.Input Arr[] = { 1, 2, 4, 5, -3, 91 }Output Maximum element : 91 Minimum Element : -3Explanation − Here to minimize the number of comparisons, we will initialize the maximum and minimum element with Arr[0]. And starting from the 2nd element compare each value with min and max and update accordingly.Input Arr[] = { 10, 20, 21, 31, 18, 11 }Output Maximum element : 31 Minimum Element : 10Explanation − Here also, to minimize the number ... Read More

Maximum Absolute Difference of Value and Index Sums in C

Sunidhi Bansal
Updated on 17-Aug-2020 08:28:33

961 Views

We are given with an array of integers. The task is to calculate the maximum absolute difference of value and index sums. That is for each pair of indexes (i, j) in an array, we have to calculate | Arr[i] - A[j] | + |i-j| and find the maximum such sum possible. Here |A| means absolute value of A. If array has 4 elements then indexes are 0, 1, 2, 3 and unique pairs will be ( (0, 0), (1, 1), (2, 2), (3, 3), (0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3) ).Input − Arr[] ... Read More

Advertisements