Found 26504 Articles for Server Side Programming

Maximum product quadruple (sub-sequence of size 4) in array in C++

Ayush Gupta
Updated on 15-Sep-2020 14:18:33

392 Views

In this problem, we are given an array arr[]. Our task is to create a program to find the Maximum product quadruple (sub-sequence of size 4) in array in C++.Code description − Here, we need to find a quadruple (sub-sequence of size 4) such that the product of all elements is maximum.Let’s take an example to understand the problem, Inputarr[] = {4, -2, 5, -6, 8}Output840ExplanationThe quadruple, (-3, 5, -7, 8) with product 840.Solution ApproachThere can be multiple solutions to a given problem.One simple solution is using the direct method by traversing the array. Then finding all possible quadruples in ... Read More

Maximum product of two non-intersecting paths in a tree in C++

Ayush Gupta
Updated on 15-Sep-2020 14:19:33

289 Views

In this problem, we are given an undirected connected tree T with n nodes. Our task is to create a program to find the Maximum product of two nonintersecting paths in a tree in C++.Problem Description − To find the maximum product of two nonintersecting paths in a tree. We will find all the non-interesting paths and then find the product of their lengths.Let’s take an example to understand the problem, InputGraph −Output8ExplanationThe non-intersecting paths that are considered are C-A-B and F-E-D-G-H.The lengths are 2 and 4. Product = 8.Solution ApproachThe solution to this problem is by traversing the tree ... Read More

Maximum product of subsequence of size k in C++

Ayush Gupta
Updated on 15-Sep-2020 14:20:26

493 Views

In this problem, we are given an array arr[] of integers and a number k. Our task is to create a program to find the Maximum product of subsequence of size k in C++.Problem Description − Here, we need to find the subsequence of size k, 1

PHP program to calculate the sum of square of first n natural numbers

AmitDiwan
Updated on 02-Jul-2020 07:14:49

650 Views

To calculate the sum of square of first n natural numbers in PHP, the code is as follows −Example Live DemoOutputThe sum of square of first 5 natural numbers is 125A function named ‘sum_of_squares’ is defined that takes the limit up to which square of natural number needs to be found. In this function, a sum value is initialized to 0. A ‘for’ loop is run over the elements ranging from 1 to the limit. Every time, to the ‘sum’ variable, square of the iterated value is added. When the control reaches the end, the value of sum is returned as ... Read More

PHP program to calculate the repeated subtraction of two numbers

AmitDiwan
Updated on 02-Jul-2020 07:13:50

263 Views

To calculate the repeated subtraction of two numbers, the code is as follows −Example Live DemoOutputThe repeated subtraction results in 18A function named ‘repeated_sub’ is defined that checks to see if two values divide each other completely, and if this is true, it divides the numbers and gives the floor value of the quotient. Otherwise, it gives the floor value of quotient and the value computed by calling the ‘repeated_sub’ function on the second value, and the remainder when the values are divided.Outside the function, values are given to both the variables and the function is called by passing these values ... Read More

PHP program to find the sum of odd numbers within a given range

AmitDiwan
Updated on 02-Jul-2020 07:12:39

1K+ Views

To find the sum of odd numbers within a given range, the code is as follows −Example Live DemoOutputThe sum of odd natural numbers between the numbers 3 and 23 is 141.75A function named ‘odd_num_sum’ is defined that computes the sum of odd numbers within a specific range of numbers. The function ‘num_in_range’ gives the range of values between two numbers that are passed as parameters to this function. Outside both the function, the range values are defined and this function ‘num_in_range’ is called by passing the low and high values as parameters. Relevant output is displayed on the console.Read More

PHP program to find the sum of first n natural numbers that are divisible by a number ‘x’ or a number ‘y’

AmitDiwan
Updated on 02-Jul-2020 07:11:37

184 Views

To find the sum of first n natural numbers that are divisible by a number ‘x’ or a number ‘y’, the code is as follows −Example Live DemoOutputThe sum of first 11 natural numbers divisible by 2 or 5 is 15A function named ‘sum_of_nums’ is defined that computes three values by checking if they can be divided by two specific values or not. Outside the function, the number and the two specific values are defined, and the function is called by passing these values as parameters. Relevant output is displayed on the console

PHP program to find the numbers within a given array that are missing

AmitDiwan
Updated on 02-Jul-2020 07:10:31

568 Views

To find the numbers within a given array that are missing, the code is as follows −Example Live DemoOutputThe missing numbers in the array is 1 2 3 4 5A function named ‘missing_nums’ is defined that checks to see if a number is missing from an array of continuous numbers. It iterates through the array and checks to see the count and the current_num that is being iterated over. If two values can’t be found when 1 is added to the previous number, it is considered to be missing.Outside the function, the array is defined, its length is assigned to a ... Read More

PHP program to find the first natural number whose factorial can be divided by a number ‘x’

AmitDiwan
Updated on 02-Jul-2020 07:08:42

214 Views

To find the first natural number whose factorial can be divided by a number ‘x’, the code is as follows −Example Live DemoOutputThe first natural number whose factorial can be divided by 16 is 4A function named ‘factorial_num’ computes factorial of a number and checks to see if it is divisible by 16, and if yes, returns that number as output. Outside the function, a number is defined and it is passed as parameter to the function. Relevant output is displayed on the console.

PHP program to find if a number is present in a given sequence of numbers

AmitDiwan
Updated on 02-Jul-2020 07:07:49

347 Views

To find if a number is present in a given sequence of numbers, the code is as follows −Example Live DemoOutputIs the number present in the sequence? Yes, it is present in the sequenceA function named ‘contains_in_sequence’ checks to see if two values are same, and if they are equal, the function returns true. If the difference between the two values multiplied by a third value is greater than 0 and the difference between the values divided by the third value gives a reminder as 0, the function returns true, otherwise, returns false. Values are assigned to the three variables and ... Read More

Advertisements