Found 33676 Articles for Programming

Maximum sum of difference of adjacent elements in C++

Ayush Gupta
Updated on 15-Oct-2020 12:17:54

315 Views

In this problem, we are given a number N. Our task is to create a program to find the Maximum sum of difference of adjacent elements in C++.Problem DescriptionWe will find the Maximum sum of the absolute difference between the adjacent elements of all permutation arrays.Let’s take an example to understand the problem, InputN = 4Output7ExplanationAll permutations of size 4 are : {1, 2, 3, 4} = 1 + 1 + 1 = 3 {1, 2, 4, 3} = 1 + 2 + 1 = 4 {1, 3, 2, 4} = 2 + 1 + 2 = 5 {1, 3, ... Read More

Python - end parameter in print()

Pradeep Elance
Updated on 22-Jul-2020 08:30:56

1K+ Views

The print() function in python always creates a newline. But there is also a parameter for this function which can put other characters instead of new line at the end. In this article we will explore various options for this parameter.ExampleIn the below example we see various ways we can assign values to the end parameter and see the result from it. Live Demoprint("Welcome to ") print("Tutorialspoint") print("Welcome to ", end = ' ') print("Tutorialspoint") print("emailid", end='@') print("tutorialspoint.com")OutputRunning the above code gives us the following result −Welcome to Tutorialspoint Welcome to Tutorialspoint emailid@tutorialspoint.comRead More

Maximum sum of absolute difference of any permutation in C++

Ayush Gupta
Updated on 15-Oct-2020 12:19:26

600 Views

In this problem, we are given an array. Our task is to create a program to find the Maximum sum of absolute difference of any permutation in C++.Problem DescriptionWe will be finding all permutation of the elements of the given array. And then finding the sum of the absolute difference of adjacent elements of the array. Lastly we will return the maximum of all sums.Let’s take an example to understand the problem, Inputarr[] = {9, 1, 6, 3}Output17ExplanationAll permutations of the array with sum of absolute difference of adjacent elements. {9, 1, 6, 3}, sum= |9-1| + |1-6| + |6-3| ... Read More

Maximum Sum Increasing Subsequence using Binary Indexed Tree in C++

Ayush Gupta
Updated on 15-Oct-2020 12:21:46

145 Views

In this problem, we are given an array arr[] of N elements. Our task is to create a program to find the maximum Sum Increasing Subsequence using Binary Indexed Tree in C++.Let’s take an example to understand the problem, Inputarr[] = {4, 1, 9, 2, 3, 7}Output13ExplanationMaximum increasing subsequence is 1, 2, 3, 7. Sum = 13Solution ApproachTo solve the problem, we will use the Binary Indexed Tree in which we will insert values and map them to binary indexed tree. Then find the maximum value.ExampleProgram to illustrate the working of our solution,  Live Demo#include using namespace std; int ... Read More

Python - Difference in keys of two dictionaries

Pradeep Elance
Updated on 22-Jul-2020 08:28:31

3K+ Views

Two python dictionaries may contain some common keys between them. In this article we will find how to get the difference in the keys present in two given dictionaries.With setHere we take two dictionaries and apply set function to them. Then we subtract the two sets to get the difference. We do it both ways, by subtracting second dictionary from first and next subtracting first dictionary form second. Those keys which are not common are listed in the result set.Example Live DemodictA = {'1': 'Mon', '2': 'Tue', '3': 'Wed'} print("1st Distionary:", dictA) dictB = {'3': 'Wed', '4': 'Thu', '5':'Fri'} print("1st Distionary:", ... Read More

Maximum sum increasing subsequence from a prefix and a given element after prefix is must in C++

Ayush Gupta
Updated on 15-Oct-2020 12:26:32

168 Views

In this problem, we are given an array arr[] of N integers and two index values x and y. Our task is to create a program to find the Maximum sum increasing subsequence from a prefix and a given element after prefix is must in C++.Problem DescriptionWe will find the maximum sum of increasing sequence till index x and including the element at index y.Let’s take an example to understand the problem, Inputarr[] = {1, 5, 9, 131, 6, 100, 11, 215}, x = 4, y = 6Output26ExplanationWe will take the subsequence till index 3 and then at last include ... Read More

Maximum Sum Increasing Subsequence | DP-14 in C++

Ayush Gupta
Updated on 22-Jul-2020 08:20:57

156 Views

In this tutorial, we will be discussing a program to find maximum Sum Increasing Subsequence.For this we will be provided with an array containing N integers. Our task is to pick up elements from the array adding to the maximum sum such that the elements are in sorted orderExample Live Demo#include using namespace std; //returning the maximum sum int maxSumIS(int arr[], int n) {    int i, j, max = 0;    int msis[n];    for ( i = 0; i < n; i++ )       msis[i] = arr[i];    for ( i = 1; i < n; ... Read More

Python - Create a dictionary using list with none values

Pradeep Elance
Updated on 22-Jul-2020 08:23:09

669 Views

Suppose you are given a list but we want to convert it to dictionary. Dictionary elements hold two values are called key value pair, we will use in case of value. The elements of the list become keys and non will remain a placeholder.With dictThe dict() constructor creates a dictionary in Python. So we will use it to create a dictionary. The fromkeys method is used to create the dictionary elements.Example Live DemolistA = ["Mon", "Tue", "Wed", "Thu", "Fri"] print("Given list: ", listA) res = dict.fromkeys(listA) # New List print("The list of lists:", res)OutputRunning the above code gives us ... Read More

Maximum sum in circular array such that no two elements are adjacent in C++

Ayush Gupta
Updated on 15-Oct-2020 12:30:13

355 Views

In this problem, we are given a circular array cirArr[]. Our task is to create a program to find the Maximum sum in circular array such that no two elements are adjacent in C++.Problem DescriptionFor the circular array, we need to find the maximum sum sum of elements of the array such that adjacent elements cannot be taken i.e. we need to take alternate elements.Circular Array is a special type of array in which the last element of the array is connected to the first element.Let’s take an example to understand the problem, InputcirArr[] = {4, 1, 5, 3, 2}Output9ExplanationThe ... Read More

Maximum sum in a 2 x n grid such that no two elements are adjacent in C++

Ayush Gupta
Updated on 15-Oct-2020 12:32:24

369 Views

In this problem, we are given a rectangular grid of size 2 x n. Our task is to create a program to find the Maximum sum in a 2 x n grid such that no two elements are adjacent in C++.Problem DescriptionTo find the maximum sum, we cannot select elements that are adjacent to the current element, vertically, horizontally or diagonally.Let’s take an example to understand the problem, InputrectGrid[2][] =389 411Output13Explanationall possible sums areIf we start from rectGrid[0][0] i.e. 3, then we can add only 9 or 1. The maxSum is 12.If we start from rectGrid[1][0] i.e. 4, then we ... Read More

Advertisements