Found 33676 Articles for Programming

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

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

454 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

Java program to find the sum of a series 1/1! + 2/2! + 3/3! + 4/4! +…….+ n/n!

AmitDiwan
Updated on 30-Aug-2024 11:42:26

1K+ Views

In this program, we'll compute the sum of the series 1/1! + 2/2! + 3/3! + ... + n/n! using Java. This involves calculating factorials and summing the results of each term divided by its factorial. We'll use Java's basic arithmetic, loop control, and built-in classes to achieve this.Problem StatementWrite a Java program to calculate the sum of the series 1/1! + 2/2! + 3/3! + ... + n/n! and print the result.Steps to find the sum of a seriesFollowing are the steps to find the sum of a series −Import necessary classes from java.io and java.lang package.Initialize variables for the sum and ... Read More

Count number of 1s in the array after N moves in C

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

458 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

251 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

Java Program for Longest Palindromic Subsequence

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

239 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

Maximize the difference between two subsets of a set with negatives in C

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

268 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 number of 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

969 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

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