Found 7347 Articles for C++

Making List Values Equal in C++

Arnab Chakraborty
Updated on 02-Sep-2020 11:28:51

97 Views

Suppose we have a list of integers called nums. Now suppose an operation where we select some subset of integers in the list and increment all of them by one. We have to find the minimum number of required operations to make all values in the list equal to each other.So, if the input is like [1, 3, 5], then the output will be 4.To solve this, we will follow these steps −if size of nums is same as 1, then −return 0ret := 0maxVal := -infminVal := inffor initialize i := 0, when i < size of nums, update ... Read More

Longest Interval Containing One Number in C++

Arnab Chakraborty
Updated on 02-Sep-2020 11:27:25

108 Views

Suppose we have a list of distinct integers called nums. We have to find the size of the largest interval (inclusive) [start, end] such that it contains at most one number in nums.So, if the input is like nums = [10, 6, 20], then the output will be 99990, as the largest interval is [11, 100000], this contains 20 only.To solve this, we will follow these steps −ret := -infend := 100000prev := 1sort the array numsn := size of numsfor initialize i := 0, when i < size of nums, update (increase i by 1), do −if i + ... Read More

Count number of sub-sequences with GCD 1 in C++

Sunidhi Bansal
Updated on 31-Aug-2020 11:56:01

253 Views

We are given an array of integer elements and the task is to find the sub-sequences from the given array which are having GCD as 1. GCD is the greatest common divisor of two or more integers that divides the given numbers entirely and greatest amongst all.Input − int arr[] = {3, 4, 8, 16}Output − Count of number of sub-sequences with GCD 1 are − 7Explanation −The sub-sequences that can be formed from the given array with GCD as 1 are (3, 4), (3, 8), (3, 16), (4, 3), (8, 3), (16, 3), (3, 4, 8)Input − int arr[] ... Read More

Count number of occurrences (or frequency) in a sorted array in C++

Sunidhi Bansal
Updated on 31-Aug-2020 11:51:01

2K+ Views

We are given a sorted array of integer type elements and the number let’s say, num and the task is to calculate the count of the number of times the given element num is appearing in an array.Input − int arr[] = {1, 1, 1, 2, 3, 4}, num = 1Output − Count of number of occurrences (or frequency) in a sorted array are − 3Input − int arr[] = {2, 3, 4, 5, 5, 6, -7}, num = 5Output − Count of number of occurrences (or frequency) in a sorted array are − 2Input − int arr[] = {-1, ... Read More

Count rows/columns with sum equals to diagonal sum in C++

Sunidhi Bansal
Updated on 31-Aug-2020 11:43:47

116 Views

We are given a matrix which is a 2-D array having rows and columns and the task is to calculate the count of sum of all the rows and columns such that it is equal to the sum of either principal or secondary matrix.Input −int arr[row][col] = {    { 4, 1, 7 },    { 10, 3, 5 },    { 2, 2, 11} }Output − Count of rows/columns with sum equals to diagonal sum are &mins; 2Explanation −sum of principal diagonal is: 4 + 3 + 11 = 18 and sum of secondary diagonal is: 7 + 3 ... Read More

Maximum circular subarray sum in C++

Sunidhi Bansal
Updated on 31-Aug-2020 11:38:09

258 Views

We are given an array and the task is to form the subarrays such that the sum of subarrays in a circular fashion will result in a maximum value.Input − int arr[] = {1, 2, 8, 4, 3, 0, 7}Output − Maximum circular subarray sum is − 22Explanation − we are given an array containing {1, 2, 8, 4, 3, 0, 7} and the subarray of it with maximum sum will be 7 + 1 + 2+ 8 + 4 is 22.Input − int arr[] = { 2, 5, -1, 6, 9, 4, -5 }Output − Maximum circular subarray sum ... Read More

Count smaller values whose XOR with x is greater than x in C++

Sunidhi Bansal
Updated on 31-Aug-2020 11:33:41

108 Views

We are given an integer number let’s say, x and the task are to count the smaller numbers less than x whose XOR with x will result in a value greater than the XOR value.The truth table for XOR operation is given belowABA XOR B000101011110Input − int x = 11Output − Count of smaller values whose XOR with x is greater than x are − 4Explanation −We are given with the x as 11 which means we need to find XOR of x with the numbers less than x. So the numbers are 1 XOR 11 < 11(FALSE), 2 XOR ... Read More

Count smaller numbers whose XOR with n produces greater value in C++

Sunidhi Bansal
Updated on 31-Aug-2020 11:30:51

74 Views

We are given an integer number let’s say, num and the task is to count the smaller numbers less than num whose XOR with num will result in a value greater than the XOR value..The truth table for XOR operation is given belowABA XOR B000101011110Input − int num = 11Output − Count of smaller numbers whose XOR with n produces greater value are − 4Explanation −We are given with the num as 11 which means we need to find XOR of num with the numbers less than num. So the numbers are 1 XOR 11 < 11(FALSE), 2 XOR 11 ... Read More

Count of alphabets having ASCII value less than and greater than k in C++

Sunidhi Bansal
Updated on 31-Aug-2020 11:28:41

237 Views

We are given a string of any length and the task is to calculate the count of alphabets having ASCII values less than or greater than or equals to the given integer value k.ASCII value for character A-Z are given belowABCDEFGHIJKLMNOPQRS65666768697071727374757677787980818283TUVWXYZ84858687888990ASCII value for characters a-z are given belowabcdefghijklmnopqrs979899100101102103104105106107108109110111112113114114tuvwxyz116117118119120121122Input − str = “TuTorials PoinT”, int k = 100Output −Count of alphabets having ASCII value less than k are − 6Count of alphabets having ASCII value equals or greater than k are − 9Explanation −We are given with k as 100 so we will check ASCII values of the characters in the ... Read More

Count of pairs from 1 to a and 1 to b whose sum is divisible by N in C++

Sunidhi Bansal
Updated on 31-Aug-2020 11:24:22

89 Views

We are given an integer array and the task is to count the total number of pairs (x, y) that can be formed using the given array values such that the integer value of x is less than y.Input − int a = 2, b = 3, n = 2Output − Count of pairs from 1 to a and 1 to b whose sum is divisible by N are − 3Explanation −Firstly, We will start from 1 to a which includes 1, 2 Now, we will start from 1 to b which includes 1, 2, 3 So the pairs that ... Read More

Advertisements