Server Side Programming Articles - Page 2151 of 2650

Minimum LCM and GCD possible among all possible sub-arrays in C++

Arnab Chakraborty
Updated on 21-Oct-2019 07:22:14

283 Views

Suppose we have an array arr of size N. it has N positive numbers. We have to find the minimum elements of all possible subarray. Suppose the array is {2, 66, 14, 521}, then minimum LCM is 2, and GCD is 1.We will solve this problem using a greedy approach. If we decrease the number of elements, then LCM will be less, and if we increase the array size, GCD will be less. We need to find the smallest element from the array, which is a single element, which will be required LCM. For GCD, GCD will be GCD of ... Read More

Minimize Cost with Replacement with other allowed in C++

Arnab Chakraborty
Updated on 21-Oct-2019 07:19:38

234 Views

Suppose we have an array with N elements. We have to remove element from the array by following the given operations. The operation is like, we have to choose any two numbers of the array, and remove larger. Cost including in this operation is the same as the smaller number. We have to delete only one element at a time, based on this operation, and perform the task in minimum cost. Suppose the array has {4, 2, 5}. I take 4 and 2, remove 4 by paying cost 2, then we remove 5 again with cost 2.The approach is too ... Read More

Meta Strings (Check if two strings can become same after a swap in one string) in C++

Arnab Chakraborty
Updated on 21-Oct-2019 07:17:11

696 Views

In this section, we will see how to check whether two strings are meta string or not. The meta strings are those strings that are very much similar. If we swap two elements in one string, then it will be matched with other string. Suppose two strings are “HELLO” and “OELLH”, then they are meta strings.To check whether two strings are meta string or not, we have to follow these steps.Steps −If both strings are of different length, then return falseElse find a number of characters, that have not matched, also store the index of non-matched charactersIf the count is ... Read More

Maximum Subarray Sum using Divide and Conquer algorithm in C++

Arnab Chakraborty
Updated on 21-Oct-2019 07:14:08

1K+ Views

Suppose we have one list of data with positive and negative values. We have to find the sum of contiguous subarray whose sum is largest. Suppose the list is containing {-2, -5, 6, -2, -3, 1, 5, -6}, then the sum of maximum subarray is 7. It is the sum of {6, -2, -3, 1, 5}We will solve this problem by using the Divide and Conquer method. The steps will look like below −Steps −Divide the array into two partsFind the maximum of the following threeMaximum subarray sum of left subarrayMaximum subarray sum of right subarrayMaximum subarray sum such that subarray ... Read More

Maximum positive integer divisible by C and is in the range [A, B] in C++

Arnab Chakraborty
Updated on 21-Oct-2019 07:10:26

165 Views

Here we will see one interesting problem. let us consider we have three integers A, B, and C. We have to find one minimum integer X, such that X mod C = 0, and X is not in the range [A, B]. If the values of A, B and C are 5, 10 and 4 respectively, then the value of X will be 4. We have to follow these steps to get the solution −Steps −If C is not in the range [A, B], then return C as a resultOtherwise get the first multiple of C, which is greater than ... Read More

Maximum length subarray with LCM equal to product in C++

Arnab Chakraborty
Updated on 21-Oct-2019 07:07:31

376 Views

Suppose we have an array A. We have to find the maximum length of the subarray, whose LCM is the same as the product of the elements of that subarray. If that kind of subarray is not found, then return -1. Suppose array is {6, 10, 21}, then the length is 2, as the subarray {10, 21} is there, whose LCM is 210, and the product is also 210.The approach is straight forward. We have to check every possible subarray of length greater or equals to 2. If the subarray is satisfying the condition, then update the answer as a ... Read More

Maximum GCD of N integers with given product in C++

Arnab Chakraborty
Updated on 21-Oct-2019 07:03:26

202 Views

Suppose we two integers N and P. The P is the product of N unknown integers. We have to find the maximum possible GCD of those integers. Suppose N = 3, and P = 24, then different groups will be like {1, 1, 24}, {1, 2, 12}, {1, 3, 8}, {1, 4, 6}, {2, 2, 6}, {2, 3, 4}. The GCDs are: 1, 1, 1, 1, 2, 1. So answer is 2 here.We will find all prime factors of P, and store them into hashmap. The N integers will have max GCD when the prime factor will be common in ... Read More

C Program to convert first character uppercase in a sentence

Sunidhi Bansal
Updated on 18-Oct-2019 14:22:35

4K+ Views

Given a string and with mixed case, i.e with both uppercase and lower case, the task is to covert the first character to uppercase rest in lowercase if it’s in upper case.Let’s understand it in depth with the help of a simple example.Like we are given a string “hElLo world”, we have to convert the first character ‘h’ which is in lowercase to uppercase ‘H’ and rest all letters before the space or end of the string to lowercase.Moreover when we encounter first character after a space we have to convert it to uppercase.ExampleInput: str[] = {“heLlO wORLD”} Output: Hello ... Read More

C Program to check if a date is valid or not

Sunidhi Bansal
Updated on 18-Oct-2019 14:06:27

3K+ Views

Given date in format date, month and year in integer. The task is to find whether the date is possible on not.Valid date should range from 1/1/1800 – 31/12/9999 the dates beyond these are invalid.These dates would not only contains range of year but also all the constraints related to a calendar date.Constraints are −Date can’t be less than 1 and more than 31Month can’t be less than 1 and more than 12Year can’t be less than 1800 and more than 9999When the months are April, June, September, November the date can’t be more than 30.When the month is February ... Read More

C Program to check Strong Number

Sunidhi Bansal
Updated on 18-Oct-2019 13:25:15

7K+ Views

Given a number ‘n’ we have to check whether the number given is Strong Number or not.Strong number is a number whose sum of all digits’ factorial is equal to the number ‘n’. Factorial implies when we find the product of all the numbers below that number including that number and is denoted by ! (Exclamation sign), For example: 4! = 4x3x2x1 = 24.So, to find a number whether its strong number, we have to pick every digit of the number like the number is 145 then we have to pick 1, 4 and 5 now we will find factorial ... Read More

Advertisements