Found 7346 Articles for C++

Generate all Permutations of a String that follow given Constraints

Sonal Meenu Singh
Updated on 18-Aug-2023 11:31:47

261 Views

Introduction In this tutorial, we implement two examples using C++ programming concepts to generate all permutations of an input string. Permutation of a string is the number of ways a string can be arranged by interchanging the position of characters. We also include some constraints or limitations. All permutations or arrangements of the input string ensure character B does not follow character A anywhere, meaning there is no AB combination in the string. To implement this task we use two approaches: Directly generate all combinations of the string while restricting AB. Using backtracking. Demonstration 1 String = ... Read More

Capitalize the First and Last Character of Each Word in a String

Sonal Meenu Singh
Updated on 18-Aug-2023 11:28:04

337 Views

Introduction In this tutorial, we implement an approach to capitalizing the first and last characters of each word in the input string. By iterating over the input string str, each word's starting and ending letters are capitalized. We implement this problem using C++ programming in two ways. Let's start this tutorial with some demonstrations. Demonstration 1 String = “coding world” Output CodinG WorlD In the above demonstration, consider the input string and the result after capitalizing the starting and ending character of each word of the string is CodinG WorlD. Demonstration 2 String = “`hello all” ... Read More

Toggle First and Last Bits of a Number

Vanshika Sood
Updated on 17-Aug-2023 20:02:03

433 Views

The following article provides an in depth explanation of the method used to modify a number by toggling its first and last bit using bitwise operators. A bitwise operator is an operator that can be used to manipulate individual bits in binary numbers or bit patterns. Problem Statement For a given number n, modify the number such that the first and the last bit of the binary expansion of the new number are flipped i.e. if the original bit is 1 then the flipped bit should be 0 and vice versa. All the bits between the first and the last ... Read More

The Sum of the Fifth Powers of the First n Natural Numbers

Vanshika Sood
Updated on 17-Aug-2023 20:00:47

374 Views

Natural numbers are numbers that start from 1 and include all the positive integers. The following article discusses two possible approaches to compute the sum of the fifth powers of the first n natural numbers. The article discusses the two approaches in detail and compares them with regards to efficiency and intuitiveness. Problem Statement The purpose of this problem is to compute the arithmetic sum of the first n natural numbers, all raised to their fifth power i.e. $\mathrm{1^5 + 2^5 + 3^5 + 4^5 + 5^5 + … + n^5}$  till the nth term. Examples Since n is a ... Read More

Set the Leftmost Unset Bit

Vanshika Sood
Updated on 27-Oct-2023 15:56:21

265 Views

This article seeks a method for setting a given number's leftmost unset bit. The first unset bit after the most significant set bit is considered the leftmost unset bit. Problem Statement Given a number n, the task is to set the left most bit of the binary expansion of the number that is unset. All the other bits should be left unchanged. If all the bits of the original number are already set, return the number. Examples Input: 46 Output: 62 Explanation Binary Expansion of 46 = 101110. Left most unset bit is 101110. Upon setting the underlined ... Read More

Pernicious Number

Vanshika Sood
Updated on 17-Aug-2023 19:47:00

109 Views

A number is considered to be pernicious if the number is a positive integer and the number of set bits in its binary expansion are prime. The first pernicious number is 3, as 3 = (11)2. It can be seen that the number of set bits in the binary representation of 3 are 2, which is a prime number. The first 10 pernicious numbers are 3, 5, 6, 7, 9, 10, 11, 12, 13, 14. Interestingly, powers of 2 can never be pernicious since they always have only 1 set bit. 1 is not a prime number. On the other ... Read More

Odious Number

Vanshika Sood
Updated on 17-Aug-2023 19:40:24

189 Views

A number is considered to be an odious number if it has an odd number of 1s in its binary expansion. The first 10 odious numbers are 1, 2, 4, 7, 10, 11, 13, 14, 16, 19, 21. Interestingly, all powers of 2 are odious since they have only 1 set bit. The following article discusses 2 approaches in detail to find whether a number is an odious number or not. Problem Statement This problem aims to check whether the given number is an odious number i.e. it is a positive number with an odd number of set bits in ... Read More

Minimum time required to complete all tasks without altering their order

Shubham Vora
Updated on 18-Aug-2023 15:12:35

108 Views

In this problem, we need to find the total time required to complete all tasks according to the given condition. We can use the map data structure to solve the problem. We can keep tracking the last performed time for each task, and if the time interval is less than K, we can increment time units accordingly. Problem statement – We have given a string task containing alphabetical characters of length N. Each character represents a task, and we need one unit of time to perform the tasks. Also, the condition is that each task should be performed at ... Read More

Minimum increments by 1 or K required to convert a string into another given string

Shubham Vora
Updated on 18-Aug-2023 15:10:46

340 Views

We have given two strings and need to convert one string to another by click increments, and we can increment characters by either 1 or k in the single operation. To solve the problem, we need to make all characters of the first string the same as a second character by performing the cyclic increment operations. If the character at the same index in both strings is the same, we don’t need to perform any increment operation. Problem statement – We have given two strings named first and second containing the uppercase alphabetical characters. The length of both strings ... Read More

Maximize length of subsequence consisting of single distinct character possible by K increments in a string

Shubham Vora
Updated on 18-Aug-2023 14:48:31

121 Views

In this problem, we need to find the maximum length of the subsequence containing the single character by increasing the multiple characters by, at most, k times. We can use the sliding window approach to solve the problem. We can find the maximum length of any window to get the result after sorting the string. Problem statement – We have given string str containing lowercase alphabetical characters. Also, we have given the positive integer k. After performing the at most k increment operations with multiple characters of the given string, we need to find the maximum length of the ... Read More

Advertisements