Server Side Programming Articles - Page 1892 of 2650

Maximum value of arr[i] % arr[j] for a given array in C++

Narendra Kumar
Updated on 03-Jun-2020 07:39:20

284 Views

In this problem, we are given an array of n elements. Our task is to create a program that will find the maximum value of arr[i]%arr[j] for a given array.So, basically we need to find the value of the maximum remainder while dividing two elements of the array.Let’s take an example to understand the problem, Input − array{3, 6, 9, 2, 1}Output − 6Explanation −3%3 = 0; 3%6 = 3; 3%9 = 3; 3%2 = 1; 3%1 = 0 6%3 = 0; 6%6 = 0; 6%9 = 6; 6%2 = 0; 6%1 =0 9%3 = 0; 9%6 = 3; 9%9 ... Read More

Maximum value of |arr[i] – arr[j] - + |i – j| in C++

Narendra Kumar
Updated on 03-Jun-2020 07:48:06

401 Views

In this problem, we are given an array of n integers. Our task is to create a program that will find the maximum value of |arr[i]-arr[j]| + |i-j|.Let’s take an example to understand the problem, Input − array = {4, 1, 2}Output − 4Explanation −|arr[0] - arr[1]|+|0-1| = |4-1| + |-1| = 3+1 = 4 |arr[0] - arr[2]|+|0-2| = |4-2| + |-2| = 2+2 = 4 |arr[1] - arr[2 ]|+|1-2| = |1-2| + |1-2| = 1+1 = 2To solve this problem, a simple approach will be using the brute force approach which will be using two loops and finding the ... Read More

Maximum value of |arr[0] – arr[1] - + |arr[1] – arr[2] - + … +|arr[n – 2] – arr[n – 1] - when elements are from 1 to n in C++

Narendra Kumar
Updated on 03-Jun-2020 07:40:53

214 Views

In this problem, we are given an array of n integers of range [1, n]. Our task is to create a program that will find the maximum value of |arr[0] – arr[1] - + |arr[1] – arr[2] - + … +|arr[n – 2] – arr[n – 1].Let’s take an example to understand the problem, Input − array= {1, 2, 3}Output − 3Explanation −max sum is |1-3|+|2-1| = 3To solve this problem, a simple approach is to create all permutations from the array. And find the maximum value of all the values from permutation. A more effective method is to generalize ... Read More

Maximum Unique Element in every subarray of size K in c++

Narendra Kumar
Updated on 03-Jun-2020 07:43:08

310 Views

In this problem, we are given an array of integers and a integers K. our task is to create a program that will find the maximum unique element in every subarray of size K with no duplicates.Let’s take an example to understand the problem, Input −array = {4, 1, 1, 3, 3} k = 3Output −4 3 1Explanation −Sub-array of size 3 Subarray {4, 1, 1}, max = 4 Subarray {1, 1, 3}, max = 3 Subarray {1, 3, 3}, max = 1To solve this problem, one simple method is to run two loops and create subarrays and find distinct ... Read More

Maximum triplet sum in array in C++

Ravi Ranjan
Updated on 23-Jul-2025 18:52:56

357 Views

In this article, we are given an array of integers. Our task is to write a program to calculate the maximum triplet sum in the given array, i.e., find the set of three elements whose sum is maximum. Input Output Scenario Consider the following input and output scenario where we have calculated the sum of each triplet and then returned the maximum sum of the triplet in the array: Input: arr = {4, 6, 1, 2} Output: Maximum triplet sum: 12 Here is an explanation of the above example: All ... Read More

Maximum sum two non-overlapping subarrays of given size in C++

Narendra Kumar
Updated on 03-Jun-2020 07:46:32

194 Views

In this problem, we are given an array of positive integers and a number k. Our task is to create a program that will find the maximum sum of two nonoverlapping subarrays of the given size(k).So, basically we have two print two non-overlapping (distinct) subarrays that have the maximum sum and are of size k.Let’s take an example to understand the problem, Input −array = {7, 1, 6, 9, 2} , k = 2Output −{7, 1} , {6, 9}Explanation −all subarrays of size 2. {7, 1} : sum = 7+1 = 8 {1, 6} : sum = 1+6 = 7 ... Read More

Top K Frequent Elements in Python

Arnab Chakraborty
Updated on 04-May-2020 10:10:02

2K+ Views

Suppose we have a non-empty array of integer numbers. we have to return the kth most frequent elements. So if the elements are [1, 1, 1, 1, 2, 2, 3, 3, 3] and k = 2, then the result will beFormally the function should −Return true if there exists i, j, ksuch that arr[i] < arr[j] < arr[k] given 0 ≤ i < j < k ≤ n-1 else return false.To solve this, we will follow these steps −num_freq = an empty map, freq_list := an empty mapfor each element i in numsif i is not in num_freq, then num_freq[i] ... Read More

Increasing Triplet Subsequence in Python

Arnab Chakraborty
Updated on 04-May-2020 10:09:06

623 Views

Suppose there is an unsorted array. We have to check whether an increasing subsequence of length 3 exists or not in that array.Formally the function should −Return true if there exists i, j, ksuch that arr[i] < arr[j] < arr[k] given 0 ≤ i < j < k ≤ n-1 else return false.To solve this, we will follow these steps −small := infinity, big := infinityfor each element i in arrayif i

Odd Even Linked List in Python

Arnab Chakraborty
Updated on 04-May-2020 10:07:50

727 Views

Suppose we have a singly linked list, we have to group all odd nodes together followed by the even nodes. Here we are talking about the node position not the value in the nodes. We should try to do it in place. So if the nodes are [1, 22, 13, 14, 25], the result will be [1, 13, 25, 22, 14]To solve this, we will follow these steps −if head is null or the next of head is null, then return headhead1 := head, head2 := next of head, head_beg := next of headwhile next of head2 is nor null ... Read More

Best Time to Buy and Sell Stock with Cooldown in C++

Arnab Chakraborty
Updated on 04-May-2020 10:00:51

172 Views

Suppose we have an array for which the ith element is the price of a given stock on the day i. We have to design an algorithm to find the maximum profit. We may complete as many transactions as we want (So, buy one and sell one share of the stock multiple times). But we have to follow these rules −We may not engage in multiple transactions at the same time (So, we must sell the stock before you buy again).After we sell our stock, we cannot buy stock on next day. (So cool down 1 day)If the input is ... Read More

Advertisements