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

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

277 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

990 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 + ... + 2n in C++

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

310 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 in C++

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

671 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: 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 + 1 + 3 + 1 + 3 + 5 + 1 + 3 + 5 + 7 + ... + 2n - 1 in C++

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

530 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

Sum of the Series 0.7.0.77.0.777 Upto n Terms in C++

sudhir sharma
Updated on 14-Aug-2020 13:31:55

248 Views

In this problem, we are given n terms of a number. The series is 0.7, 0.77, 0.777…. Our task is to create a program to find the sim of the series 0.7, 0.77, 0.777 … upto n terms.Let’s take an example to understand the problem, Input  4Output  Explanation −0.7 + 0.77 + 0.777 + 0.7777 = 3.0247To solve this problem, we will derive the formula for sum of series. Let’s find the general formula for it, sum = 0.7 + 0.77 + 0.777 + ... upto n terms sum = 7 (0.1 + 0.11 + 0.111 + … upto n ... Read More

Sum of the Products of All Possible Subsets in C++

sudhir sharma
Updated on 14-Aug-2020 13:24:44

763 Views

In this problem, we are given an array arr[] of N numbers. Our task is to create a program that will find the sum of the products of all possible subsets.Here, we will find all subsets and then find the product of all elements for each subset. Then add all the values to calculate the sum.Let’s take an example to understand the problem, Inputarr[] = {4, 5, 6}Output209Explanation −All subsets of arr[] are: {4}, {5}, {6}, {4, 5}, {5, 6}, {4, 6}, {4, 5, 6} Sum of product = (4) + (5) + (6) + (4*5) + (5*6) + (4*6) ... Read More

Different Methods to Copy in C++ STL: std::copy, std::copy_n, std::copy_if, and std::copy_backward

Sunidhi Bansal
Updated on 14-Aug-2020 08:43:35

471 Views

As the method name suggests copy() method is used to copy the data through various methods available in C++ STL. All the methods differ in functionalities and the parameters. These methods are available in header file. Let’s discuss each method and their functionalities.Copy(start_i1, end_i1, start_i2)This method is used to copy the data from one iterator to another iterator within specified range where both the start and end elements of an iterator are inclusive. It takes three types of arguments i.e. −Start_i1 − It will point to the initial element of the iterator, let’s say, i_1 from where the element ... Read More

Advertisements