C++ Articles

Page 45 of 597

Euler's Four Square Identity in C++

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

In this problem, we are given two numbers and we need to find the product of the numbers using Euler's four square Identity. Euler’s Four Square Identity is the method to find the product of two numbers which can be represented using the sum of four squares of the number if the numbers can be represented as sum of four squares.The product a * b can be represented as the sum of four squares ifa = x12 + x22 + x32 + x42 b = y12 + y22 + y32 + y42 a * b = z12 + z22 + z32 + z42Let’s take an example ...

Read More

Count pairs in an array that hold i*arr[i] > j*arr[j] in C++

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

We are given an array of numbers. The goal is to find the pair of elements of array such that they hold the conditionIf (i*arr[i] > j*arr[j]) then (arr[i], arr[j]) is a valid pair.If the array is [ 5, 4, 3, 2, 1 ] then pairs will be [3, 1] and [2, 1].Let us understand with examples.Input − arr[] = [ 1, 5, 4, 1, 2, 8, 3 ]Output − Count of pairs in an array that hold i*arr[i] > j*arr[j] are − 3Explanation − Pairs are (5, 1), (4, 1), (8, 3)Input − arr[] = [ -1, -2, 3, ...

Read More

Count of sub-strings of length n possible from the given string in C++

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

We are given a string str[] and a number n. The goal is to find all substrings of str[] that have length n. If string is “abcde” and n=3 then substrings of length 3 are “abc”, “bcd”, “cde” and count is 3.Let us understand with examples.Input − str[] = “computer” n=4Output − Count of substrings of length n possible from the given string are − 5Explanation − Substrings with length 4 are: “comp”, “ompu”, ”mput”, “pute”, “uter”Input − str[] = “development” n=5Output − Count of substrings of length n possible from the given string are − 7Explanation − Substrings with ...

Read More

Program to create largest lexicographic number from a list of numbers in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 381 Views

Suppose we have a list of numbers called nums, we have to rearrange its order to form the largest possible number and return that as a string.So, if the input is like nums = [20, 8, 85, 316], then the output will be "88531620".To solve this, we will follow these steps −Define an array tempfor each item i in nums:insert i into temp as stringsort the array temp based on lexicographic sequence (check for two strings a, b when a concatenate b is larger than b concatenate a or not)for each string s in temp:res := res concatenate sreturn resLet ...

Read More

Maximum Subarray Sum Excluding Certain Elements in C++ program

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

In this problem, we are given two arrays arr1[] of size n and arr2[] of size m. Our task is to create a program to find the maximum subarray sum excluding certain elements.Problem Description − We need to find the maximum subarray sum from elements of the array arr1[] that are not present in arr2[].Let’s take an example to understand the problem, Inputarr1[] = {4, 5, 7, 2, 9}, arr2[] = {1, 9, 2, 7}Output9Explanationarr1 after removal of elements of arr2[] = {4, 5} Both can form a subarray, hence sum = 4+5 = 9.Solution ApproachA simple solution to the ...

Read More

Maximum subsequence sum such that no three are consecutive in C++ Program

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

In this problem, we are given an array arr[] consisting of n positive integers. Our task is to create a program to find the maximum subsequence sum such that no three are consecutive.Problem Description − Here, we need to find the sum of sequences created from the array such that there are no three consecutive elements.Consecutive elements of an array are those elements that have followed the same order of index.arr[0], arr[1], arr[2], …Let’s take an example to understand the problem, Inputarr[] = {5, 9, 12, 15}Output32ExplanationSum = 5 + 12 + 15 = 32Solution ApproachA simple solution to the ...

Read More

Maximum sum of smallest and second smallest in an array in C++ Program

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

In this problem, we are given an array arr[]. Our task is to create a program to find the maximum sum of smallest and second smallest in an array.Problem Description − We need to find the sum of the smallest and second smallest elements of a sum subarray of the array. And return the maximum of all such subarray sums.ExampleLet’s take an example to understand the problem, Inputarr[] = {3, 5, 4, 2, 9, 1, 6}Output11ExplanationTheir out of all subarrays possible, {2, 9} has a maximum sum of smallest elements. Sum = 2 + 9 = 11Solution ApproachA simple solution ...

Read More

Maximum Sum of Products of Two Array in C++ Program

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

In this problem, we are given two arrays arr1[] and arr2[] of size n. Our task is to create a program to find the maximum Sum of Products of Two Array.Problem Description − We need to find the maximum sum of products of two arrays. We need to find the maximum sum of the product of one element from arr1 and other elements from arr2.Let’s take an example to understand the problem, Inputarr1[] = {3, 5, 6} arr2[] = {1, 4, 2}Output37ExplanationMaximum sum of products: 6*4 + 5*2 + 3*1 = 24 + 10 + 3 = 37Solution ApproachA simple ...

Read More

Maximum sum subarray such that start and end values are same in C++ Program

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

In this problem, we are given an array arr[] of size n consisting of positive values. Our task is to create a program to find the maximum sum subarray such that start and end values are the same.Problem Description − Here, we need to find a subarray such that the elements at index i (starting index of subarray) and j (ending index of subarray) are the same i.e. arr[i] = arr[j]. And the sum of elements of the subarray is maximized.Let’s take an example to understand the problem, Inputarr[] = {2, 1, 3, 5, 6, 2, 4, 3}Output23ExplanationAll subarrays which ...

Read More

Maximum sum subsequence with at-least k distant elements in C++ program

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

In this problem, we are given an array arr[] of size n and a number k. Our task is to create a program to find the maximum sum subsequence with atleast k distant elements.Problem Description − We need to find the sum of subarrays such that the elements of the subarray are taken from the array whose index is at k distance and the sum is maximized.Let’s take an example to understand the problem, Inputarr[] = {2, 3, 7, 9, 2, 8, 3}Output15ExplanationAll subsequences that satisfy the given condition, {2, 9, 3}, Sum = 14 {3, 2}, Sum = 5 ...

Read More
Showing 441–450 of 5,962 articles
« Prev 1 43 44 45 46 47 597 Next »
Advertisements