Found 7347 Articles for C++

Find a partition point in array in C++

Hafeezul Kareem
Updated on 01-Feb-2021 11:59:38

350 Views

In this tutorial, we are going to find the partition point in an array where all the elements left to the partition point are small and all the elements right to the partition point are large.Let's see the steps to solve the problem.Initialize the array.Iterate over the array.Iterate from 0 to I and check each value whether it is smaller than the current value or not.Iterate from I to n and check each value whether it is larger than the current value or not.If the bot the conditions satisfied, then return the value.Print the partition point.ExampleLet's see the code. Live Demo#include ... Read More

Find a pair with given sum in BST in C++

Hafeezul Kareem
Updated on 01-Feb-2021 11:58:31

136 Views

In this tutorial, we are going to write a program that finds the pair whose sum is equal to the given number in the binary search tree.We are going to store and values of trees in two different lists to find the pairs. Let's see the steps to solve the problem.Create a struct node for a binary tree.Write a function to insert a new node into the binary search tree.Remember, in the binary search tree all the elements that are less than root are smaller, and right are larger.Initialize two empty lists to store the left and right nodes of ... Read More

Find a Number X whose sum with its digits is equal to N in C++

Hafeezul Kareem
Updated on 01-Feb-2021 11:58:04

141 Views

In this tutorial, we are going to find a number whose some including with its digits is equal to the given number N.The idea is simple, we are going to check the left and right 100 numbers of the given number. It won't lie out that bound as N ≤ 1000000000 and the sum won't exceed 100.Let's see the steps to solve the problem.Initialize the number.Write a loop that iterates 100 times.Get the n - i and n + i values.Find the sum of the digits and add them.If anyone of them is equal to the N, print them.ExampleLet's see ... Read More

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

Hafeezul Kareem
Updated on 01-Feb-2021 11:57:48

123 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. Live Demo#include using namespace std; int numberWithMaximumMultiples(int arr[], int n) {    int result = -1;    for (int i = 0; i < n; i++) {     ... Read More

Find a Fixed Point (Value equal to index) in a given array in C++ Program

Hafeezul Kareem
Updated on 01-Feb-2021 11:48:14

199 Views

In this tutorial, we are going to solve the following problem.Given an array, find the number which is equal to the index. It's a straightforward problem.Iterate over the given array and return the index which is equal to the array element.ExampleLet's see the code. Live Demo#include using namespace std; int linearSearch(int arr[], int n) {    for(int i = 0; i < n; i++) {       if(arr[i] == i) {          return i;       }    }    return -1; } int main() {    int arr[] = {10, 20, 30, 40, 50, 5, 60};    cout

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

Hafeezul Kareem
Updated on 01-Feb-2021 11:47:43

208 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 2^(2^A) % B in C++

Hafeezul Kareem
Updated on 01-Feb-2021 11:45:39

76 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. Live Demo#include using namespace std; long long solveTheEquation(long long A, long long B) {    // 2^(2^1) % B = 4 ... Read More

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

Hafeezul Kareem
Updated on 01-Feb-2021 11:42:59

98 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 (a^b)%m where ‘a’ is very large in C++

Hafeezul Kareem
Updated on 01-Feb-2021 11:42:43

144 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

Final string after performing given operations in C++

Hafeezul Kareem
Updated on 01-Feb-2021 11:42:25

148 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

Advertisements