Found 7347 Articles for C++

Count of words that are present in all the given sentences in C++

Sunidhi Bansal
Updated on 03-Dec-2020 07:23:07

171 Views

We are given multiple sentences in the form of strings. The goal is to count the number of words that exist in all of the sentences.Note − words containing all lowercase letters will be considered onlyIf sentences are −“ I am learning C language ”“ learning new things is easy ““ Kids are learning healthy habits “Only “learning” exists in all three. So count is 1.Let us understand with examplesInput − “The clothes were dry”, “All the kids were playing”, “Those were the best days”Output − Count of words that are present in all the given sentences are − 2Explanation ... Read More

Count pairs formed by distinct element sub-arrays in C++

Sunidhi Bansal
Updated on 03-Dec-2020 07:19:25

93 Views

We are given an array arr[] containing integer elements. The goal is to find the count of pairs that can be formed by elements of sub-arrays of arr[] such that each subarray has only distinct elements. If the array is [ 1, 2, 2, 3, 3 ] then subarrays with distinct elements only will be [ 1, 2 ] and [ 2, 3 ]. And pairs will be (1, 2) and (2, 3) hence count of pairs is 2.Let us understand with examplesInput − arr[] = {1, 2, 5, 3 }Output − Count of pairs formed by distinct element sub-arrays ... Read More

Count of triangles with total n points with m collinear in C++

Sunidhi Bansal
Updated on 03-Dec-2020 07:17:44

106 Views

We are given two variables n and m representing the number of points on a 2D plane. Out of n points, m points are collinear. The goal is to find the number of triangles that can be formed using these n points.Collinear points − The points that lie on the same line are called collinear. Points A and B are collinear.Given n=4 (A, B, C, D ) , m=2 (A, B)Number of triangles −Choosing any three points out of 4 = 4C3But collinear points cannot form triangle so remove possible triangles that will be counted above = 2C3Total triangles= 4C3 ... Read More

Count of total anagram substrings in C++

Sunidhi Bansal
Updated on 03-Dec-2020 07:16:14

611 Views

We are given a string str[] as input. The goal is to count the number of anagram substrings present in str[]. Two strings are anagrams of each other if they contain the same number of characters and all characters occur in both. The order of characters can be different.“abc” is an anagram of “cba”, “bca” etc.Let us understand with examples.Input − str[] = “abccb”Output − Count of total anagram substrings are − 4Explanation − Anagrams are − (b, b), (c, c), (bc, cb), (bcc, ccb)Input − str = “aaa”Output − Count of total anagram substrings are − 4Explanation − Anagrams ... Read More

Count of suffix increment/decrement operations to construct a given array in C++

Sunidhi Bansal
Updated on 03-Dec-2020 07:14:43

105 Views

We are given a target array arr[] containing positive integers. The goal is to construct the target array arr[] using an initial array with all 0s. The operations that can be applied on a given empty array with all 0s will suffix increment/decrement operations.If we choose any index say i, then in case of suffix increment operation we will add 1 to all elements from index i till last index.In case of suffix decrement operation we will subtract 1 from all elements from index i till last index.Let us understand with examplesInput − arr[]= { 1, 2, 3 }Output − ... Read More

Program to create largest lexicographic number from a list of numbers in C++

Arnab Chakraborty
Updated on 03-Dec-2020 05:57:20

257 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

Program to count number of operations to convert binary matrix to zero matrix in C++

Arnab Chakraborty
Updated on 03-Dec-2020 05:52:05

82 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

Program to find minimum number of roads we have to make to reach any city from first one in C++

Arnab Chakraborty
Updated on 03-Dec-2020 05:41:15

623 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

Count of words whose i-th letter is either (i-1)-th, i-th, or (i+1)-th letter of given word in C++

Sunidhi Bansal
Updated on 02-Dec-2020 12:44:19

126 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

Count pairs with sum as a prime number and less than n in C++

Sunidhi Bansal
Updated on 02-Dec-2020 12:41:45

264 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

Advertisements