Server Side Programming Articles

Page 1138 of 2109

Double the first element and move zero to end in C++ Program

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

In this tutorial, we are going to write a program that doubles the first element and moves all the zeroes to the end of the given array.We have to double a number when there are tow the same elements in adjacent indices. After that, we have to add a zero to the array.Move all the zeroes in the array to the end.ExampleLet's see the code.#include using namespace std; void moveZeroesToEnd(int arr[], int n) {    int count = 0;    for (int i = 0; i < n; i++) {       if (arr[i] != 0) {   ...

Read More

Double Tree with examples in C++ Program

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

In this tutorial, we are going to learn how to double the given tree.Let's see the steps to solve the problem.Create node class.Initialize the tree with dummy data.Write a recursive function to double the tree.Recursively traverse through the tree.Store the left node in a variable.After traversing add the data by creating a new node.Now, add the left node to the newly created node as a left child.Print the tree.ExampleLet's see the code.#include using namespace std; class node {    public:    int data;    node* left;    node* right; }; node* newNode(int data) {    node* Node = new ...

Read More

Create a new string by alternately combining the characters of two halves of the string in reverse in C++ Program

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

In this tutorial, we are going to write a program that creates a new string by alternately combining the characters of the two halves of the string in reverse order.Let's see the steps to solve the problem.Initialize the string.Find the length of the string.Store the first half and second half string indexes.Iterate from the ending of the two halves of the string.Add each character to the new string.Print the new string.ExampleLet's see the code.#include using namespace std; void getANewString(string str) {    int str_length = str.length();    int first_half_index = str_length / 2, second_half_index = str_length;    string new_string ...

Read More

Count of different ways to express N as the sum of 1, 3 and 4 in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 11-Mar-2026 890 Views

Given a positive number N as input. The goal is to find the number of ways in which we can express N as a sum of 1s, 3s and 4s only. For example, if N is 4 then it can be represented as 1+1+1+1, 3+1, 1+3, 4 so the number of ways will be 4.Let us understand with examples.For ExampleInput -  N=5Output - Count of different ways to express N as the sum of 1, 3 and 4 are: 6Explanation -  5 can be represented as:1+1+1+1+11+3+13+1+11+1+34+11+4Input - N=6Output - Count of different ways to express N as the sum of ...

Read More

Final string after performing given operations in C++

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

In this tutorial, we are going to solve the following problem.Given a string containing only characters a and b, our task is to delete the sub-string ab from the string. And print the remaining string.Here, the idea is very simple to solve the problem. Every string with only a's and b's will shrink to either a's or b's at the end.Let's see the steps to solve the problem.Initialize the string.Initialize two counter variables for a and b.Iterate over the given string.Count the a's and b'sFind the maximum from the a and b frequencies.Print the difference between the two.ExampleLet's see the ...

Read More

Find (a^b)%m where 'a' is very large in C++

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

In this tutorial, we are going to solve the equation (ab)%m where a is a very large number.The equation (ab)%m=(a%m)*(a%m)...b_times. We can solve the problem by finding the value of a%m and then multiplying it b times.Let's see the steps to solve the problem.Initialize the numbers a, b, and m.Write a function to find the a%m.Initialize the number with 0.Iterate over the number in string format.Add the last digits to the number.Update the number with number modulo them.Get the value of a%m.Write a loop that iterates b times.Multiply the a%m and modulo the result with m.Print the result.ExampleLet's see the ...

Read More

Find A and B from list of divisors in C++

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

In this tutorial, we are going to solve the below problem.Given an array of integers, we have to find two numbers A and B. All the remaining numbers in the array are the divisors of A and B.If a number is a divisor of both A and B, then it will present twice in the array.Let's see the steps to solve the problem.The max number in the array is one of the numbers from A and B. Let's say it is A.Now, B will be the second-largest number or the number which is not a divisor of A.ExampleLet's see the ...

Read More

Find 2^(2^A) % B in C++

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

In this tutorial, we are going to write a program to evaluate the equation 2^(2^A) % B.We are going to find the value of the equation using a recursive function. Let's see the steps to solve the problem.Write a recursive function that takes 2 arguments A and B.If A is 1, then return 4 % B as 2^(2^1) % B = 4 % B.Else recursively call the function with A-1 and b.Return the result^2%B.Print the solutionExampleLet's see the code.#include using namespace std; long long solveTheEquation(long long A, long long B) {    // 2^(2^1) % B = 4 % ...

Read More

Find (1^n + 2^n + 3^n + 4^n) mod 5 in C++

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

In this tutorial, we are going to solve the following problem.Given an integer n, we have to find the (1n+2n+3n+4n)%5The number (1n+2n+3n+4n) will be very large if n is large. It can't be fit in the long integers as well. So, we need to find an alternate solution.If you solve the equation for the number 1, 2, 3, 4, 5, 6, 7, 8, 9 you will get 10, 30, 100, 354, 1300, 4890, 18700, 72354, 282340 values respectively.Observe the results of the equation carefully. You will find that the last digit of the equation result repeats for every 4th number. ...

Read More

Find a number that divides maximum array elements in C++

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

In this tutorial, we are going to find the number that is divided into maximum elements in the given array.Let's see the steps to solve the problem.Initialize the array and a variable to store the result.Iterate over the array.Initialize the counter variable.Iterate over the array again.Increment the counter if the current element is divisible by the array element.Update the result if the current count is maximum.Print the result.ExampleLet's see the code.#include using namespace std; int numberWithMaximumMultiples(int arr[], int n) {    int result = -1;    for (int i = 0; i < n; i++) {       ...

Read More
Showing 11371–11380 of 21,090 articles
Advertisements