Programming Articles - Page 2515 of 3366

Array elements that appear more than once?

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

340 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

393 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

112 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

167 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

400 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

492 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

273 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

1K+ 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

What can cause the \"cannot find symbol\" error in Java?

Vivek Verma
Updated on 29-May-2025 05:39:49

12K+ Views

In Java, the cannot find symbol error occurs when we try to reference a variable that is not declared in the program or when we try to access a class that is not declared or imported.  Possible Causes of 'Cannot Find Symbol' Error Below is a list of some possible causes of the 'cannot find symbol' error in Java - Using a variable that is not declared or outside the code. Using wrong cases ("tutorials" and "Tutorials" are different) or making spelling mistakes. The packaged class has ... Read More

When can we call wait() and wait(long) methods of a Thread in Java?

Vivek Verma
Updated on 29-May-2025 17:52:43

649 Views

This article will discuss the wait() and wait(long timeout) methods, along with a suitable example that explains when to call these methods in a thread in Java. The wait() Method In Java, the wait() method is used in multithreading and causes the current thread to "wait" until another thread calls notify() or notifyAll() on the same object. When the wait() method is called, the thread moves from the running to the waiting state and resumes only when notified, if not a deadlock will be formed. Note: Call the wait() method only when the thread should wait without any time limit. Following ... Read More

Advertisements