Programming Articles

Page 219 of 2544

Check if it is possible to reach vector B by rotating vector A and adding vector C to its in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 193 Views

Suppose we have three vectors x, y and z in 2D plane. We have to check whether we can get vector y from vector x by rotating it 90 degrees(clockwise) or adding z with it any number of times as required.So, if the input is like x = (-4, -2) y = (-1, 2) z = (-2, -1), then the output will be True as we can add z with x to get location (-2, -1), then rotate 90° clockwise to get (-1, 2).To solve this, we will follow these steps −Define a function util() . This will take p, ...

Read More

Count the number of common divisors of the given strings in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 11-Mar-2026 349 Views

Given two strings numo and demo as input. The goal is to find the number of common divisors of both the strings. The divisors of a string are found using following technique: If string str has sub1 as its divisor then we can construct str using sub1 by repeating it any number of times till str is generated. Example: str=abcabcabc sub1=abcFor ExampleInputnumo = "abababab" demo = "abababababababab"OutputCount of number of common divisors of the given strings are: 2ExplanationThe strings can be generated using following divisor substrings : “ab”, “abab”Inputnumo = "pqqppqqp" demo = "pqpq"OutputCount of number of common divisors of ...

Read More

Check if it is possible to rearrange a binary string with alternate 0s and 1s in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 427 Views

Suppose we have a binary string s whose length is 2 or more. We have to check whether we can rearrange s such that there are alternate 0s and 1s.So, if the input is like s = "1000111", then the output will be True as we can form "1010101" from s.To solve this, we will follow these steps −one_count := count of 1 in binary string szero_count := count of 0 in binary string sif size of s is even, thenreturn true when one_count is same as zero_count otherwise falsereturn true when |one_count - zero_count| is same as 1 otherwise ...

Read More

Count subsets that satisfy the given condition in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 11-Mar-2026 470 Views

Given an array of numbers and an integer x as input. The goal is to find all the subsets of arr[] such that individual elements of that set as well as the sum of them fully divisible by x.For ExampleInputarr[] = {1, 2, 3, 4, 5, 6} x=3OutputCount of subsets that satisfy the given condition :3ExplanationThe subsets will be: [3], [6], [3, 6]Inputarr[] = {1, 2, 3, 4, 5, 6} x=4OutputCount of subsets that satisfy the given condition :1ExplanationThe subsets will be: [4]Approach used in the below program is as follows −In this approach we will count the elements of ...

Read More

Check if it is possible to rearrange rectangles in a non-ascending order of breadths in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 176 Views

Suppose we have a list of rectangles represented using its length and breadth. We can rotate any rectangle by 90 degrees so after rotating, the breadth will become length and vice-versa. We have to check whether we can sort the rectangles in non-increasing order of breadth.So, if the input is like rects = [[4, 5], [5, 7], [4, 6]], then the output will be True as the breadths are [5, 7, 6] now if we rotate last two rectangles then breadths will be [5, 5, 4] which is in non-increasing way.To solve this, we will follow these steps −m := ...

Read More

Count squares with odd side length in Chessboard in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 11-Mar-2026 885 Views

Given a number size as input as dimension of size*size Chessboard. The goal is to find the number of squares that can be formed inside that board having odd lengths.For ExampleInputsize=3OutputCount of squares with odd side length in Chessboard are: 10ExplanationAll squares will be as shown : and 1 whole square of size 3x3.Inputsize=4OutputCount of squares with odd side length in Chessboard are: 20Explanationthere will be 16, 1X1 squares. And 4, 3X3 squares inside it.Approach used in the below program is as follows −In this approach we will traverse from length of square as 1 to length as size. For ...

Read More

Check if it is possible to return to the starting position after moving in the given directions in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 243 Views

Suppose we are at position (0, 0) We have a string represents consecutive directions using four letters. We have to check whether we can return at (0, 0) position after considering all of the given directions. The symbols areE for eastW for westN for northS for south.So, if the input is like "EENWWS", then the output will be true, move east two units, then go north, then west two units then again south, so this is the starting position.To solve this, we will follow these steps −l := size of moves arrayif l is same as 0, then −return truelft ...

Read More

Count subarrays whose product is divisible by k in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 11-Mar-2026 408 Views

Given an array arr[] and an integer k as input. The goal is to find the number of subarrays of arr[] such that the product of elements of that subarray is divisible by k.For ExampleInputarr[] = {2, 1, 5, 8} k=4OutputCount of sub-arrays whose product is divisible by k are: 4ExplanationThe subarrays will be: [ 8 ], [ 5, 8 ], [ 1, 5, 8 ], [ 2, 1, 5, 8 ].Inputarr[] = {7, 1, 9, 7} k=9OutputCount of sub−arrays whose product is divisible by k are: 6ExplanationThe subarrays will be: [ 9 ], [ 9, 7 ], [ 1, ...

Read More

Count subtrees that sum up to a given value x in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 11-Mar-2026 382 Views

Given a binary tree and a value x as input. The goal is to find all the subtrees of a binary tree that have sum of weights of its nodes equal to x.For ExampleInputx = 14. The tree which will be created after inputting the values is given belowOutputCount of subtrees that sum up to a given value x are: 1Explanationwe are given with a x value as 14. As we can see there is only one leaf node with the values as 14 therefore the count is 1.Inputx = 33. The tree which will be created after inputting the ...

Read More

Check if it is possible to serve customer queue with different notes in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 147 Views

Suppose we have an array called notes represents different rupee notes holding by customers in a queue. They are all waiting to buy tickets worth Rs 50. Here possible notes are [50, 100 and 200]. We have to check whether we can sell tickets to the people in order or not, initially we have Rs 0 in our hand.So, if the input is like notes = [50, 50, 100, 100], then the output will be True for first two we do not need to return anything, but now we have two Rs 50 notes. So for last two we can ...

Read More
Showing 2181–2190 of 25,433 articles
« Prev 1 217 218 219 220 221 2544 Next »
Advertisements