C++ Articles

Page 104 of 597

Count number of step required to reduce N to 1 by following certain rule in C++

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

We are given a number N. The goal is to count the number of steps required to reduce the number to 1 by following rules −If the number is power of 2, reduce it to its half.Else reduce it to the N-(nearest power of 2 which is less than N).For step 1, we will check if N is power of 2, by checking if ceil(log2(N)), floor(log2(N)) return the same result. If yes then N=N/3, increment count of operation.If the result of step 1 is false then we will perform step 2 and subtract the nearest power of 2 less than ...

Read More

Large to Small Sort in C++

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

Suppose we have a list of integers nums, we have to sort the list in this manner −First element is maximumSecond element is minimumThird element is 2nd maximumFourth element is 2nd minimumAnd so on.So, if the input is like [6,3,10,4], then the output will be [10, 3, 6, 4]To solve this, we will follow these steps −Define an array retsort the array numsj := size of nums - 1i := 0while i

Read More

Count number of triplets (a, b, c) such that a^2 + b^2 = c^2 and 1<=a<=b<=c<= n in C++

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

We are given an integer n. The goal is to find triplets ( set of 3 numbers ) that satisfy the conditions −a2+b2=c21

Read More

Beer Bottles in C++

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

Suppose we have one number n. Here n indicates n full beer bottles. If we can exchange 3 empty beer bottles for 1 full beer bottle, we have to find the number of beer bottles we can drink.So, if the input is like 10, then the output will be 14.To solve this, we will follow these steps −Define a function solve(), this will take n, ret := 0while n >= 3, do −q := n / 3ret := ret + q * 3n := n - q * 3n := n + qret := ret + nreturn retLet us see ...

Read More

Count number of triplets in an array having sum in the range [a,b] in C++

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

We are given an array of integers, Arr[] and two variables a and b to define a range [a,b]. The goal is to find the number of triplets whose sum lies in between this range [a,b].We will do this by using three for loops. Increment count if arr[i]+arr[j]+arr[k]>=a and arr[i]+arr[j]+arr[k]

Read More

Most Frequent Number in Intervals in C++

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

Suppose we have a list of lists of integers intervals where each element has interval like [start, end]. We have to find the most frequently occurred number in the intervals. If there are ties, then return the smallest number.So, if the input is like [[2, 5], [4, 6], [7, 10], [8, 10]], then the output will be 4To solve this, we will follow these steps −Define one map mcnt := 0, val := 0for each value it in x −(increase m[it[0]] by 1)decrease m[it[1] + 1] by 1last := 0for each key it in mlast := last + value of ...

Read More

Count of index pairs with equal elements in an array in C++

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

We are given with an array of N elements. The goal is to find the index pairs (i, j) which have the same element value such that i!=j. i.e, Arr[i]=Arr[j] and i!=j. This is used to make pairs of gloves of equal size. Out of N gloves only paired gloves are useful to sell.We will do this by running two loops with 0 -1. Total pairs=2Approach used in the below program is as followsWe take an integer array Arr[] initialized with random numbers for size of gloves > 0.Take a variable n which stores the length of Arr[].Function countPairs(int arr[], ...

Read More

TV Shows in C++

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

Suppose we have a list of TV shows, and another list of duration, and an integer k, here shows[i] and duration[i] shows the name and duration watched by the ith person, we have to find the total duration watched of the k most watched shows.So, if the input is like shows: ["Castle Play", "Fairy Tale Series", "Castle Play", "Jerry Mouse", "Rich Boy"], duration: [6, 4, 6, 14, 5] and k = 2, then the output will be 26.To solve this, we will follow these steps −Define one map mn := size of vfor initialize i := 0, when i < ...

Read More

Counting numbers whose difference from reverse is a product of k in C++

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

We are given a range [l,r] and a number k. The goal is to find all the numbers between l and r (l

Read More

Minimum String in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 1K+ Views

Suppose we have two strings s and t of same length, and both are in lowercase letters. Consider we have rearranged s at first into any order, then count the minimum number of changes needed to turn s into t.So, if the input is like s = "eccynue", t = "science", then the output will be 2 as if we rearrange "eccynue" to "yccence", then replace y with s and second c with i, it will be "science".To solve this, we will follow these steps −ret := 0Define two arrays cnt1 to hold frequency of s and cnt2 to hold ...

Read More
Showing 1031–1040 of 5,962 articles
« Prev 1 102 103 104 105 106 597 Next »
Advertisements