Found 7197 Articles for C++

Remove 9 in C++

Arnab Chakraborty
Updated on 11-Jul-2020 11:42:01

242 Views

Suppose we have an integer n, we have to return nth integer after following this operation: Start from integer 1, remove any integer that contains 9 such as 9, 19, 29... So now, we will have a new integer sequence like 1, 2, 3, 4, 5, 6, 7, 8, 10, 11, ... We have to keep in mind that 1 will be the first integer.So, if the input is like 9, then the output will be 10To solve this, we will follow these steps −ret := 0s := 1while n is non-zero, do −ret := ret + (n mod 9) ... Read More

Coin Path in C++

Arnab Chakraborty
Updated on 11-Jul-2020 11:40:21

260 Views

Suppose we have an array A (index starts at 1) with N numbers: A1, A2, ..., AN and another integer B. The integer B denotes that from any index is i in the array A, we can jump to any one of the places in the array A indexed i+1, i+2, …, i+B if this place can be jumped to. Also, if we step on the index i, we have to pay Ai amount of coins. If Ai is -1, it means we can’t jump to the place indexed i in the array.Now, when we start from the place indexed ... Read More

Maximum Average Subarray II in C++

Arnab Chakraborty
Updated on 11-Jul-2020 11:36:37

924 Views

Suppose we have an array with n integers, we have to find the contiguous subarray whose length is greater than or equal to k that has the maximum average value. We have to find the maximum average value.So, if the input is like [1, 12, -5, -6, 50, 3], k = 4, then the output will be 12.75, as when length is 5, maximum average value is 10.8, when length is 6, maximum average value is 9.16667. Thus output is 12.75.To solve this, we will follow these steps −Define a function ok(), this will take x, an array nums, k, ... Read More

Maximum Vacation Days in C++

Arnab Chakraborty
Updated on 11-Jul-2020 11:33:30

435 Views

Suppose one company wants to give one of its best employees the option to travel among N cities to collect some resources. But employees want some vacations also, we could take vacations in some particular cities and weeks. Our task is to schedule the traveling to maximize the number of vacation days we could take, but there are certain rules and restrictions we have to follow.We can only travel among N cities; they are represented by indexes from 0 to N-1. Firstly, we are in the city indexed 0 on Monday.These cities are connected by flights. We have one N ... Read More

Word Abbreviation in C++

Arnab Chakraborty
Updated on 11-Jul-2020 11:28:19

1K+ Views

Suppose we have an array of n unique strings, We have to generate minimal possible abbreviations for every word following rules below.Starting with the first character and then the number of characters abbreviated, which followed by the last character.If we find any conflict and that is more than one words share the same abbreviation, a longer prefix can be used instead of only the first character until making the map from word to abbreviation become unique.When the abbreviation doesn't make the word shorter, then keep it as original.So, if the input is like ["like", "god", "internal", "me", "internet", "interval", "intension", ... Read More

Swap two variables in one line in C/C++, Python, PHP and Java

Hafeezul Kareem
Updated on 11-Jul-2020 08:06:36

619 Views

In this tutorial, we are going to learn how to swap two variables in different languages. Swapping means interchanging the values of two variables. Let's see an example.Inputa = 3 b = 5Outputa = 5 b = 3Let's see one by one.PythonWe can swap the variable with one line of code in Python. Let's see the code.Example Live Demo# initializing the variables a, b = 3, 5 # printing before swaping print("Before swapping:-", a, b) # swapping a, b = b, a # printing after swapping print("After swapping:-", a, b)OutputIf you run the above code, then you will get the following ... Read More

Maximum Sum Decreasing Subsequence in C++

Revathi Satya
Updated on 22-May-2024 11:57:17

401 Views

In this article, we are given an array arr[] of N integers. Our task is to find the Maximum Sum Decreasing Subsequence in C++. A Maximum Sum Decreasing Subsequence (MSDS) is a subsequence of a given sequence of array. Here the sequence elements are ordered in decreasing arrangement and the sum of these elements is the highest. Here, given a sequence of array a1, a2, …, an, the goal is to find a subsequence ai1, ai2, …, aik, where i1>i2>…>i1, such that we say the subsequence is ai1>ai2>…>aik (ordered in a decreasing order). The sequence of terms ai1+(ai2)...+(aik) is ... Read More

Maximum sum Bi-tonic Sub-sequence in C++

Ayush Gupta
Updated on 15-Oct-2020 13:56:52

199 Views

In this problem, we are given an array arr[]. Our task is to create a program to find the maximum sum Bi-tonic subsequence in C++.Bi-tonic subsequence is a special sequence whose elements first increase and then decrease.Let’s take an example to understand the problem, Inputarr[] = {4, 2, 3, 7, 9, 6, 3, 5, 1}Output33ExplanationThe Bi-tonic subsequence which gives the largest sum is {2, 3, 7, 9, 6, 5, 1} Sum = 2 + 3 + 7 + 9 + 6 + 5 + 1 = 33Solution ApproachTo find the maximum sum bitonic subsequence, we will create two arrays, incSeq[] ... Read More

Maximum sum bitonic subarray in C++

Ayush Gupta
Updated on 15-Oct-2020 14:00:02

582 Views

In this problem, we are given an array arr[]. Our task is to create a program to find the maximum sum bitonic subarray in C++.Bitonic Subarray is a special subarray in which the element strictly increase first and then strictly decreases after reaching a certain point.Let’s take an example to understand the problem, Inputarr[] = {4, 2, 3, 7 ,9, 6, 3, 5, 1}Output30ExplanationThe bitonic subarray is [2, 3, 7, 9, 6, 3]. Sum = 2 + 3 + 7 + 9 + 6 + 3 = 30Solution ApproachThe solution is similar to that in the bitonic subsequence problem. We ... Read More

Maximum sum and product of the M consecutive digits in a number in C++

Revathi Satya
Updated on 22-May-2024 11:55:01

345 Views

In this article, we are given a string representing a number. Our task is to create a program in C++ to find the maximum sum and product of M consecutive digits from the given number. We find all sequences of M consecutive digits and return the maximum sum and product. In mathematics, consecutive numbers are defined as those numbers that follow each other in increasing order from the smallest to the largest, with no missing numbers in between. This problem can be iterated through the string representation of the number, in other words, we consider consecutive segments of the length ... Read More

Advertisements