Find the Largest Three Elements in an Array in C++

Akansha Kumari
Updated on 29-Jul-2025 17:55:31

10K+ Views

An array is a group of similar data elements stored at contiguous memory locations. It is one of the fundamental data structures in programming and used to store and manage multiple values of the same type. Here, we are given an array arr[] containing N unsorted elements, and our task is to find the three largest elements from this array. To understand the problem better. let's see the following example scenarios: Scenario 1 Input: arr[] = {7, 3, 9, 12, 1} Output: 12, 9, 7 Scenario 2 Input: arr[] = {15, 22, 6, 3, 11, 8} Output: 22, 15, ... Read More

3-Way Merge Sort in C++

Aman Kumar
Updated on 29-Jul-2025 16:22:31

2K+ Views

Merge sort is a popular sorting algorithm that follows the divide-and-conquer strategy. It works by recursively dividing the input array into two halves, sorting each half, and then merging them. With a time complexity of O(n log n). 3-Way Merge Sort An optimized variation of merge sort is the 3-way merge sort, where the array is divided into three equal parts instead of two. This reduces the number of recursive calls and improves performance in certain scenarios. 3-Way Merge Sort algorithm Following are the steps (algorithm) to implement the 3-way merge sort: Divide the array ... Read More

Can a Constructor Be Made Final in Java

Aishwarya Naglot
Updated on 29-Jul-2025 16:21:25

7K+ Views

A constructor is a special method in Java that is used to initialize objects. It gets called when an instance of a class is created. The constructor has the same name as the class and does not have a return type. The question is whether a constructor can be declared or made final in Java? The answer is no. Let's understand why. Why can't we declare a Java Constructor "final"? In Java, the final keyword is used to restrict modification of the members of a class (methods and variables). For example, a final method of a class cannot be overridden ... Read More

3Sum Closest in C++

Aman Kumar
Updated on 29-Jul-2025 15:18:00

1K+ Views

The 3 Sum Closest problem involves finding the sum of three numbers in an array that is closest to a given target value. We are given an integer array nums of length n and an integer target. Our goal is to find three integers in the array such that their sum is as close as possible to the target using C++ program. In this task, we assume that each input has exactly one solution. If there are multiple sums equally close to the target, we return the maximum one. Let's consider the following example scenario to understand the problem more ... Read More

Area of a N-Sided Regular Polygon in C++

Aman Kumar
Updated on 29-Jul-2025 14:47:49

1K+ Views

A regular polygon with n sides is a two-dimensional shape with n equal-length sides and n equal interior angles. It is both equilateral and equiangular (all sides are equal and all angles are equal). In the above diagram, we see that the polygon can be divided into n equal triangles. For one of the triangles, the complete angle at the center can be divided into = 2pi/n So, angle t = pi/n Now, tan(t) = a/2*h So, h = a/(2*tan(t)) Area of ... Read More

Area of a Polygon with Given n Ordered Vertices in C++

Aman Kumar
Updated on 29-Jul-2025 14:39:53

996 Views

A polygon is a closed two-dimensional shape formed by connecting three or more straight lines end-to-end. These lines form sides, and their connection points are called vertices. When the vertices of the polygon are given in a specific order either clockwise or counter-clockwise, we can calculate the area of the polygon using a mathematical formula known as the Shoelace Formula or Surveyor’s Formula. You are given the coordinates of a polygon with n vertices. The vertices are provided in an ordered manner, meaning they are listed either in clockwise or anticlockwise order starting from the first vertex to the last. ... Read More

Alternate Lower Upper String Sort in C++

Nishu Kumari
Updated on 29-Jul-2025 14:28:24

606 Views

We are given a string that contains both lowercase and uppercase characters, and we have to sort them in an alternate way, meaning one lowercase letter, then one uppercase letter, then again a lowercase letter, and so on, all in sorted order within their cases. Let's understand this with a few example scenarios. Scenario 1 Input: "aFegrAfStRzsV" Output: "AagfRsSeTvz" Explanation: Sorted uppercase letters: A, F, R, S, T, V Sorted lowercase letters: a, e, f, g, r, s, z We place one uppercase letter, then one lowercase letter, starting with an uppercase. We repeat this until all letters ... Read More

Add One to Number Represented as Array of Digits in C++

Nishu Kumari
Updated on 29-Jul-2025 14:22:35

309 Views

In this problem, we are given a number in the form of an array, and each digit of the number is stored at one index of the array, and the most significant digit comes first. Our task is to add 1 to this number and return the updated number in the same array format. Let's understand it with some example scenarios. Scenario 1 Input: Input_arr[] = {2, 6, 1} Output: 262 Explanation: The array represents 261. Adding 1 gives 261 + 1 = 262. Scenario 2 Input: input_arr[] = {5, 9, 9, 9} Output: 6000 Explanation: The array ... Read More

Alternate Odd and Even Nodes in a Singly Linked List in C++

Nishu Kumari
Updated on 29-Jul-2025 12:57:44

356 Views

Given a singly linked list, we need to rearrange its nodes so that even and odd numbers come one after the other alternatively. If the list starts with an even number, the next should be odd, then even, and so on. Similarly, if it begins with an odd number, the next should be even, then odd, and so on. Let's look at some example scenarios to understand the concept better. Scenario 1 Input: 45 -> 21 -> 2 -> 213 -> 3 -> 34 -> 78 -> 12 Output: 45 -> 2 -> 21 -> 34 -> 213 -> 78 ... Read More

Additive Number in C++

Nishu Kumari
Updated on 28-Jul-2025 19:30:40

677 Views

We are given a string containing only digits from 0 to 9, and we need to check whether it is an additive number. An additive number is a string that can form an additive sequence, where each number (starting from the third) is the sum of the previous two. For the sequence to be valid, it must have at least three numbers. Let's look at the example scenarios to understand the problem clearly: Scenario 1 Input: "112358" Output: true Explanation: The digits can form the sequence: 1, 1, 2, 3, 5, 8 Here, 1 + 1 = 2 1 ... Read More

Advertisements