Found 7347 Articles for C++

Find subarray with given sum - (Handles Negative Numbers) in C++

sudhir sharma
Updated on 25-Jan-2022 13:06:14

429 Views

In this problem, we are given an array arr[] consisting of N integers stored in unsorted order. Our task is to find a subarray with a given sum.Let's take an example to understand the problem, Input : arr[] = {2, 5, -1, 4, 6, -9, 5} sum = 14 Output : subarray = {5, -1, 4, 6}Explanation −Subarray sum = 5 - 1 + 4 + 6 = 14Solution ApproachA simple solution to the problem is using nested loops. We will loop through the array and using an inner loop, we will find subarray. For each subarray we will find ... Read More

Find subarray with given sum - (Nonnegative Numbers) in C++

sudhir sharma
Updated on 25-Jan-2022 12:52:30

316 Views

In this problem, we are given an array arr[] consisting of N positive integers stored in unsorted order. Our task is to find a subarray with a given sum.Let's take an example to understand the problem, Input : arr[] = {2, 5, 1, 4, 6, 9, 5} sum = 11 Output : subarray = {1, 4, 6}Explanation −Subarray sum = 1 + 4 + 6 = 11Solution ApproachA simple solution to the problem is using nested loops. We will loop through the array and using an inner loop, we will find subarray. For each subarray we will find the sum ... Read More

Find sub-string with given power in C++

sudhir sharma
Updated on 25-Jan-2022 12:06:06

177 Views

In this problem, we are given a string str and an integer pow. Our task is to find a sub-string with given power.We need to return the substring whose power is equal to pow.Power of string is the sum of powers of its characters.Power of character : a -> 1, b -> 2, c -> 3, ...Let's take an example to understand the problem, Input : string = "programming" power = 49 Output : 'pro'Explanation −Power of matrix : pro, power(p) = 16 power(p) = 18 power(p) = 15 Total = 16 + 18 + 15 = 49Solution ApproachA simple ... Read More

Find sub-matrix with the given sum in C++

sudhir sharma
Updated on 25-Jan-2022 11:16:54

1K+ Views

In this problem, we are given a 2D matrix of size N*N and two variables sum and size. Our task is to find a sub-matrix with the given sum.We need to find a sub-matrix of size*size with element sum equal to sum.Let's take an example to understand the problem, Input : mat[][] = {    {1, 5, 7, 9}    {2, 4, 6, 8}    {1, 2, 5, 6}    {3, 6, 9, 3} } sum = 22 Size = 2 Output : YESExplanation −The submatrix of size k with sum 22 is {5, 7} {4, 6}Solution ApproachA simple solution ... Read More

Find start and ending index of an element in an unsorted array in C++

sudhir sharma
Updated on 25-Jan-2022 08:48:09

794 Views

In this problem, we are given an array aar[] of n integer values which are not sorted and an integer val. Our task is to find the start and ending index of an element in an unsorted array.For the occurrence of the element in the array, we will return, "Starting index and ending index " if it is found in the array twice or more."Single index " if it is found in the array once."Element not present " if it is not present in the array.Let's take an example to understand the problem, Example 1Input : arr[] = {2, 1, ... Read More

Find Square Root under Modulo p (Shanks Tonelli algorithm) in C++

sudhir sharma
Updated on 25-Jan-2022 08:28:01

401 Views

In this problem, we are given two values n and a prime number p. Our task is to find Square Root under Modulo p.Let's take an example to understand the problem, Input : n = 4, p = 11 Output : 9Solution ApproachHere, we will be using Tonelli-Shanks Algorithm.Tonelli-Shanks Algorithm is used in modular arithmetic to solve for a value x in congruence of the form x2 = n (mod p).The algorithm to find square root modulo using shank's Tonelli Algorithm −Step 1 − Find the value of $(n^{((p-1)/2)})(mod\:p)$, if its value is p -1, then modular square root is ... Read More

Find Square Root under Modulo p (When p is in form of 4*i + 3) in C++

sudhir sharma
Updated on 25-Jan-2022 07:46:35

73 Views

In this problem, we are given two values n and a prime number p. Our task is to find Square Root under Modulo p (When p is in form of 4*i + 3). Here, p is of the form (4*i + 3) i.e. p % 4 = 3 for i > 1 and p being a prime number.Here are some numbers, 7, 11, 19, 23, 31...Let's take an example to understand the problem, Input : n = 3, p = 7 Output :Solution ApproachA simple solution to the problem is using a loop. We will loop from 2 to (p ... Read More

Find the number of primitive roots modulo prime in C++.

sudhir sharma
Updated on 24-Jan-2022 13:34:13

519 Views

In this problem, we are given a prime number N. Our task is to find the number of primitive roots modulo prime.Primitive Root of a number − It is a number (r) smaller than N which has all values of r^x(mod N) different for all X in range [0, n-2].Let’s take an example to understand the problem, Input : N = 5 Output : 2Solution ApproachA simple solution to the problem is based on a trial method. We will check for all numbers from 2 to (N-1) for the conditions with x ranging from [0, n-2] and break if a value ... Read More

Find the number of points that have atleast 1 point above, below, left or right of it in C++

sudhir sharma
Updated on 24-Jan-2022 13:24:34

64 Views

In this problem, we are given N points that lie in a 2D plane. Our task is to find the number of points that have at least 1 point above, below, left or right of it.We need to count all the points that have at least 1 point which satisfies any of the below conditions.Point above it − The point will have the same X coordinate and the Y coordinate is one more than its current value.Point below it − The point will have the same X coordinate and the Y coordinate is one less than its current value.Point left ... Read More

Find the number of operations required to make all array elements Equal in C++

sudhir sharma
Updated on 24-Jan-2022 13:18:46

287 Views

In this problem, we are given an array arr of size n. Our task is to Find the number of operations required to make all array elements EqualThe operation is defined as distribution of equal weights from the element with maximum weight to all the elements of the array.If it is not possible to make array elements equal, print -1. Let’s take an example to understand the problem,  Input : arr[] = {7, 3, 3, 3} Output : 3ExplanationArray after distribution is {4, 4, 4, 4}Solution ApproachA simple solution to the problem is by finding the largest value of the array. ... Read More

Advertisements