Count Fibonacci Numbers in Given Range in O(log n) Time and O(1) Space in C++

Sunidhi Bansal
Updated on 15-May-2020 09:00:55

618 Views

We are given the range having start and end numbers and the task is to calculate the total count of Fibonacci numbers available between the given range in O(Log n) time and O(1) space.What are Fibonacci numbersFibonacci numbers are the sequence of numbers known as Fibonacci sequence where every new number is the sum of the last two preceding numbers.Where, f(0) = 0 and f(1) = 1 i.e. f(0) and f(1) have fixed positions in the sequence and the calculation will start from the third number.Formula used for calculating the sequence is −Fn = Fn-1 + Fn-2Where, F0 = 0, ... Read More

Count by Multiple Fields with MongoDB Aggregation

AmitDiwan
Updated on 15-May-2020 08:59:09

825 Views

To count by multiple fields, use $facet in MongoDB. The $facet processes multiple aggregation pipelines within a single stage on the same set of input documents. Let us create a collection with documents −> db.demo721.insertOne( ...    { ... ...       "details1": { ...          "id":101 ... ...       }, ...       "details2": { ...          "id":101 ...       }, ...       "details3": { ...          "id":101 ...       } ...    } ... ); {    "acknowledged" : ... Read More

Update All Values of a Field with a Specific String in MongoDB

AmitDiwan
Updated on 15-May-2020 08:55:19

268 Views

To update all the values, use update() along with multi:true. Let us create a collection with documents −> db.demo720.insertOne({"SubjectName":"MySQL"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eaae7ca43417811278f5883") } > db.demo720.insertOne({"SubjectName":"Java"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eaae7ce43417811278f5884") } > db.demo720.insertOne({"SubjectName":"C"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eaae7d143417811278f5885") } > db.demo720.insertOne({"SubjectName":"C++"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eaae7d543417811278f5886") }Display all documents from a collection with the help of find() method −> db.demo720.find();This will produce the following output −{ "_id" : ObjectId("5eaae7ca43417811278f5883"), "SubjectName" : "MySQL" } { "_id" : ObjectId("5eaae7ce43417811278f5884"), "SubjectName" : "Java" } { ... Read More

Add New Field and Concatenate Price in MongoDB Query

AmitDiwan
Updated on 15-May-2020 08:52:31

428 Views

To add a new field, use the $addFields in MongoDB. Let us create a collection with documents −> db.demo719.insertOne( ...    { ...       "Number":"7374644", ...       "details" : { ...          "otherDetails" : [ ...             { ...                "ProductId" :"102", ...                "ProductPrice" : NumberInt(500) ...             }, ...             { ...                "ProductId" :"103", ...     ... Read More

Count Digits in a Factorial in C++

Sunidhi Bansal
Updated on 15-May-2020 08:51:41

2K+ Views

We are given an integer value and the task is to first calculate the factorial of a number and then calculate the total number of digits in a result.What is a factorial numberFactorial of a number is calculated by multiplying the digits in a number while decrementing the value of digit by 1. It is denoted by the symbol ‘!’ i.e. 0!, 1!, 2!, 3!, 5!, ...., etc. Factorial of 0! and 1! is always 1.I.e. factorial of 2 = 2 * (2-1) = 2 * 1 = 2       factorial of 3 = 3 * (3-1) * ... Read More

Count Factorial Numbers in a Given Range in C++

Sunidhi Bansal
Updated on 15-May-2020 08:49:16

366 Views

We are given the range starting from an integer value holded by a variable let’s say start till the variable end and the task is to count the total number of factorial numbers available in the given range.What is a factorial numberFactorial of a number is calculated by multiplying the digits in a number while decrementing thevalue of digit by 1. It is denoted by the symbol ‘!’ i.e. 0!, 1!, 2!, 3!, 5!, ...., etc. Factorial of 0! and 1! is always 1.I.e. factorial of 2 = 2 * (2-1) = 2 * 1 = 2       ... Read More

Count Even and Odd Digits in an Integer in C++

Sunidhi Bansal
Updated on 15-May-2020 08:45:56

3K+ Views

We are given with an integer number and the task is to count the even numbers and the odd numbers in a digit. Also, we will keep check on whether the even digits in an integer are occurring an even number of times and also the odd digits in an integer are occurring an odd number of times.For ExampleInput − digit = 12345 Output − count for even digits = 2       count for odd digits = 3Explanation − Yes, Also, even digits are occurring even number of times i.e. 2 and odd digits are occurring odd number ... Read More

Count Common Characters in Two Strings in C++

Sunidhi Bansal
Updated on 15-May-2020 08:41:27

2K+ Views

We are given with the two strings let’s say str1 and str2 and the task is to find the count of common characters in two strings i.e. if str1[i] = str[j], then they will be considered as a pair and count will increased to 1 and if str1[i]!=str2[j] then they willn’t be considered as a pair and count willn’t increase to 1.For ExampleInput − str1 = “hello”       str2 = “heoo” Output − count is: 3Explanation − str1[0] = str2[0] i.e. h ; str1[1] = str2[1] i.e. e ; str1[2]!=str2[2] i.e. l and o; str1[3]=str2[3] i.e. o. So, the ... Read More

Convert Characters of a String to Opposite Case in C++

Sunidhi Bansal
Updated on 15-May-2020 08:30:17

592 Views

We are given a string of any length and the task is to convert the string having uppercase letters to lowercase letters and lowercase letters to uppercase letters.For ExampleInput − string str = ”Welcome To The Site!”Output − wELCOME tO tHE sITE!Explanation − converted the letters W, T, T, S to lowercase and letters e, l, c, o, m, e, o, , i, t, e to uppercase and it doesn’t perform any operations to the special characters.Input − string str = ”HELLO”Output − helloExplanation − converted the letters H, E, L, L, E to lowercase.This can be done using two different approachesUsing inbuilt functions provided by ... Read More

Convert Distance from KM to Meters and Centimeters in PL/SQL

Sunidhi Bansal
Updated on 15-May-2020 08:27:43

658 Views

The task is to convert the distance from kilometers to meters and centimeters in PL/SQL.PL/SQL is the extension of SQL which combines the Data manipulation of SQL with the working of procedural language.According to the problem we should have distance in kilometers whose value we have to convert in meters and centimeters.As per the conversion rule −1km = 1000 meters1km = 100000 centimetersAccording to this conversion rule we want the distance to be converted by a logic in PL/SQL.ExampleInput: kilometer = 10 Output: meter = 10000    Centimeter = 1000000 Input: kilometer = 9 Output: meter = 9000    Centimeter ... Read More

Advertisements