diff --git a/04- April/26- Add Digits/26- Add Digits (Noura Algohary).cpp b/04- April/26- Add Digits/26- Add Digits (Noura Algohary).cpp new file mode 100644 index 000000000..b646fb405 --- /dev/null +++ b/04- April/26- Add Digits/26- Add Digits (Noura Algohary).cpp @@ -0,0 +1,26 @@ +// Author: Noura Algohary +// beats 100% in runtime + +class Solution { +public: + int recursiveAddition(string num) + { + // the ending contidion is reaching a one digit number + if(num.size() == 1) + return (num[0] - '0'); + + // to store the summation of digits of num + int sumInt = 0; + + for(int i=num.size() - 1; i>=0; i--) + { + // convert the digit num[i] from char to int, then sum it + sumInt += num[i] - '0'; + } + + return recursiveAddition(to_string(sumInt)); + } + int addDigits(int num) { + return recursiveAddition(to_string(num)); + } +}; \ No newline at end of file diff --git a/04- April/26- Add Digits/26- Add Digits (Noura Algohary).py b/04- April/26- Add Digits/26- Add Digits (Noura Algohary).py new file mode 100644 index 000000000..ed964d65b --- /dev/null +++ b/04- April/26- Add Digits/26- Add Digits (Noura Algohary).py @@ -0,0 +1,19 @@ +# Author: Noura Algohary + +class Solution: + def addDigits(self, num: int) -> int: + + def recursiveAddition(num): + # the ending contidion is reaching a one digit number + if len(num) == 1: + return int(num) + + # to store the summation of digits of num + sumInt = 0 + + for i in range(len(num) - 1, -1, -1): + sumInt += int(num[i]) + + return recursiveAddition(str(sumInt)) + + return recursiveAddition(str(num)) \ No newline at end of file diff --git a/04- April/26- Add Digits/26- Add Digits (RotenKiwi).cpp b/04- April/26- Add Digits/26- Add Digits (RotenKiwi).cpp new file mode 100644 index 000000000..ebf0748cb --- /dev/null +++ b/04- April/26- Add Digits/26- Add Digits (RotenKiwi).cpp @@ -0,0 +1,13 @@ +// Author: RotenKiwi +class Solution { +public: + int addDigits(int num) { + if(num<10)return num; // If number is less than 10, retrn it. + int curr=0; // Variable to store the digit sum. + while(num){ // Calculation of digit sum. + curr=curr+(num%10); + num=num/10; + } + return addDigits(curr); // Recurse the same for the new number + } +}; diff --git a/04- April/26- Add Digits/26- Add Digits (Zeinab Mohy).cpp b/04- April/26- Add Digits/26- Add Digits (Zeinab Mohy).cpp new file mode 100644 index 000000000..2f0f22c17 --- /dev/null +++ b/04- April/26- Add Digits/26- Add Digits (Zeinab Mohy).cpp @@ -0,0 +1,31 @@ +// Author: Zeinab Mohy + +class Solution { +public: + int addDigits(int num) { + //convert num into string to apply my idea + string s=to_string(num); + //if the length of string is greater than 1 then .. + if(s.length()>1){ + while(s.length()>1){ + /*initialize the summation of the digits with 0 + then sum the digits in string after convert all into integer + then convert summatiomn into string again and so on + until the length of string equal 1*/ + int sum=0; + string ss; + for(int i=0;i

@@ -1499,4 +1500,36 @@ public: } }; ``` + +
+

+ +## 27) [Bulb Switcher](https://leetcode.com/problems/bulb-switcher/) + +### Difficulty + +![](https://img.shields.io/badge/Medium-orange?style=for-the-badge) + +### Related Topic + +`Math` `Brainteaser` + +### Code + + +```cpp +class Solution { +public: + int bulbSwitch(int n) { + // for this problem we need to know how many light bulbs are on after n rounds + // to know that you need to know for each light bulb to stay on at the end it needs to have odd number of divisors + // the only numbers that have odd number of divisors are perfect squares + // so the answer is the number of perfect squares less than or equal to n + // which is sqrt(n) + + + return sqrt(n); + } +}; +``` \ No newline at end of file