C++ Articles - Page 259 of 719

Count rotations divisible by 8 in C++

Sunidhi Bansal
Updated on 01-Dec-2020 12:57:52

348 Views

We are given a large number. The goal is to count the rotations of num that are divisible by 8.As the rotations can not be done again and again. We will use the divisible by 8 property. If the last three digits are divisible by 8then the number is divisible by 8. If the number is 1800 then it’s rotations will be 1800, 0180, 0018, 8001 out of 1800 is divisible by 8.Let us understand with examples.Input − num=15320Output − Count of rotations divisible by 4 are: 1Explanation − Rotations are −15320, 01532, 20153, 32015, 53201 Out of these, only ... Read More

Count rotations divisible by 4 in C++

Sunidhi Bansal
Updated on 01-Dec-2020 12:55:38

705 Views

We are given a large number. The goal is to count the rotations of num that are divisible by 4.As the rotations can not be done again and again. We will use the divisible by 4 property. If the last two digits are divisible by 4 then the number is divisible by 4. If the number is 1234 then it’s rotations will be 1234, 4123, 3412, 2341 out of which 3412 will be divisible by 4 as the last two digits 12 is divisible by 4.Let us understand with examples.Input − num=15324Output − Count of rotations divisible by 4 are: ... Read More

Count rotations in sorted and rotated linked list in C++

Sunidhi Bansal
Updated on 01-Dec-2020 12:52:56

196 Views

We are given a linked list. The list is sorted first and then rotated by K number of nodes. The goal is to find the value of K. If we are given below linked list as input which is rotated by K number of nodes −Then original must have been −And we can see K here is 2. Input linked list is a rotation of 2 nodes in the original sorted linked list.Let us understand with examples.Input − List : 5 → 7 → 9 → 1 → 3Output Elements in the linked list are: 5 7 9 1 3Count of ... Read More

Count rotations of N which are Odd and Even in C++

Sunidhi Bansal
Updated on 01-Dec-2020 12:50:06

191 Views

We are given a number N. The goal is to count the rotations of N that make an odd number and rotations that make an even number. If the number N is 123 its rotations would be 123, 321, 132. The odd rotations are 123 and 321 ( 2 ) and even rotation is 132 ( 1 ).Let us understand with examples.Input − N= 54762Output −Count of rotations of N which are Odd are − 2Count of rotations of N which are Even are − 3Explanation − Rotations are −54762, 25476, 62547, 76254, 47625.Even rotations are 3 − 54762, 25476, ... Read More

Count rows in a matrix that consist of same element in C++

Sunidhi Bansal
Updated on 01-Dec-2020 12:48:41

860 Views

We are given a matrix consisting of integers. The goal is to find the number of rows in the matrix that have all identical elements in it.If there is 5X4 matrix as shown −15131111115323577777The answer would be 2, row 1 (having all 1’s) and row 3 (having all 7’s) contain the same element.Let us understand with examples.Input matrix =    [ 1 1 1 1 ]    [ 2 3 2 7 ]    [ 3 3 3 3 ]Output − Count of rows in a matrix that consist of same element are − 2Explanation − Row 0 contains all 1’s ... Read More

Count special palindromes in a String in C++

Sunidhi Bansal
Updated on 01-Dec-2020 12:43:57

658 Views

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

Count sub-arrays which have elements less than or equal to X in C++

Sunidhi Bansal
Updated on 01-Dec-2020 12:42:14

330 Views

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

Count subarrays consisting of only 0’s and only 1’s in a binary array in C++

Sunidhi Bansal
Updated on 01-Dec-2020 12:39:47

517 Views

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

Count subarrays having total distinct elements same as original array in C++

Sunidhi Bansal
Updated on 01-Dec-2020 12:37:35

240 Views

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

Count Subarrays with Consecutive elements differing by 1 in C++

Sunidhi Bansal
Updated on 01-Dec-2020 12:35:08

325 Views

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

Advertisements