Found 26504 Articles for Server Side Programming

Count of strings that can be formed from another string using each character at-most once in C++

Sunidhi Bansal
Updated on 02-Dec-2020 12:29:30

369 Views

We are given two strings i.e. str1 and str2 and the task is to calculate the count of strings that can completely be generated from another string, but we can use one character once for forming the string. Like, we will take two strings str1 and str2 and check for the occurrence of str2 in str1 by using the character of str1 exactly once.Input − str_1 = "technical learning", str_2 = "learning"Output − Count of strings that can be formed from another string using each character at-most once are − 1Explanation − As we can see str_2 occurs in str_1 ... Read More

Count pairs (p, q) such that p occurs in array at least q times and q occurs at least p times in C++

Sunidhi Bansal
Updated on 02-Dec-2020 12:27:57

247 Views

We are given an array of positive integers. The goal is to find the count of pairs of elements of arr[] such that pairs have elements ( p, q ) where p occurs in array for at least q times and q occurs in array for at-least p times.Let us understand with examples.Input − int arr[] = { 3, 3, 3, 5, 5, 6, 6}Output − Count of pairs in an array such that frequency of one is at least value of other are − 1Explanation − The valid pairs in an array where p occurs q times and q ... Read More

Count pairs in an array such that frequency of one is at least value of other in C++

Sunidhi Bansal
Updated on 02-Dec-2020 12:26:16

328 Views

We are given an array of positive integers. The goal is to find the count of pairs of elements of arr[] such that pairs have elements ( A, B ) where frequency of A is B times and frequency of B is A.Let us understand with examples.Input − int arr[] = { 3, 3, 3, 5, 5, 6, 6}Output − Count of pairs in an array such that frequency of one is at least value of other are − 1Explanation − The valid pairs in an array where A occurs B times and B occurs A times are (3, 3) ... Read More

Count of substrings of a binary string containing K ones in C++

Sunidhi Bansal
Updated on 02-Dec-2020 12:24:52

555 Views

We are given a string of binary numbers i.e. combination of 0’s and 1’s and an integer value k and the task is to calculate the count of substrings formed with the given binary string having given k 1’s.Input − string str = ‘10000100000’, k = 2Output − Count of substrings of a binary string containing K ones are − 6Explanation − Substrings that can be formed from the given string are 1, 10, 100, 1000, 10000, 010, 100001, 10001, 1001, 101, 11, 1000010. So there are 6 substrings having k number of 1’s i.e. exactly 2 ones.Input − string ... Read More

Count of sub-strings of length n possible from the given string in C++

Sunidhi Bansal
Updated on 02-Dec-2020 12:22:38

679 Views

We are given a string str[] and a number n. The goal is to find all substrings of str[] that have length n. If string is “abcde” and n=3 then substrings of length 3 are “abc”, “bcd”, “cde” and count is 3.Let us understand with examples.Input − str[] = “computer” n=4Output − Count of substrings of length n possible from the given string are − 5Explanation − Substrings with length 4 are: “comp”, “ompu”, ”mput”, “pute”, “uter”Input − str[] = “development” n=5Output − Count of substrings of length n possible from the given string are − 7Explanation − Substrings with ... Read More

Count of sub-strings that contain character X at least once in C++

Sunidhi Bansal
Updated on 02-Dec-2020 12:17:55

316 Views

We are given a string str[] and a character X. The goal is to find the substrings of str[] such that all the substrings contain X at least once. For str[]=”abc '' and X=’a’, the substrings containing ‘a’ at-least once are “a”, “ab”, “abc”. The count is 3.Let us understand with examples.Input − str[] = “aabccd” X=’c’Output − Count of sub-strings that contain character X at least once are − 14Explanation − Substrings containing at-least one ‘c’ will be : “c”, “c”, “bc”, “cc”, “cd”, “abc”, “bcc”, “ccd”, “aabc”, “abcc”, “bccd”, “aabcc”, “abccd”, “aabccd”.Input − str[] = “settings” X=’s’Output − ... Read More

Count of sub-strings that do not contain all the characters from the set {‘a’, ‘b’, ‘c’} at the same time in C++

Sunidhi Bansal
Updated on 02-Dec-2020 12:21:18

268 Views

We are given a string str[] containing ‘a’, ‘b’ and ‘c’ only. The goal is to find the substrings of str[] such that all the three characters are not part of that substring. For any string str, substrings could be “a”, “b”, “c”, “abb”, “bba”, “bc”, “ca”, “ccc” but not “abc”, “bcca” , “cab” as these have ‘a’, ‘b’ and ‘c’, all three.Let us understand with examples.Input − str[] = “aabc”Output − Count of sub-strings that do not contain all the characters from the set {‘a’, ‘b’, ‘c’} at the same time are − 8Explanation − Substrings will be : ... Read More

Count of subarrays whose maximum element is greater than k in C++

Sunidhi Bansal
Updated on 02-Dec-2020 12:10:35

341 Views

We are given an array arr[] containing integer elements and a variable k . The goal is to find the count of subarrays of arr[] that have greatest/maximum element more that k. If the array is [1, 2, 3] and k is 1. Then possible subarrays are [1], [2], [3], [1, 2], [2, 3], [1, 2, 3]. The subarrays with maximum element > 1 are [2], [3], [1, 2], [2, 3], [1, 2, 3]. So the count is 5.Let us understand with examplesInput − arr[] = {1, 2, 5, 3 } k=3Output − Count of subarrays whose maximum element is ... Read More

Count of values of x <= n for which (n XOR x) = (n – x) in C++

Sunidhi Bansal
Updated on 02-Dec-2020 12:09:07

374 Views

We are given a number n as input. The goal is to find values x such that condition (n xor x)=(nx) holds. Also x lies in range [0,n].Let us understand with examplesInput − n=10Output − Count of values of x

Count of unique pairs (arr[i], arr[j]) such that i < j in C++

Sunidhi Bansal
Updated on 02-Dec-2020 12:07:12

864 Views

We are given an array containing integer elements. The goal is to find unique pairs of elements of array such that pairs of type (arr[i],arr[j]) have indexes such that iLet us understand with examplesInput − arr[] = {1,2,3};Output − Count of unique pairs (arr[i], arr[j]) such that i < j are − 3Explanation − As all elements are unique. Pairs would be −(1,2) - ( arr[0],arr[1] ) 0

Advertisements