We are given a string str. The goal is to count all substrings of str that are special palindromes and have length greater than 1. The special palindromes are strings that have either all the same characters or only the middle character as different. For example if string is “baabaa” then special palindromes that are substring of original are “aa”, “aabaa”, “aba”, “aa”Let us understand with examples.Input − str=”abccdcdf”Output − Count of special palindromes in a String is − 3Explanation − Substrings that are special palindromes are − “cc”, “cdc”, “dcd”Input − str=”baabaab”Output − Count of special palindromes in a ... Read More
We are given an array arr[] containing integers and a variable X. The goal is to count all subarrays of arr[] such that each subarray contains only elements that are less than or equal to X. For example if array is [1, 2, 3] and X=2 then subarrays will be [1], [2] and [1, 2]Let us understand with examples.Input − arr[] = { 4, 3, 2, 1, 6 }; X=3Output − Count of sub-arrays which have elements less than or equal to X is − 6Explanation − Subaarays will be −[3], [2], [1], [3, 2], [2, 1], [3, 2, 1]Input ... Read More
We are given an array arr[] containing 0’s and 1’s only. The goal is to count all subarrays of arr[] such that each subarray contains only 0’s or only 1’s not both. If the array is [1, 0, 0] .Subarrays will be for 0’s only [0], [0], [0, 0] and for 1’s only [1].Let us understand with examples.Input − arr[] = { 0, 0, 1, 1, 1, 0 };Output − Subarrays with only 0's are : 4 Subarrays with only 1's are : 6Explanation − Subaarays will be −For all 0’s: [0], [0], [0], [0, 0]. Total 4 ( arr[0], ... Read More
We are given an array arr[] containing integers. The goal is to count all subarrays of arr[] such that the number of distinct elements in each is the same as the number of distinct elements in the original array. If the original array is [1, 1, 2, 3] then subarrays will be [1, 2, 3] and [1, 1, 2, 3].Total distinct elements in the original array is 3. Total distinct elements in both subarrays is also 3Let us understand with examples.Input − arr[] = {1, 2, 1, 2, 3, 4, 2 };Output − Count of subarrays having total distinct elements ... Read More
We are given an array arr[] containing integers. The goal is to count all subarrays of arr[] such that consecutive elements in each subarray differ by 1 only. If the array is [1,2,3] .Subarrays will be [1,2], [2,3], [1,2,3] only.Let us understand with examples.Input − arr[] = { 4,3,2,1 };Output − Count of Subarrays with Consecutive elements differing by 1 is − 6Explanation − Subaarays will be −[4,3], [3,2], [2,1], [4,3,2], [3,2,1], [4,3,2,1]. Total 6.Input − arr[] = { 1,5,6,7,9,11 };Output − Count of Subarrays with Consecutive elements differing by 1 is − 3Explanation − Subaarays will be −[5,6], [6,7], [5,6,7]. Total 3The approach used in the below program is as followsWe will traverse the array using a for a loop. From i=0 to i
We are given an array arr[] containing 0’s and 1’s only. The goal is to count all subarrays of arr[] such that occurrences of 0s and 1s is equal in all. If the array is [1, 0, 0] .Subarray will be [1, 0] only.Let us understand with examples.Input − arr[] = { 0, 0, 1, 1, 1, 0 };Output − Count of subarrays with an equal number of 1’s and 0’s are − 4Explanation − Subaarays will be −arr[0 to 3] = [0, 0, 1, 1], arr[1 to 2] = [0, 1], arr[4 to 5] =[1, 0], Arr[0 to 5] ... Read More
We are given an array arr[] of integers. Also a number K. The goal is to count all subarrays of arr[] such that all elements of the subarray are greater than K or K is less than all elements of the subarrays. If the array is [1, 2, 3] and K is 1. Subarrays will be [2], [3], [2, 3].Let us understand with examples.Input − arr[] = { 2, 2, 1, 1, 1, 5 }; K=1Output − Count of subarrays with all elements greater than K are − 4Explanation − Subaarays will be: [2], [2], [5], [2, 2]. All elements ... Read More
We are given an array arr[] of integers. Also, two numbers A and B. The goal is to count all subarrays of arr[] such that occurrences of A and B is equal in all. If the array is [1, 2, 3] and A is 1 and B is 2. Subarrays will be [3], [1, 2], [1, 2, 3].Let us understand with examples.Input − arr[] = { 2, 2, 1, 1, 1, 5 }; A=1, B=5Output − Count of subarrays with equal no. of occurrences of two given elements are − 4Explanation − Subaarays will be − [2], [2], [2, 2], ... Read More
We are given a string str with a sentence and a number k. The goal is to find the count of in str that have ascii value less than k and words with ascii value greater than k.ASCII − Unique code as the number assigned to each character in a language.Let us understand with examples.Input − str= “This is ASCII”. k=300Output − Count of the number of words having sum of ASCII values less than k are − 1Count of number of words having sum of ASCII values greater than k are − 2Explanation − word “is” has ascii less ... Read More
We are given a number N as input. The goal is to count the total number of digits between numbers 1 to N. 1 to 9 numbers require 1 digit each, 11 to 99 require 2 digits each, 100 to 999 require 3 digits each and so on.Let us understand with examplesInput − N=11Output − Count of total number of digits from 1 to N are: 13Explanation − Numbers 1 to 9 has 1 digit each : 9 digits 10, 11 have 2 digits each. 4 digits. Total digits= 9+4=13.Input − N=999Output − Count of the total number of digits ... Read More