Hafeezul Kareem

Hafeezul Kareem

259 Articles Published

Articles by Hafeezul Kareem

Page 4 of 26

Largest number with one swap allowed in C++

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 522 Views

In this tutorial, we are going to write a program that finds the largest number with a single swap.Let's see the steps to solve the problem.Initialise the number n.Convert the integer to string.Write a loop that iterates from the ending of the string.Find the max digit and index.If the current digits is less than the max digit, then update start index with current index and end index with max digit index.If the start index is -1, then return n.Else swap the digits in the start and end indexes.Return the integer by converting.ExampleLet's see the code.#include using namespace std; int ...

Read More

Largest number with prime digits in C++

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 437 Views

In this tutorial, we are going to write a program that finds the largest number with prime digits that is less than n.Let's see the steps to solve the problem.Write a loop that iterates from 0 to n.If the current digit is not prime.While the digit is less 2, decrement the i value. If the i value is negative, then make it 0.Update the current index value with the next smallest prime digit.From the next index, make every digit to 7.Return n.ExampleLet's see the code.#include using namespace std; bool isPrime(char c) {    return c == '2' || c ...

Read More

Largest number with the given set of N digits that is divisible by 2, 3 and 5 in C++

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 277 Views

In this tutorial, we are going to write a program that finds the largest number formed from the array that is divisible by 2, 3, and 5.Let's see the steps to solve the problem.Initialise the array.The number must end with 0 and the sum of all the numbers should be divisible by 3 to be divisible by 2, 3, and 5.Check for the 0 in the array and print not possible if it's not present in the array.Sort the array in descending order.Find the remainder for sum % 3.If the remainder is not 1, then delete all the digits from ...

Read More

Largest permutation after at most k swaps in C++

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 690 Views

In this tutorial, we are going to write a program that finds the largest permutation after at most k swaps.Let's see the steps to solve the problem.Initialise the array.Initialise an array to store the index with size n + 1.Iterate over the array and store the index of each element in the position array.Write a loop that iterate till n and k is greater than 0.Store the position of n - i element in a temp variable.Update position of current element arr[i] with position[n - i].Update the position position[n - i] with current index i.Swap the current element arr[i] with ...

Read More

Largest set with bitwise OR equal to n in C++

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 134 Views

In this tutorial, we are going to write a program that finds the largest set with bitwise OR is equal to the given number n.Let's see the steps to solve the problem.Initialise the number n.Write a loop that iterates from 0 to n.If the i | n is equal to n, then add i to the result.Return the result.ExampleLet's see the code.#include using namespace std; void printBitWiseOrSet(int n) {    vector v;    for (int i = 0; i

Read More

Largest subarray having sum greater than k in C++

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 438 Views

In this tutorial, we are going to write a program that finds the largest subarray have sum greater than k.Let's see the steps to solve the problem.Initialise the array.Iterate over the array and store sum at each index in a vector along with the index.Sort the above sums based on sum and index.Initialise an array to store the indexes.Write a loop that iterates till n.Update the values with min index of above indexes array and previous sums array index.Initialise sum to 0.Write a loop that iterates till n.Add current element to sum.If the sum is greater than k.The maximum subarray ...

Read More

Largest subarray with equal number of 0s and 1s in C++

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 241 Views

Let's see the steps to complete the program.Initialise the array.Make all zeroes in the array to -1.Have a map an empty map to store the previous indexes.Initialise sum to 0, max length to 0 and ending index to -1.Write a loop that iterates till n.Add current element to sum.If the sum is equal to 0.Update the max length with i + 1.And ending index to i.If the sum is present in previous sums map and i - previousIndexes[sum] is greater than max length.Update the max length and ending index.Else add the sum to the previous indexes map.Print the starting index ...

Read More

Largest sum subarray with at-least k numbers in C++

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 442 Views

Let's see the steps to complete the program.Initialise the array.Initialise max_sum array of size n.Find the max sum for every index and store it in max_sum array.Compute the sum of all the elements and store it in a variable sum.Write a loop that iterates from i = k to n.Add a[i] - a[i - k] to the sum.Update the result with max of result, sum.Update the result with max of result, sum + max_sum[i - k].ExampleLet's see the code.#include using namespace std; int getMaxSum(int a[], int n, int k) {    int maxSum[n];    maxSum[0] = a[0];    int currentMax ...

Read More

Deletion in a Binary Tree in C++ Program

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 6K+ Views

In this tutorial, we are going to learn how to delete a node in a binary tree.The nodes in a binary tree don't follow any order like binary search trees. So, how to arrange the nodes after deleting a node in a binary tree?Well, we will replace the deepest node of the tree with the deleting node. And then we will delete the deepest node from the node.Let's see the steps to solve the problem.Initialize the tree with binary node struct.Write a function (preorder, in order, and postorder) to print the nodes of the tree.Write a function to delete the ...

Read More

Deletions of “01” or “10” in binary string to make it free from “01” or “10” in C++ Program

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 291 Views

In this tutorial, we are going to write a program that finds the total number of pairs (01 or 10) to free the binary string from the pairs (01 and 10). Let's see an example.Input − 101010001Output − 4In the above example, we have to delete a total of 4 pairs to free the binary string from the pairs (01 and 10).The resultant string after deleting all the pairs is 0.We have to delete all the 01 and 10 pairs from the binary string. So, the total number of pairs to be deleted is the minimum of count(1) and count(0).Let's ...

Read More
Showing 31–40 of 259 articles
« Prev 1 2 3 4 5 6 26 Next »
Advertisements