Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Articles - Page 255 of 719
341 Views
Suppose we have a list of numbers called nums, we have to rearrange its order to form the largest possible number and return that as a string.So, if the input is like nums = [20, 8, 85, 316], then the output will be "88531620".To solve this, we will follow these steps −Define an array tempfor each item i in nums:insert i into temp as stringsort the array temp based on lexicographic sequence (check for two strings a, b when a concatenate b is larger than b concatenate a or not)for each string s in temp:res := res concatenate sreturn resLet ... Read More
161 Views
Suppose we have a binary matrix. Now consider an operation where we take one cell and flip it and all its neighboring cells (up, down, left, right). We have to find the minimum number of operations required such that matrix contains only 0s. If there is no solution, then return -1.So, if the input is like0010then the output will be 3.To solve this, we will follow these steps −Define an array dir of size: 4 x 2 := {{1, 0}, {0, 1}, { - 1, 0}, {0, - 1}}const int inf = 10^6Define a function getPos(), this will take i, ... Read More
752 Views
Suppose we have two lists costs_from and costs_to of same size where each index i represents a city. It is making a one-way road from city i to j and their costs are costs_from[i] + costs_to[j]. We also have a list of edges where each edge contains [x, y] indicates there is already a one-way road from city x to y. If we want to go to any city from city 0, we have to find the minimum cost to build the necessary roads.So, if the input is like costs_from = [6, 2, 2, 12] costs_to = [2, 2, 3, ... Read More
212 Views
We are given a string str[] as input. The goal is to count the words from str[] that have the same length as str[] and have positions of letters such that ith letter is replaced with letter at position (i1) or (i) or (i+1).For the first letter replacement will be from position i or i+1For the last letter replacement will be from position i-1 or i.Let us understand with examples.Input − str[] = “TPP”Output − Count of words whose i-th letter is either (i-1)-th, i-th, or (i+1)-th letter of given word are − 4Explanation Replacing T by T (i)th or 1st ... Read More
389 Views
We are given a positive number n as input. The goal is to find the count of possible pairs (i, j) such that each pair has sum (i+j) which is prime and is less than n. Also i != j and i, j>=1 If n is 4 then only 1 pair is possible which is (1, 2). Here 1+2 = 3 is prime and less than 4. Also 1, 2 >=1.Let us understand with examples.Input − n=7Output − Count of pairs with sum as a prime number and less than n are − 3Explanation − Pairs will be (1, 2), ... Read More
265 Views
We are a variable num. The goal is to find the count of permutations of numbers between [1, num] in which numbers are first decreasing then increasing. For example if num=3 then numbers are 1, 2, 3. The permutations will be [ 3, 1, 2 ] and [2, 1, 3] and count is 2.We know that in every permutation the change from decreasing of numbers to increasing of numbers will be decided based on position of 1 which is smallest. After each 1 the numbers will start increasing. For a permutation to decrease and then increase, 1 should lie between ... Read More
288 Views
We are two variables n and m representing a grid of size n x m and initial point x, y to start from.Also given pairs of steps/moves that can be taken to traverse inside the grid as moves ( (1, 1), (2, 2) ) etc. Each pair of moves represents the unit of steps taken in the x, y axis. The goal is to find the total steps that can be taken to traverse inside the grid within boundaries [1, n] X [1, m] If n is 5 and m is 4 and current position is 2, 2 and step ... Read More
915 Views
We are given a string containing the parentheses and the task is to calculate the count of pairs of parentheses sequences that can be formed such that the parentheses are balanced.Parentheses are said to be balanced when there are equal numbers of opening and closing brackets. The parentheses used once can’t be considered twice for forming the pair.Input − string paran[] = { ")()())", "(", ")(", ")(", ")" }Output − Count of pairs of parentheses sequences such that parentheses are balanced are: 1Explanation − we will take every set of string to calculate the count better. Lets take the first ... Read More
305 Views
We are given a string which can contain “ab” and the task is to calculate the count of operations required to remove or delete “ab” from the string. So, our task is to firstly check whether the string contains “ab” or not if yes then we have to make string “ab” free.Input − string str = "ababaa"Output − Count of operations to make a binary string “ab” free are − 4Explanation − As we can see in the string “ab” pattern is occurring two times so we will replace “ab” with “bba” so the count of operations is 1 and ... Read More
279 Views
We are given an array of integer elements and the task is to calculate the count of sub-arrays that can be formed from the given array such that its elements can form a valid palindrome. Palindromes are the sequences that are arranged similarly from start and the end.Input − int arr[] = { 3, 3, 1, 4, 2, 1, 5}Output − Count of sub-arrays whose elements can be re-arranged to form palindromes are − 9Explanation − The valid sub-arrays whose elements can be arranged to form a palindrome are {3}, {3}, {1}, {4}, {2}, {1}, {5}, {1, 2, 1} and ... Read More