C++ Articles

Page 118 of 597

Minimum number of adjacent swaps to reverse a String

Prabhdeep Singh
Prabhdeep Singh
Updated on 26-Jul-2023 1K+ Views

A string str is given and we can swap only adjacent characters to make the string reverse. We have to find the number of minimum moves required to make the string reverse just by swapping the adjacent characters. We will implement two approaches to find the required solution with the explanation and the implementation of the code. Sample Examples Input1: string str1 = “shkej” Output: 10 Explanation First, we will take the last character to the first position which will take 4 swappings, and then the string will be “jshke”. Then we will move ‘e’ to the second ...

Read More

Minimum count of prefixes and suffixes of a string required to form a given string

Prabhdeep Singh
Prabhdeep Singh
Updated on 26-Jul-2023 460 Views

Prefixes are the substring from the given string that starts from the zeroth index and can go up to length 1 to the size of the string. Similarly, the suffix is the substring of any length 1 to the size of the string and ends at the last index. We will be given two strings and have to create the first string by using any number of prefixes and suffixes of the second string in any way. If it is not possible to create the given string from the given methods then we will return -1. Sample Examples Input 1: ...

Read More

Encrypt the string

Prabhdeep Singh
Prabhdeep Singh
Updated on 26-Jul-2023 2K+ Views

Encryption is the technique to change the data by using some techniques or certain steps so it changes to another information or the previous information cannot be gathered from it directly. For encryption, we have to follow certain steps that are fixed for a particular type of encryption. In this problem, we will be given a string and we have to encrypt it by following the given steps − First, we have to get all the substring that contains the same characters and replace that substring with a single character followed by the length of the substring. Now, change ...

Read More

Count of 3 length strings using given characters containing at least 2 different characters

Prabhdeep Singh
Prabhdeep Singh
Updated on 26-Jul-2023 230 Views

Three integers are given to us ‘a’, ‘b’, and ‘c’ represents the frequency of the three different characters ‘A’, ‘B’, and ‘C’. We have to find the number of different strings that can be formed by using these characters and there must be at least two different characters present in the string formed. We will see two approaches for this problem one is the naive approach and another is the mathematical approach. Sample Examples Input 1: a = 3, b = 2, c = 4 Output: 3 Explanation We can create three strings ‘ABC’, ‘ABC’, ...

Read More

Maximize 0s to be flipped in a given Binary array such that there are at least K 0s between two 1s

Prabhdeep Singh
Prabhdeep Singh
Updated on 26-Jul-2023 195 Views

A Binary Array is a special type of array that only contains the numbers 0 and 1. In this problem, we have given a binary array and integer K. Our task is to count the maximum number of 0’s that can be flipped to 1 in a given binary array such that there are at least K 0’s between two 1s. Sample Examples Input 1: arr[] = { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0 }, K = 2 Output 1: yes Explanation The 3rd and 6th indexes of the above ...

Read More

Print all Balanced Brackets Strings that can be formed by replacing wild card ‘?’

Prabhdeep Singh
Prabhdeep Singh
Updated on 26-Jul-2023 279 Views

Balanced Brackets mean if we have a string of brackets then each open bracket has a corresponding close bracket and the pair of brackets is properly nested. The size of the string should be ab even number. In this problem we have given a string of the brackets that also contain the character ‘?’ and our task is to form every possible balanced bracket string by replacing the ‘?’ to appropriate brackets. In our given string only parentheses ‘(‘ and ‘)’ brackets are used. Sample Examples Input 1: str = “()(?)?” Output 1: ()(()) Explanation Only one balanced ...

Read More

Check if the end of the given Binary string can be reached by choosing the jump value in between the given range

Prabhdeep Singh
Prabhdeep Singh
Updated on 26-Jul-2023 120 Views

A binary string is a string that contains only two different types of characters that are 0 and 1. We are given a binary string and two integers L and the R. We can make the jump of size between ‘L’ and ‘R’ length both inclusive and only from the index where the value of the string is ‘0’. We have to start from the Zeroth index and find out whether we can reach the last index or not. Sample Examples Input1: string str = “01001110010” int L = 2, R = 4 Output: Yes, we can reach the ...

Read More

Minimize XOR of Pairs to Make the Array Palindrome Using C++

Way2Class
Way2Class
Updated on 25-Jul-2023 171 Views

In the field of computer science, solving optimization problems efficiently is crucial for developing optimal algorithms and systems. One such problem is minimizing the XOR (exclusive OR) of pairs in an array to make it a palindrome. This situation holds significance because it offers a chance to determine the optimal approach towards reordering items within an array, which can lead to lower XOR value and creation of palindromes. This essay examines two methods that utilize C++ programming language for resolving this predicament. Syntax To begin with, let's define the syntax of the function we will be using in the following ...

Read More

Maximize Subarrays of 0s of Length X in Given Binary String after Flipping at Most One \'1\' Using C++

Way2Class
Way2Class
Updated on 25-Jul-2023 232 Views

In computer programming, it is often necessary to manipulate binary strings and find optimal solutions for various problems. One such problem is to maximize the subarrays of 0s of a specific length, X, in a given binary string. Before we proceed further, it's essential to acknowledge that there exists a restriction - the string permits modification for just one '1'. Let's delve into an effective methodology to overcome this obstacle by applying C++. Syntax Before diving into the codes ahead its crucial to clarify the syntax of the method we'll be utilizing. int maxSubarrays(string binaryString, int X); Algorithm Here ...

Read More

Exit Point in a Binary Matrix Using C++

Way2Class
Way2Class
Updated on 25-Jul-2023 257 Views

A binary matrix refers to a grid with rows and columns composed exclusively of 0s or 1s within computer programming parlance. Identifying the exit point within a binary matrix constitutes a coding challenge encountered during programming interviews and competitions. In this write-up, we'll explain distinct approaches for addressing this problem with C++. Syntax It may prove beneficial to first familiarize ourselves with the syntax that will feature prominently in our upcoming code examples before delving into the algorithm. `pair findExitPoint(const vector& matrix)`. Algorithm Now, let's outline the step-by-step algorithm to find the exit point in a binary matrix − ...

Read More
Showing 1171–1180 of 5,961 articles
« Prev 1 116 117 118 119 120 597 Next »
Advertisements