Skip to content

Commit

Permalink
Merge branch '7oSkaaa:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
AhmedGamal2212 committed Apr 27, 2023
2 parents 6cf74a0 + 8b713a2 commit db7dda3
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 0 deletions.
26 changes: 26 additions & 0 deletions 04- April/26- Add Digits/26- Add Digits (Noura Algohary).cpp
Original file line number Diff line number Diff line change
@@ -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));
}
};
19 changes: 19 additions & 0 deletions 04- April/26- Add Digits/26- Add Digits (Noura Algohary).py
Original file line number Diff line number Diff line change
@@ -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))
13 changes: 13 additions & 0 deletions 04- April/26- Add Digits/26- Add Digits (RotenKiwi).cpp
Original file line number Diff line number Diff line change
@@ -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
}
};
31 changes: 31 additions & 0 deletions 04- April/26- Add Digits/26- Add Digits (Zeinab Mohy).cpp
Original file line number Diff line number Diff line change
@@ -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<s.length();i++){
ss=s[i];
int x=stoi(ss);
sum+=x;
}
s=to_string(sum);
}
// after that convert string into integer to return the result
int ans=stoi(s);
return ans;
}else{
return num;
}
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Author: Mahmoud Aboelsoud

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);
}
};
33 changes: 33 additions & 0 deletions 04- April/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
1. **[Last Stone Weight](#24--last-stone-weight)**
1. **[Smallest Number in Infinite Set](#25--smallest-number-in-infinite-set)**
1. **[Add Digits](#26--add-digits)**
1. **[Bulb Switcher](#27--bulb-switcher)**

<hr>
<br><br>
Expand Down Expand Up @@ -1499,4 +1500,36 @@ public:
}
};
```
<hr>
<br><br>
## 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);
}
};
```

0 comments on commit db7dda3

Please sign in to comment.