Found 1401 Articles for C

Array element moved by k using single moves?

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

42 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

91 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

246 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

Area of Reuleaux Triangle?

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

254 Views

Here we will see how to calculate the area of Reuleaux Triangle like below. The Reuleaux Triangle has one equilateral triangle inside it. Suppose its height is h, this shape is made by the intersection of three circles.There are three circular sectors. The area of each sector is −Since the area of the equilateral triangle is added three times, then we have to subtract them. So the final area is −Example#include #include using namespace std; float areaReuleaux(float h) {    if (h < 0) //if h is negative it is invalid    return -1;    float area = ... Read More

Area of a n-sided regular polygon with given Radius?

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

216 Views

Here we will see how to get the area of an n-sided regular polygon whose radius is given. Here the radius is the distance from the center of any vertex. To solve this problem, we have drawn one perpendicular from the center to one side. Let each side is of length ‘a’. The perpendicular is dividing the side into two parts. The length of each part is a/2. The perpendicular and one radius is making an angle x. Let the length of the radius is h.Here we can see that the polygon is divided into N equal triangles. So for ... Read More

Area of a leaf inside a square?

Arnab Chakraborty
Updated on 01-Aug-2019 07:26:47

680 Views

Here we will see how to get the area of a leaf-like below, which is present inside the square ABCD. Each side of the square is of length ‘a’.The leaf has two equal parts. Area of each part is said p, now −And the area of the full leaf is 2p.Example#include using namespace std; float leafArea(float a){    return (a * a * (3.1415/2 - 1)); } int main() {    float square_side = 7.0f;    cout

Area of a circle inscribed in a rectangle which is inscribed in a semicircle?

Arnab Chakraborty
Updated on 31-Jul-2019 13:38:21

85 Views

Let us consider one semicircle is given. Its radius is R. One rectangle of length l and breadth b is inscribed in that semi-circle. Now one circle with radius r is inscribed in the rectangle. We have to find the area of the inner circle.As we know biggest rectangle that can be inscribed within the semi-circle has length l and breadth b, then the equation of l and b will be like following −Now, the biggest circle that can be inscribed within the rectangle has radius r is like below −Example#include #include using namespace std; float innerCircleArea(float R){ ... Read More

Arc length from given Angle?

Arnab Chakraborty
Updated on 31-Jul-2019 13:33:53

120 Views

Here we will see how to get the arc length from the given angle. One circle is given. The radius of the circle is given. Our task is to get the arc length using the radius and the angle. The angle is in degree.Here r and x is given. We have to find the value of L. The formula is like below −𝐿 = 2𝜋𝑟 ∗ (𝑥/360)Example#include using namespace std; float getArcLength(float r, float x){    return (2 * 3.1415f * r) * (x / 360.0f); } int main() {    float rad = 12.0f;    float angle = 45.0f;    cout

Anti Clockwise spiral traversal of a binary tree?

Arnab Chakraborty
Updated on 31-Jul-2019 13:31:46

168 Views

Here we will see one interesting problem. We have one binary tree. We have to traverse the tree in anti-clockwise manner. The traversal will be like below −The traversal sequence is 1, 8, 9, 10, 11, 12, 13, 14, 15, 3, 2, 4, 5, 6, 7AlgorithmantiClockTraverse(root)Begin    i := 1, j := height of the tree    flag := false    while i data = data;       this->left = NULL;       this->right = NULL;    } }; int getHeight(Node* root) {    if (root == NULL)    return 0;    //get height of left and right ... Read More

An interesting solution to get all prime numbers smaller than n?

Arnab Chakraborty
Updated on 02-Jul-2020 09:40:59

92 Views

Here we will see how to generate all prime numbers that are less than n in an efficient way. In this approach we will use the Wilson’s theorem. According to his theorem if a number k is prime, then ((k - 1)! + 1) mod k will be 0. Let us see the algorithm to get this idea.This idea will not work in C or C++ like language directly, because it will not support the large integers. The factorial will generate large numbers.AlgorithmgenAllPrime(n)Begin    fact := 1    for i in range 2 to n-1, do       fact ... Read More

Advertisements