Programming Articles

Page 1413 of 2547

Minimum swaps required to bring all elements less than or equal to k together in C++

Narendra Kumar
Narendra Kumar
Updated on 11-Mar-2026 593 Views

Problem statementGiven an array of n positive integers and a number k. Find the minimum number of swaps required to bring all the numbers less than or equal to k together.ExampleIf input array is = {1, 5, 4, 7, 2, 10} and k = 6 then 1 swap is required i.e. swap element 7 with 2.AlgorithmCount all elements which are less than or equals to ‘k’. Let’s say the count is ‘cnt’Using two pointer technique for window of length ‘cnt’, each time keep track of how many elements in this range are greater than ‘k’. Let’s say the total count ...

Read More

Convex Hull Monotone chain algorithm in C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 316 Views

In this tutorial, we will be discussing a program to find the convex hull of a given set of points.Convex hull is the smallest polygon convex figure containing all the given points either on the boundary on inside the figure.Example#include #define llu long long int using namespace std; //structure for the given point struct Point {    llu x, y;    bool operator= t && calc_crossproduct(ans[k - 2],       ans[k - 1], A[i - 1])

Read More

Minimum Swaps required to group all 1's together in C++

Narendra Kumar
Narendra Kumar
Updated on 11-Mar-2026 303 Views

Problem statementGiven an array of 0’s and 1’s. The task is to find the minimum number of swaps required to group all 1’s present in the array together.ExampleIf input array = {1, 0, 1, 1, 0, 1} then 1 swap is required. i.e. swap first 0 with last 1.AlgorithmCount total number of 1’s in the arrayIf count is x, then we need to find the subarray of length x of this array with maximum number of 1’sMinimum swaps required will be the number of 0’s in the subarray of length x with maximum number of 1’sExample#include using namespace std; ...

Read More

Print direction of moves such that you stay within the [-k, +k] boundary in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 268 Views

In this problem, we have to find a valid way to move positive direction or negative direction in such a way that we stay within a certain limit that is provided by the user.Here, We are given a certain maximum limit K, which is the maximum value to which we can move and an array of n positive values to move. We have to return the sequence i.e. positive or negative directions to move so that it never crosses K value.Let’s take an example to understand the topic better, Input : K = 56 and the array is [25 , ...

Read More

Convex Hull using Divide and Conquer Algorithm in C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 1K+ Views

In this tutorial, we will be discussing a program to find the convex hull of a given set of points.Convex hull is the smallest polygon convex figure containing all the given points either on the boundary on inside the figure.In this program, we will use brute force to divide the given points into smaller segments and then finally merging the ones that follow on to construct the convex hull.Example#include using namespace std; //storing the center point of polygon pair mid; //calculating the quadrant of //a particular point int quad(pair p){    if (p.first >= 0 && p.second >= 0)   ...

Read More

Minimum swaps required to make a binary string alternating in C++

Narendra Kumar
Narendra Kumar
Updated on 11-Mar-2026 493 Views

Problem statementGiven a binary string of even length and equal number of 0’s and 1’s. What is the minimum number of swaps to make the string alternating? A binary string is alternating if no two consecutive elements are equalExampleIf str = 11110000 then 2 swaps are required.AlgorithmCount number of zeroes at odd position and even position of the string. Let their count be oddZeroCnt and evenZeroCnt respectivelyCount number of ones at odd position and even position of the string. Let their count be oddOneCnt and evenOneCnt respectivelyWe will always swap a 1 with a 0. So we just check if ...

Read More

Print digit's position to be removed to make a number divisible by 6 in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 332 Views

In this problem, we are given number and we have to remove more digit from the number. So that the new number formed after removal is divisible by 6.Let’s take an example to learn the concept better −Input : 1324 Output : 4Explanation − on removing the 4th number we will get 132 which is divisible by 6.Here, we are given a number and we have to return the position from where the number is removed to make it divisible by 6.For solving this problem, we will try to create a logic that solves the problem. For this, we will ...

Read More

Coordinates of rectangle with given points lie inside in C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 278 Views

In this tutorial, we will be discussing a program to find the coordinates of rectanglewith given points lying inside.For this we will be provided with some coordinate points. Our task is to find the smallest rectangle such that all the points lie inside it and it should have its sides parallel to the coordinate axis.Example#include using namespace std; //calculating the coordinates of smallest rectangle void print_rectangle(int X[], int Y[], int n){    //finding minimum and maximum points    int Xmax = *max_element(X, X + n);    int Xmin = *min_element(X, X + n);    int Ymax = *max_element(Y, Y ...

Read More

Minimum toggles to partition a binary array so that it has first 0s then 1s in C++

Narendra Kumar
Narendra Kumar
Updated on 11-Mar-2026 163 Views

Problem statementGiven an array of n integers containing only 0 and 1. Find the minimum toggles (switch from 0 to 1 or vice-versa) required such the array the array become partitioned, i.e., it has first 0s then 1s.ExampleIf arr[] = {1, 0, 0, 1, 1, 1, 0} then 2 toggle is required i.e. toggle first one and last zero.AlgorithmIf we observe the question, then we will find that there will definitely exist a point from 0 to n-1 where all elements left to that point should contains all 0’s and right to point should contains all 1’sThose indices which don’t ...

Read More

Minimum value among AND of elements of every subset of an array in C++

Narendra Kumar
Narendra Kumar
Updated on 11-Mar-2026 228 Views

Problem statementGiven an array of integers, the task is to find the AND of all elements of each subset of the array and print the minimum AND value among all those.ExampleIf arr[] = {1, 2, 3, 4, 5} then (1 & 2) = 0 (1 & 3) = 1 (1 & 4) = 0 (1 & 5) = 1 (2 & 3) = 2 (2 & 4) = 0 (2 & 5) = 0 (3 & 4) = 0 (3 & 5) = 1 (4 & 5) = 4AlgorithmThe minimum AND value of any subset of the array will be ...

Read More
Showing 14121–14130 of 25,466 articles
Advertisements