Found 1339 Articles for C

Biggest Reuleaux Triangle inscribed within a Square inscribed in an equilateral triangle?

Arnab Chakraborty
Updated on 01-Aug-2019 08:04:59

148 Views

Here we will see the area of biggest Reuleax triangle inscribed within a square which is inscribed in an equilateral triangle. Suppose ‘a’ is the side of the triangle. The side of the square is x and the height of the Reuleaux triangle is h.The side of the triangle is −So the value of x is −𝑥 = 0.464𝑎The height of the Reuleaux triangle is the same as x. So x = h. So the area of the Reuleaux triangle is −Example#include #include using namespace std; float areaReuleaux(float a) { //side of triangle is a    if (a ... Read More

Biggest Reuleaux Triangle inscirbed within a square inscribed in a semicircle?

Arnab Chakraborty
Updated on 01-Aug-2019 08:00:41

105 Views

Here we will see the area of biggest Reuleax triangle inscribed within a square which is inscribed in a semicircle. Suppose the radius of the semicircle is R, are side of the square is ‘a’ and the height of the Reuleax triangle is h.We know that the side of the square inscribed in a semicircle is −The height of the Reuleaux triangle is the same as a. So a = h. So the area of the Reuleaux triangle is −Example#include #include using namespace std; float areaReuleaux(float r) { //radius of the semicircle is r    if (r < ... Read More

Array with GCD of any of its subset belongs to the given array?

Arnab Chakraborty
Updated on 01-Aug-2019 07:57:21

162 Views

Here we will see one interesting problem. There is a set of N elements. We have to generate an array such that the GCD of any subset of that array lies in the given set of elements. And another constraint is that the generated array should not be more than thrice the length of the set of the GCD. For example, if there are 4 numbers {2, 4, 6, 12}, then one array will be {2, 2, 4, 2, 6, 2, 12}To solve this problem, we have to sort the list at first, then if the GCD is the same ... Read More

Array sum after dividing numbers from previous?

Arnab Chakraborty
Updated on 01-Aug-2019 07:54:29

115 Views

Here we will see one interesting problem. We will take one array, then find the sum by taking each element after dividing it by the previous elements. Let us consider an array is {5, 6, 7, 2, 1, 4}. Then the result will be 5 + (6 / 5) + (7 / 6) + (2 / 7) + (1 / 2) + (4 / 1) = 12.15238. Let us see the algorithm to get the concept.AlgorithmdivSum(arr, n)begin    sum := arr[0]    for i := 1 to n-1, do       sum := sum + arr[i] / arr[i-1]    done    return sum endExample#include using namespace std; float divSum(int arr[], int n){    float sum = arr[0];    for(int i = 1; i

Array elements with prime frequencies?

Arnab Chakraborty
Updated on 02-Jul-2020 09:45:57

436 Views

Suppose we have one array. we have to count how many of the elements present in the array prime number of times. So if the array is {1, 2, 2, 0, 1, 5, 2, 5, 0, 0, 1, 1}, then 1 is present 4 times, 2 is present 3 times, 0 is present 3 times, and 5 is present 2 times. So there are three elements {2, 0, 5} that have occurred prime number of times. So the count will be 3.AlgorithmcountPrimeOccurrence(arr, n)Begin    count := 0    define map with int type key and int type value    for ... Read More

Array elements that appear more than once?

Arnab Chakraborty
Updated on 01-Aug-2019 07:48:30

323 Views

Here we will see one problem. We have one array. our task is to find those elements whose frequencies are more than 1. Suppose the elements are {1, 5, 2, 5, 3, 1, 5, 2, 7}. Here 1 has occurred 2 times, 5 has occurred 3 times and 2 has occurred three times, others have occurred only once. So the output will be {1, 5, 2}AlgorithmmoreFreq(arr, n)Begin    define map with int type key and int type value    for each element e in arr, do       increase map.key(arr).value    done    for each key check whether the ... Read More

Array element with minimum sum of absolute differences?

Arnab Chakraborty
Updated on 02-Jul-2020 09:50:00

383 Views

Here we will see one interesting problem. We are taking one array ‘a’ with N elements. We have to find an element x such that |a[0] - x| + |a[1] - x|+ … + |a[n-1] - x| is minimized. Then we have to find the minimized sum.Let the array is: {1, 3, 9, 6, 3} now the x is 3. So the sum is |1 - 3| + |3 - 3| + |9 - 3| + |6 - 3| + |3 - 3| = 11.To solve this problem, we have to choose the median of the array as x. If ... Read More

Array element moved by k using single moves?

Arnab Chakraborty
Updated on 01-Aug-2019 07:44:08

99 Views

Suppose we have an array that has n elements from 1 to n in shuffled order. Another integer K is given. There are N peoples they are standing in a queue to play badminton. The first two players will go for playing, then the loser will go and place at the end of the queue. The winner will play with the next person from the queue and so on. They will play until someone wins K times consecutively. Then that player becomes the winner.If the queue is like [2, 1, 3, 4, 5] and K = 2, then output will ... Read More

Arrangement of words without changing the relative position of vowel and consonants?

Arnab Chakraborty
Updated on 02-Jul-2020 09:53:20

156 Views

Suppose we have a string with n elements (n < 10). We have to find the number of ways that the string can be arranged without changing the relative position of the vowel and consonants.The approach is simple. We have to count the number of vowels and consonants in the given string, then we have to find how many ways we can arrange the vowels only, then find the number of ways to arrange the consonant only, after that multiply these two results to get the total ways.AlgorithmarrangeWayCount(str)Begin    define an array ‘freq’ to store frequency.    count and place ... Read More

Arrange first N natural numbers such that absolute difference between all adjacent elements > 1?

Arnab Chakraborty
Updated on 02-Jul-2020 09:54:17

383 Views

We have the first N natural numbers. Our task is to get one permutation of them where the absolute difference between every two consecutive elements is > 1. If no such permutation is present, return -1.The approach is simple. We will use the greedy approach. We will arrange all odd numbers in increasing or decreasing order, then arrange all even numbers in decreasing or increasing orderAlgorithmarrangeN(n)Begin    if N is 1, then return 1    if N is 2 or 3, then return -1 as no such permutation is not present    even_max and odd_max is set as max even ... Read More

Advertisements