question_slug
stringlengths
3
77
title
stringlengths
1
183
slug
stringlengths
12
45
summary
stringlengths
1
160
โŒ€
author
stringlengths
2
30
certification
stringclasses
2 values
created_at
stringdate
2013-10-25 17:32:12
2025-04-12 09:38:24
updated_at
stringdate
2013-10-25 17:32:12
2025-04-12 09:38:24
hit_count
int64
0
10.6M
has_video
bool
2 classes
content
stringlengths
4
576k
upvotes
int64
0
11.5k
downvotes
int64
0
358
tags
stringlengths
2
193
comments
int64
0
2.56k
concatenation-of-consecutive-binary-numbers
Concatenation of Consecutive Binary Numbers: Python, One-liner
concatenation-of-consecutive-binary-numb-jwg0
You can use bin(integer)[2:] or format(integer, \'b\') or f\'{integer:b}\' to get binary string. Then convert it back to int from binary by int(string, 2).\n\nc
hollywood
NORMAL
2021-01-27T09:20:19.045900+00:00
2021-01-27T09:39:28.513842+00:00
206
false
You can use `bin(integer)[2:]` or `format(integer, \'b\')` or `f\'{integer:b}\'` to get binary string. Then convert it back to `int` from `binary` by `int(string, 2)`.\n```\nclass Solution:\n def concatenatedBinary(self, n: int) -> int:\n return int(\'\'.join([format(x, \'b\') for x in range(1, n + 1)]), 2) % (10**9 + 7)\n```
3
1
['Python']
1
concatenation-of-consecutive-binary-numbers
Rust one-liner solution
rust-one-liner-solution-by-sugyan-efyp
rust\nconst DIV: u64 = 1_000_000_007;\n\nimpl Solution {\n pub fn concatenated_binary(n: i32) -> i32 {\n (2..=n).fold(1_u64, |acc, x| {\n (
sugyan
NORMAL
2021-01-27T08:49:29.568752+00:00
2021-01-27T08:49:29.568795+00:00
96
false
```rust\nconst DIV: u64 = 1_000_000_007;\n\nimpl Solution {\n pub fn concatenated_binary(n: i32) -> i32 {\n (2..=n).fold(1_u64, |acc, x| {\n ((acc << (32 - x.leading_zeros())) + x as u64) % DIV\n }) as i32\n }\n}\n```
3
0
['Rust']
0
concatenation-of-consecutive-binary-numbers
C++ O(NlogN) simple solution
c-onlogn-simple-solution-by-varkey98-xi1v
\n #define mod 1000000007\nint concatenatedBinary(int n) \n{\n\tlong ret=0,p=1;\n\tfor(int i=n;i>=1;--i)\n\t{\n\t\tint temp=i;\n\t\twhile(temp)\n\t\t{\n\t\t\
varkey98
NORMAL
2020-12-09T04:40:54.093454+00:00
2020-12-14T02:38:41.462924+00:00
375
false
```\n #define mod 1000000007\nint concatenatedBinary(int n) \n{\n\tlong ret=0,p=1;\n\tfor(int i=n;i>=1;--i)\n\t{\n\t\tint temp=i;\n\t\twhile(temp)\n\t\t{\n\t\t\tret+=(temp&1)*p;\n\t\t\ttemp/=2;\n\t\t\tp*=2; \n\t\t\tp%=mod;\n\t\t\tret%=mod;\n\t\t}\n\t}\n\treturn ret;\n}\n\t```
3
1
['C']
2
concatenation-of-consecutive-binary-numbers
[Go] O(n) with explanation
go-on-with-explanation-by-cwc123-21k3
The rule described means every time left-shift, then add the number\n\n\nnumber = 1, string = 1\nnumber = 2, string left shift 2 bits = 100, adds number 2 becom
cwc123
NORMAL
2020-12-06T05:41:19.756073+00:00
2021-01-28T01:24:54.959938+00:00
135
false
The rule described means every time left-shift, then add the number\n\n```\nnumber = 1, string = 1\nnumber = 2, string left shift 2 bits = 100, adds number 2 becomes 110\nnumber = 3, string left shift 2 bits = 11000, adds number 3 beomces 11011\n...\n```\n\nThe technique is to use bit-wise operation to find number of digits for the number, then do left-sift and add number. Also, add number mean bit-wise operation `or`.\n\nupdate at 1/28, original code find digits is not efficient, change to new one\n\n```golang\nfunc concatenatedBinary(n int) int {\n\tnum, digits := 1, 1\n\tvar ans int\n\tmod := int(1e9 + 7)\n\n\tfor ; num <= n; num++ {\n if num == 1 << digits {\n\t\t\tdigits++\n\t\t}\n\n\t\tans = ans << digits\n\t\tans |= num\n\n\t\tans = ans % mod\n\t}\n\n\treturn ans\n}\n```\n\noriginal\n\n```golang\nfunc concatenatedBinary(n int) int {\n\tmod := int64(1e9 + 7)\n\tvar ans int64\n\n\tfor i := 1; i <= n; i++ {\n\t\tsize := digits(i)\n\t\tans = ((ans << size) | int64(i)) % mod\n\t}\n\n\treturn int(ans)\n}\n\nfunc digits(i int) int {\n\tvar shift int\n\n\tfor shift = 31; (1<<shift)&i == 0; shift-- {\n\t}\n\n\treturn shift + 1\n}\n```
3
0
['Go']
1
concatenation-of-consecutive-binary-numbers
Simple Java Solution 4 Lines of Code:
simple-java-solution-4-lines-of-code-by-2v4ro
\nclass Solution {\n public int concatenatedBinary(int n) {\n long val=0, i=0;\n while(i++<n)\n val= ((val<<(1+(int)(Math.log(i)/Mat
cuda_coder
NORMAL
2022-09-23T17:49:08.456692+00:00
2022-09-23T17:49:08.456737+00:00
55
false
```\nclass Solution {\n public int concatenatedBinary(int n) {\n long val=0, i=0;\n while(i++<n)\n val= ((val<<(1+(int)(Math.log(i)/Math.log(2))))+i)%1000000007;\n return (int)val;\n }\n}\n```
2
1
['Bit Manipulation']
2
concatenation-of-consecutive-binary-numbers
Super Easy C++ One Liner Clean code
super-easy-c-one-liner-clean-code-by-you-jc9e
\nclass Solution {\npublic:\n int concatenatedBinary(int n) {\n long long int ans=0;\n int i=1;\n while(i<=n){\n ans=((ans<<(
you_r_mine
NORMAL
2022-09-23T14:53:26.987594+00:00
2022-09-23T14:53:26.987631+00:00
37
false
```\nclass Solution {\npublic:\n int concatenatedBinary(int n) {\n long long int ans=0;\n int i=1;\n while(i<=n){\n ans=((ans<<(1+int(log2(i))))%1000000007+i)%1000000007;\n i++;\n }\n return ans;\n }\n};\n```
2
0
[]
0
concatenation-of-consecutive-binary-numbers
Python/JS/Go/C++ O(n) by bit operation [w/Comment]
pythonjsgoc-on-by-bit-operation-wcomment-yanx
Python/JS/Go/C++ O(n) by bit operation \n\n---\n\nDemonstration with n = 3\n\n1 = 0b 1\n2 = 0b 10\n3 = 0b 11\n\nConcatenation from 1 to 3 in binary = 0b 1 10 11
brianchiang_tw
NORMAL
2022-09-23T11:35:46.875852+00:00
2022-09-23T11:35:46.875890+00:00
151
false
Python/JS/Go/C++ O(n) by bit operation \n\n---\n\nDemonstration with n = 3\n\n1 = 0b 1\n2 = 0b 10\n3 = 0b 11\n\nConcatenation from 1 to 3 in binary = 0b 1 10 11 = 0b 11011 = 27 in decimal\n\n---\n\n**Implementation**:\n\nPython:\n\n```\nclass Solution:\n def concatenatedBinary(self, n: int) -> int:\n\n constant = 10**9 + 7\n summation = 0\n bit_width = 0\n \n # iterate from 1 to n\n for i in range(1, n+1):\n \n # update binary bit width when we meet power of 2\n if i & i-1 == 0:\n bit_width += 1\n \n # use binary left rotation to implement concatenation\n summation = summation << bit_width\n \n # add with current number\n summation = summation | i\n \n # mod with constant defined by description\n summation %= constant\n \n return summation\n```\n\n---\n\nJavascript\n\n```\nvar concatenatedBinary = function(n) {\n \n const constant = 10**9 + 7;\n let summation = 0;\n let bit_width = 0;\n\n // iterate from 1 to n\n for( let i = 1 ; i <= n ; i++ ){\n\n // update binary bit width when we meet power of 2\n if( 0 == (i & i-1) ){\n bit_width += 1;\n }\n\n\n // use binary left rotation to implement concatenation\n summation = summation * (2 ** bit_width);\n \n // add with current number\n summation = summation + i;\n\n // mod with constant defined by description\n summation = summation % constant;\n }\n\n return summation;\n \n};\n```\n\n---\n\nGo:\n\n```\nfunc concatenatedBinary(n int) int {\n \n const constant = int(1e9+7)\n summation := 0\n bit_width := 0\n\n // iterate from 1 to n\n for i := 1 ; i <= n ; i++ {\n\n // update binary bit width when we meet power of 2\n if 0 == (i & (i-1) ){\n bit_width += 1;\n }\n\n\n // use binary left rotation to implement concatenation\n summation = summation << bit_width;\n \n // add with current number\n summation = summation | i;\n\n // mod with constant defined by description\n summation = summation % constant;\n }\n\n return summation;\n}\n```\n\n---\n\nC++\n\n```\nclass Solution {\npublic:\n int concatenatedBinary(int n) {\n\n int constant = 1e9 + 7;\n long summation = 0;\n int bit_width = 0;\n \n // iterate from 1 to n\n for( int i = 1 ; i <= n ; i++ ){\n \n // update binary bit width when we meet power of 2\n if( 0 == (i & i-1) ){\n bit_width += 1;\n }\n \n \n // use binary left rotation to implement concatenation\n summation = summation << bit_width;\n \n // add with current number\n summation = summation | i;\n \n // mod with constant defined by description\n summation %= constant;\n }\n \n return int(summation);\n \n \n }\n};\n```\n\n
2
0
['Bit Manipulation', 'C', 'Python', 'Go', 'JavaScript']
1
concatenation-of-consecutive-binary-numbers
Python Elegant & Short | One line | Reducing
python-elegant-short-one-line-reducing-b-guc4
\nclass Solution:\n """\n Time: O(n*log(n))\n Memory: O(1)\n """\n\n MOD = 10 ** 9 + 7\n\n def concatenatedBinary(self, n: int) -> int:\n
Kyrylo-Ktl
NORMAL
2022-09-23T08:00:42.237052+00:00
2022-09-30T13:16:00.463005+00:00
228
false
```\nclass Solution:\n """\n Time: O(n*log(n))\n Memory: O(1)\n """\n\n MOD = 10 ** 9 + 7\n\n def concatenatedBinary(self, n: int) -> int:\n return reduce(lambda x, y: ((x << y.bit_length()) | y) % self.MOD, range(1, n + 1))\n```\n\nIf you like this solution remember to **upvote it** to let me know.
2
0
['Python', 'Python3']
0
concatenation-of-consecutive-binary-numbers
Very Simple Code Bit Manu. || C++
very-simple-code-bit-manu-c-by-omhardaha-g6ha
\nclass Solution\n{\npublic:\n int mod = 1e9 + 7;\n\n int concatenatedBinary(int n)\n {\n long long ans = 0;\n int i = 1;\n\n whil
omhardaha1
NORMAL
2022-09-23T07:19:19.770756+00:00
2022-09-23T07:19:19.770794+00:00
30
false
```\nclass Solution\n{\npublic:\n int mod = 1e9 + 7;\n\n int concatenatedBinary(int n)\n {\n long long ans = 0;\n int i = 1;\n\n while (i <= n)\n {\n int leftShift = log2(i) + 1;\n ans <<= leftShift;\n ans = (ans | i++) % mod;\n }\n\n return (ans % mod);\n }\n};\n```
2
0
['C']
0
concatenation-of-consecutive-binary-numbers
Simple Solution || Python || Bit Manipulation
simple-solution-python-bit-manipulation-fbp27
\nclass Solution:\n def concatenatedBinary(self, n: int) -> int:\n ans = \'\'\n for i in range(1, n + 1):\n ans += str(bin(i)[2:])\n
T5691
NORMAL
2022-09-23T07:10:43.432216+00:00
2022-09-23T07:10:43.432255+00:00
76
false
```\nclass Solution:\n def concatenatedBinary(self, n: int) -> int:\n ans = \'\'\n for i in range(1, n + 1):\n ans += str(bin(i)[2:])\n \n return int(ans, 2) % ((10**9) + 7)\n```
2
0
['Bit Manipulation', 'Python']
0
concatenation-of-consecutive-binary-numbers
Java || Bit Manipulation
java-bit-manipulation-by-parmeshk07-6bs9
```\n\nclass Solution {\n public int concatenatedBinary(int n) {\n long ans = 0;\n int bit_count = 0;\n for(int i=1; i<=n; i++){\n
Parmeshk07
NORMAL
2022-09-23T06:57:45.493659+00:00
2022-09-23T06:57:45.493697+00:00
154
false
```\n\nclass Solution {\n public int concatenatedBinary(int n) {\n long ans = 0;\n int bit_count = 0;\n for(int i=1; i<=n; i++){\n if((i&(i-1)) == 0)\n bit_count++;\n \n ans = ((ans << bit_count) | i)%1000000007;\n }\n return (int)ans;\n }\n}\n\n\n
2
0
['Bit Manipulation', 'Java']
1
concatenation-of-consecutive-binary-numbers
Java || 100% fast Solution
java-100-fast-solution-by-spirited-coder-hu4l
\nclass Solution {\n public int concatenatedBinary(int n) {\n final long mod = (long)(1e9 + 7);\n long result = 0;\n int size = 0;\n
Spirited-Coder
NORMAL
2022-09-23T06:15:46.436435+00:00
2022-09-23T06:15:46.436476+00:00
235
false
```\nclass Solution {\n public int concatenatedBinary(int n) {\n final long mod = (long)(1e9 + 7);\n long result = 0;\n int size = 0;\n for(int i = 1; i <= n; i++)\n {\n if((i & (i-1)) == 0)\n {\n size++;\n }\n result = ((result << size) | i)%mod;\n }\n \n return (int)result;\n }\n}\n```
2
0
['Bit Manipulation', 'Java']
3
concatenation-of-consecutive-binary-numbers
Java solution | one liner
java-solution-one-liner-by-rahultherock9-7syr
1 Liner using Long.range and reduce\n\n\npublic int concatenatedBinary(int n) {\n\t\treturn (int) LongStream.range(1, n + 1).reduce(0, (sum, i) -> (sum * (int)
rahultherock955
NORMAL
2022-09-23T05:02:06.997254+00:00
2022-09-23T05:02:06.997296+00:00
23
false
1 Liner using Long.range and reduce\n\n```\npublic int concatenatedBinary(int n) {\n\t\treturn (int) LongStream.range(1, n + 1).reduce(0, (sum, i) -> (sum * (int) Math.pow(2, Long.toBinaryString(i).length()) + i) % 1_000_000_007);\n }\n```
2
0
[]
0
concatenation-of-consecutive-binary-numbers
Concise and easy js solution with explanation O(N)
concise-and-easy-js-solution-with-explan-ccjm
Main idea: What concatenating actually does is, to shift the origin values to the left and then plus the new values.\n\nFor example: 2 concat 3 = 10 concat 11 =
wai_tat
NORMAL
2022-09-23T04:38:37.806671+00:00
2022-09-23T05:44:35.081653+00:00
252
false
Main idea: What concatenating actually does is, to shift the origin values to the left and then plus the new values.\n\nFor example: 2 concat 3 = 10 concat 11 => 10 shift 2 bits then add 11 = 1011.\n\nAlso: \n1. Shifting n bits is equal to mulitply by 2 ** n. \n2. For i that 2 ** (n-1) <= i < 2 ** n we shift the same number of bits n.\n\nSo now the coding part becomes simple:\n\n```\nvar concatenatedBinary = function(n) {\n let mod = 10 ** 9 + 7;\n let mul = 2;\n \n let ans = 1;\n for(let i = 2; i <= n; i++){\n if(i === mul) mul *= 2;\n ans = (ans * mul + i) % mod;\n }\n \n return ans;\n};\n```
2
0
['JavaScript']
2
concatenation-of-consecutive-binary-numbers
โœ…โœ” SIMPLE PYTHON3 SOLUTION โœ…โœ” O)n log n ) time
simple-python3-solution-on-log-n-time-by-84vz
UPVOTE if it is helpfull\n\n\nclass Solution:\n def countBits(self,x):\n ab = 0\n while x:\n x >>= 1\n ab += 1\n r
rajukommula
NORMAL
2022-09-23T03:12:00.521382+00:00
2022-09-23T03:12:00.521420+00:00
222
false
***UPVOTE*** if it is helpfull\n```\n\nclass Solution:\n def countBits(self,x):\n ab = 0\n while x:\n x >>= 1\n ab += 1\n return ab\n def concatenatedBinary(self, n: int) -> int:\n res = 0\n mod = 10**9 + 7\n for i in range(1, n+1):\n res = ((res << self.countBits(i)) + i) % mod\n return res\n```
2
0
['Python', 'Python3']
2
concatenation-of-consecutive-binary-numbers
C++ | Solution | Easy to understand
c-solution-easy-to-understand-by-yash112-3800
\t#Please upvote, It it is helpful :)\n\tclass Solution {\n\tpublic:\n\t\tint concatenatedBinary(int n) {\n\t\t\tlong long ans = 0;\n\t\t\tint M = 1e9+7;\n\t\t\
yash112002
NORMAL
2022-09-23T02:17:30.625493+00:00
2022-09-23T02:17:30.625531+00:00
164
false
\t#Please upvote, It it is helpful :)\n\tclass Solution {\n\tpublic:\n\t\tint concatenatedBinary(int n) {\n\t\t\tlong long ans = 0;\n\t\t\tint M = 1e9+7;\n\t\t\tstring num = "";\n\t\t\t// concatinating the numbers\n\t\t\tfor(int i = n; i >= 1; i--)\n\t\t\t{\n\t\t\t\tint t = i;\n\t\t\t\twhile(t)\n\t\t\t\t{\n\t\t\t\t\tnum += t%2 + \'0\';\n\t\t\t\t\tt /= 2;\n\t\t\t\t}\n\t\t\t}\n\t\t\t// evaluating num\n\t\t\tint pow = 1;\n\t\t\tfor(int i = 0; i < num.size(); i++)\n\t\t\t{\n\t\t\t\tlong long t = (num[i]-\'0\')*pow;\n\t\t\t\tpow *= 2;\n\t\t\t\tpow %= M;\n\t\t\t\tans += t;\n\t\t\t\tans %= M;\n\t\t\t}\n\t\t\treturn ans%M;\n\t\t}\n\t};
2
0
['C', 'Iterator', 'C++']
0
concatenation-of-consecutive-binary-numbers
JAVA โœ… Easy and short solution
java-easy-and-short-solution-by-jahanwee-hmuj
\nclass Solution {\n public int concatenatedBinary(int n) {\n long res = 0;\n int m = 1000000007;\n for(int i=1;i<=n;i++){\n S
JAHANWEE
NORMAL
2022-09-23T01:47:25.206635+00:00
2022-09-23T20:31:52.400947+00:00
168
false
```\nclass Solution {\n public int concatenatedBinary(int n) {\n long res = 0;\n int m = 1000000007;\n for(int i=1;i<=n;i++){\n String s = Integer.toBinaryString(i);\n res = (res << s.length() ) %m;\n res = (res+i)%m;\n }\n return (int)res;\n }\n}\n```
2
0
['Java']
2
concatenation-of-consecutive-binary-numbers
DAILY LEETCODE SOLUTION || EASY C++ SOLUTION
daily-leetcode-solution-easy-c-solution-ukmnz
\nclass Solution {\npublic:\n long long int mod=1e9+7;\n int concatenatedBinary(int n) {\n long long int ans=0;\n for(long long int i=1;i<=n
pankaj_777
NORMAL
2022-09-23T00:23:27.170562+00:00
2022-09-23T00:23:27.170597+00:00
112
false
```\nclass Solution {\npublic:\n long long int mod=1e9+7;\n int concatenatedBinary(int n) {\n long long int ans=0;\n for(long long int i=1;i<=n;i++)\n {\n ans=(ans<<(long long int)(log2(i)+1))%mod;\n ans=(ans+i)%mod;\n }\n return ans;\n }\n};\n```
2
0
['Math', 'Bit Manipulation', 'C']
0
concatenation-of-consecutive-binary-numbers
Python | O(n) | Pure bit-manipulation without strings | Explanation
python-on-pure-bit-manipulation-without-0xmv8
Concatenation in binary is analagous to shifting left the overall result by the amount of bits necessary to store the next number to concatenate and then summin
roncato
NORMAL
2022-04-12T21:16:48.671608+00:00
2022-04-14T02:52:25.254404+00:00
127
false
Concatenation in binary is analagous to shifting left the overall result by the amount of bits necessary to store the next number to concatenate and then summing the value. For example:\n```\nresult= 1 = 0b1\nnext_number = 2 = 0b10\n\nresult << 2 => result = 0b100\nresult = result + next_number = 0b100 + 0b10 = 0b110\n```\n\n```python\nclass Solution:\n def concatenatedBinary(self, n: int) -> int:\n result = 0\n MOD = (10**9 + 7)\n L = 0\n for num in range(1, n + 1):\n # Reached a new length. e.g. 1, 10, 100, 1000, etc...\n # We could also compute the length for every number as math.floor(math.log2(num) + 1)\n # As per https://en.wikipedia.org/wiki/Bit-length\n if (num & (num - 1)) == 0:\n L += 1\n result = ((result << L) + num) % MOD\n return result\n```
2
0
['Bit Manipulation']
1
concatenation-of-consecutive-binary-numbers
C++ | faster than 99.37%
c-faster-than-9937-by-bananymousosq-5x7l
\nclass Solution {\npublic:\n\tint concatenatedBinary(int n) {\n\t\tint64_t ans = 0;\n\t\tfor (int i = 1; i <= n; i++) {\n\t\t\tint bits = 32 - __builtin_clz(i)
bananymousosq
NORMAL
2021-05-16T12:05:40.657118+00:00
2021-05-16T12:05:40.657151+00:00
164
false
```\nclass Solution {\npublic:\n\tint concatenatedBinary(int n) {\n\t\tint64_t ans = 0;\n\t\tfor (int i = 1; i <= n; i++) {\n\t\t\tint bits = 32 - __builtin_clz(i);\n\t\t\tans = (ans << bits | i) % 1\'000\'000\'007;\n\t\t}\n\t\treturn ans;\n\t}\n};\n```
2
0
[]
0
concatenation-of-consecutive-binary-numbers
C++ Easy and Simple Bit Manipulation Solution
c-easy-and-simple-bit-manipulation-solut-ewrx
\nclass Solution {\npublic:\n \n int concatenatedBinary(int n) {\n long long int result=1;\n int total_bits=0;\n for(int i=2;i<=n;i++
talrejalavina
NORMAL
2021-01-27T20:29:43.838189+00:00
2021-01-27T20:29:43.838225+00:00
181
false
```\nclass Solution {\npublic:\n \n int concatenatedBinary(int n) {\n long long int result=1;\n int total_bits=0;\n for(int i=2;i<=n;i++)\n {\n total_bits = (int)log2(i)+1;\n result= (result<< total_bits) +i;\n if(result>INT_MAX)\n result= result%1000000007;\n \n }\n return result%1000000007;\n }\n};\n```
2
0
['Bit Manipulation', 'C', 'C++']
0
concatenation-of-consecutive-binary-numbers
C++ O(N) Fast and Simple Solution
c-on-fast-and-simple-solution-by-vsfjyaj-io9h
\nclass Solution {\npublic:\n \n int concatenatedBinary(int n) {\n long long int result=1;\n int total_bits=0;\n for(int i=2;i<=n;i++
vSfJyajp
NORMAL
2021-01-27T20:26:40.450516+00:00
2021-01-27T20:26:40.450563+00:00
185
false
```\nclass Solution {\npublic:\n \n int concatenatedBinary(int n) {\n long long int result=1;\n int total_bits=0;\n for(int i=2;i<=n;i++)\n {\n total_bits = (int)log2(i)+1;\n result= (result<< total_bits) +i;\n if(result>INT_MAX)\n result= result%1000000007;\n \n }\n return result%1000000007;\n }\n};\n```
2
0
['Bit Manipulation', 'C', 'C++']
0
concatenation-of-consecutive-binary-numbers
[CPP] [Recursion] Easy to Understand with explanation..
cpp-recursion-easy-to-understand-with-ex-qakw
We will be using recursion to solve this question. Assume that we have an answer till n-1 returned by the recursive function. We store this in the temp variable
harmxnkhurana
NORMAL
2021-01-27T18:27:47.309500+00:00
2021-01-27T18:28:36.649025+00:00
88
false
We will be using recursion to solve this question. Assume that we have an answer till `n-1` returned by the recursive function. We store this in the `temp` variable. \n\nNow, from this `temp`, we need to calculate the final answer for the given `n`.\n\n* Shift the answer returned for `n-1` (stored in `temp`) by the number of bits to represent `n`.\n* Add `n` to that.\n* Return (with mod `1000000007`)\n\n```\n\nclass Solution {\npublic:\n int mod = 1000000007;\n unsigned int helper(unsigned int n) \n { \n unsigned int count = 0; \n while (n) \n { \n count++; \n n >>= 1; \n } \n return count; \n } \n \n int concatenatedBinary(int n) {\n if (n == 1){\n return 1;\n }\n \n long temp = concatenatedBinary(n-1);\n \n return ((temp<<helper(n)) + n)%mod;\n }\n};\n```
2
0
[]
2
concatenation-of-consecutive-binary-numbers
[Python] Simple Solution - Casting of Types using built-ins
python-simple-solution-casting-of-types-6orkl
Approach: Convert int to binary (string to join) and then back to int.\n\n\nclass Solution:\n def concatenatedBinary(self, n: int) -> int:\n binaryStr
vikktour
NORMAL
2021-01-27T15:43:15.083814+00:00
2021-01-27T15:44:05.559818+00:00
235
false
Approach: Convert int to binary (string to join) and then back to int.\n\n```\nclass Solution:\n def concatenatedBinary(self, n: int) -> int:\n binaryString = "" #our result in form of a string\n for i in range(1,n+1):\n #cast integer i into binary, and then to string, and remove the "0b" component in the front\n binaryString += str(bin(i))[2:]\n #cast from binary to integer\n decimal = int(binaryString, 2)\n\n return decimal % 1000000007\n```
2
0
['Python', 'Python3']
1
concatenation-of-consecutive-binary-numbers
Python straightforward
python-straightforward-by-leovam-y8xj
This is straightforward but non-optimal solution. Luckily it can pass when I did for the daily chanllenge. \nAfter reading the LC solution, I realilze this migh
leovam
NORMAL
2021-01-27T14:52:53.151935+00:00
2021-01-27T14:53:03.941995+00:00
208
false
This is straightforward but non-optimal solution. Luckily it can pass when I did for the daily chanllenge. \nAfter reading the LC solution, I realilze this might be one advantange of Python, and the Math solution might be what the problem wants to test ...\n```\nclass Solution:\n def concatenatedBinary(self, n: int) -> int:\n res = \'\'\n \n for i in range(1, n+1):\n res += bin(i)[2:]\n \n \n return int(res,2) % (10**9 + 7)\n```
2
0
['Python']
0
concatenation-of-consecutive-binary-numbers
[java] solution
java-solution-by-manishkumarsah-zg54
\nclass Solution {\n public int concatenatedBinary(int n) {\n \n int mod = 1000000007;\n int num = 0;\n \n for(int i=1;i<=
manishkumarsah
NORMAL
2021-01-27T13:34:11.913949+00:00
2021-01-27T13:34:11.913976+00:00
71
false
```\nclass Solution {\n public int concatenatedBinary(int n) {\n \n int mod = 1000000007;\n int num = 0;\n \n for(int i=1;i<=n;i++)\n {\n String binaryRep = Integer.toBinaryString(i);\n \n for(char ch:binaryRep.toCharArray())\n {\n int val = (ch==\'0\')?0:1;\n \n num = ((num*2)%mod + val)%mod;\n }\n }\n return num;\n }\n}\n```
2
0
[]
0
concatenation-of-consecutive-binary-numbers
Python: using bin() and int() functions
python-using-bin-and-int-functions-by-ps-3xtj
\n\nclass Solution:\n def concatenatedBinary(self, n: int) -> int:\n stp = ""\n for i in range(0, n+1):\n stp +=bin(i)[2:]\n
PSC_Crack
NORMAL
2021-01-27T09:46:54.910923+00:00
2021-01-27T16:53:00.274539+00:00
105
false
\n```\nclass Solution:\n def concatenatedBinary(self, n: int) -> int:\n stp = ""\n for i in range(0, n+1):\n stp +=bin(i)[2:]\n return int(stp, 2) % (10**9 + 7)\n```\n\nUpdated Code :\n```\nclass Solution:\n def concatenatedBinary(self, n: int) -> int:\n stp = []\n for i in range(0, n+1):\n stp.append(bin(i)[2:])\n stp = "".join(stp)\n return int(stp, 2) % (10**9 + 7)\n```
2
0
[]
1
concatenation-of-consecutive-binary-numbers
C++||Easy to understand included comments in O(n)
ceasy-to-understand-included-comments-in-9a49
\nclass Solution {\npublic:\n int concatenatedBinary(int n) {\n long long int M = 1000000007;//modulo operator\n long long int d1=0,res;\n
nishi_04
NORMAL
2021-01-27T08:38:54.537628+00:00
2021-01-27T08:39:50.720512+00:00
294
false
```\nclass Solution {\npublic:\n int concatenatedBinary(int n) {\n long long int M = 1000000007;//modulo operator\n long long int d1=0,res;\n for(long long int i=1;i<=n;i++){\n d1=((d1<<(int(log2(i))+1))%M+i)%M;//shifting the bits to left side \n \n \n }\n \n \n return d1;\n }\n};\n```
2
0
['C', 'C++']
0
concatenation-of-consecutive-binary-numbers
Simple Recursive 1 line C++ & Python solution
simple-recursive-1-line-c-python-solutio-gj68
The idea is to use a simple formula:\na(n) = a(n-1) * 2^(1 + floor(log2(n))) + n\nC++: \n\nclass Solution {\npublic:\n const int mod = 1e9 + 7;\n int conc
sainikhilreddy
NORMAL
2020-12-06T16:24:58.574190+00:00
2020-12-06T16:25:44.034737+00:00
160
false
The idea is to use a simple formula:\n**a(n) = a(n-1) * 2^(1 + floor(log2(n))) + n**\n**C++:** \n```\nclass Solution {\npublic:\n const int mod = 1e9 + 7;\n int concatenatedBinary(int n) {\n return (n == 1) ? 1 : (concatenatedBinary(n - 1) * (long)(pow(2, 1 + (int)log2(n))) + n) % mod;\n }\n};\n```\n**PS:** The declaration line of mod can be removed and it can be directly placed in the return statement of the function.\n\n**Python:**\n```\nclass Solution:\n def concatenatedBinary(self, n: int) -> int:\n return 1 if n == 1 else (self.concatenatedBinary(n - 1) * int(math.pow(2, 1 + int(math.log2(n)))) + n) % (10 ** 9 + 7)\n```
2
0
['C', 'Python']
0
concatenation-of-consecutive-binary-numbers
Python - 4 line solution (Easy)
python-4-line-solution-easy-by-afrozchak-c3gt
\nclass Solution:\n def concatenatedBinary(self, n: int) -> int:\n result = ""\n for i in range(1, n+1):\n result = result + bin(i)[
afrozchakure
NORMAL
2020-12-06T13:07:56.908892+00:00
2020-12-06T13:07:56.908916+00:00
165
false
```\nclass Solution:\n def concatenatedBinary(self, n: int) -> int:\n result = ""\n for i in range(1, n+1):\n result = result + bin(i)[2:]\n return int(result , 2) % (10**9 + 7)\n```
2
0
['Python']
1
concatenation-of-consecutive-binary-numbers
Easy Solution with Explanation | c++
easy-solution-with-explanation-c-by-ajay-mthc
let\'s take an example \nn = 3\n\nIntially ans = 1;\n\n\nfor i --> 2\n\tnumber of bits in 2 is 2;\n\tshift answer by number of bits i.e. 2 ans add i\n\tans = 1
ajayking9627
NORMAL
2020-12-06T11:14:36.576304+00:00
2020-12-06T11:21:50.779618+00:00
103
false
let\'s take an example \n**n = 3**\n\n**Intially ans = 1;**\n\n\n**for i --> 2**\n\tnumber of bits in 2 is 2;\n\tshift answer by number of bits i.e. 2 ans add i\n\tans = 1 << 2 + 2\n\t\t= 6\n\n**for i --> 3**\n\tnumber of bits in 3 is 2;\n\tshift answer by number of bits i.e. 2 ans add i\n\tans = 6 << 2 + 3\n\t\t= 27\n\n**so, answer is 27.**\n```\nclass Solution {\npublic:\n int concatenatedBinary(int n) {\n long long ans = 1;\n int mod = 1e9 + 7;\n for(int i = 2; i <= n; i++) {\n int bits = (int)log2(i)+1; // count number of bits in i\n ans = ((ans << bits) % mod + i) % mod; // left shift current ans by number of bits in i and current element to ans.\n }\n return (int)ans;\n }\n};\n```
2
0
[]
1
concatenation-of-consecutive-binary-numbers
[C++] Simple | O(nlogn) approach
c-simple-onlogn-approach-by-avishubht762-vxdo
\nclass Solution {\npublic:\n long long int concatenatedBinary(int n) \n {\n long long mod=1e9+7;\n long long sum=0,mul=1;\n for(int
avishubht762
NORMAL
2020-12-06T08:31:17.676537+00:00
2020-12-06T08:31:17.676581+00:00
96
false
```\nclass Solution {\npublic:\n long long int concatenatedBinary(int n) \n {\n long long mod=1e9+7;\n long long sum=0,mul=1;\n for(int i=n;i>0;i--)\n {\n int k=i;\n while(k>0)\n {\n sum=sum+(mul*(k%2))%mod;\n k=k/2;\n mul=(mul*2)%mod;\n }\n }\n return sum%mod;\n }\n};\n```
2
0
['C']
0
concatenation-of-consecutive-binary-numbers
Python 1-liner
python-1-liner-by-lokeshsk1-6rh8
\nclass Solution:\n def concatenatedBinary(self, n: int) -> int:\n ans=\'\'\n for i in range(n+1):\n ans+=bin(i)[2:]\n return
lokeshsk1
NORMAL
2020-12-06T05:38:10.595328+00:00
2020-12-06T16:07:47.315157+00:00
165
false
```\nclass Solution:\n def concatenatedBinary(self, n: int) -> int:\n ans=\'\'\n for i in range(n+1):\n ans+=bin(i)[2:]\n return int(ans,2)%(10**9+7)\n```\n\n``` \nclass Solution:\n def concatenatedBinary(self, n: int) -> int:\n return int(\'\'.join([bin(i)[2:] for i in range(n+1)]),2)%(10**9+7) \n```
2
1
['Python', 'Python3']
0
concatenation-of-consecutive-binary-numbers
"Python" using bin() and int() function
python-using-bin-and-int-function-by-har-782v
Easy to understand and using bin() and int() functions\nSimple python3 solution\n\nclass Solution:\n def concatenatedBinary(self, n: int) -> int:\n b,
harish_sahu
NORMAL
2020-12-06T04:08:15.690750+00:00
2020-12-06T04:08:15.690801+00:00
138
false
**Easy to understand and using bin() and int() functions**\n*Simple python3 solution*\n```\nclass Solution:\n def concatenatedBinary(self, n: int) -> int:\n b, m = [], (10 ** 9) + 7\n for i in range(1, n + 1):\n s = bin(i)\n b.append(s[2:])\n s = \'\'.join(b)\n return int(s, 2) % m\n```
2
0
['Python', 'Python3']
0
concatenation-of-consecutive-binary-numbers
[JAVA] Clean O(n) Solution
java-clean-on-solution-by-ziddi_coder-ewc5
\tclass Solution {\n\t\tpublic int concatenatedBinary(int n) {\n\t\t\tlong size = 0, result = 0;\n\n\t\t\tint ma = (int)1e9+7;\n\n\t\t\tfor(int i = 1;i<=n;i++)
Ziddi_Coder
NORMAL
2020-12-06T04:04:42.965134+00:00
2020-12-06T04:17:39.608131+00:00
359
false
\tclass Solution {\n\t\tpublic int concatenatedBinary(int n) {\n\t\t\tlong size = 0, result = 0;\n\n\t\t\tint ma = (int)1e9+7;\n\n\t\t\tfor(int i = 1;i<=n;i++) {\n\t\t\t\tif((i&(i-1)) == 0) size++;\n\n\t\t\t\tresult = ((result << size) | i)%ma;\n\t\t\t}\n\t\t\treturn (int)result;\n\t\t}\n\t}
2
0
[]
0
concatenation-of-consecutive-binary-numbers
Brute Force works?
brute-force-works-by-iamvaibhave53-amk4
\ndef d(n): \n return bin(n).replace("0b", "") \nclass Solution:\n def concatenatedBinary(self, n: int) -> int:\n a=""\n for i in range(1,n
IamVaibhave53
NORMAL
2020-12-06T04:02:57.665863+00:00
2020-12-06T04:02:57.665907+00:00
208
false
```\ndef d(n): \n return bin(n).replace("0b", "") \nclass Solution:\n def concatenatedBinary(self, n: int) -> int:\n a=""\n for i in range(1,n+1):\n a+=d(i)\n return int(a,2)%(pow(10,9)+7)\n```\n\nNo idea whats the point of this question?
2
0
[]
1
concatenation-of-consecutive-binary-numbers
JavaScript Solution
javascript-solution-by-hongleyou-hfhc
\tvar concatenatedBinary = function(n) {\n\t\tconst mod = 1000000007;\n\t\tlet len = 1, num = 0;\n\n\t\tfor (let i = 1; i <= n; i++) {\n\t\t\tfor (let j = 0; j
hongleyou
NORMAL
2020-12-06T04:02:05.412215+00:00
2020-12-06T04:02:05.412258+00:00
295
false
\tvar concatenatedBinary = function(n) {\n\t\tconst mod = 1000000007;\n\t\tlet len = 1, num = 0;\n\n\t\tfor (let i = 1; i <= n; i++) {\n\t\t\tfor (let j = 0; j < len; j++) {\n\t\t\t\t num = (num << 1) % mod;\n\t\t\t}\n\n\t\t\tnum = (num + i) % mod;\n\n\t\t\tif (((i+1) & i) === 0) {\n\t\t\t\tlen++;\n\t\t\t}\n\t\t}\n\n\t\treturn num;\n\t};\n\n\t/*\n\t 1 10 11 100 101 110 111 1000, 1001, 1010, 1011, 1100, 1101, 1110, 1111\n\n\t 1 6 27 220 \n\t*/
2
0
[]
1
minimum-element-after-replacement-with-digit-sum
Python3 || 2 lines, map ||
python3-2-lines-map-by-spaulding-6nt7
python3 []\nclass Solution:\n def minElement(self, nums: List[int]) -> int:\n\n add_digits = lambda num: sum(int(x) for x in str(num))\n\n retu
Spaulding_
NORMAL
2024-09-28T16:41:28.771315+00:00
2024-09-28T16:46:45.755246+00:00
498
false
```python3 []\nclass Solution:\n def minElement(self, nums: List[int]) -> int:\n\n add_digits = lambda num: sum(int(x) for x in str(num))\n\n return min(map(add_digits, nums))\n```\n\nI could be wrong, but I think that time complexity is *O*(*N*) and space complexity is *O*(1), in which *N* ~ the total number of digits.
13
0
['Python3']
0
minimum-element-after-replacement-with-digit-sum
โœ…Simple Solution
simple-solution-by-durjoybarua5327-qv6w
Python\n\nclass Solution:\n def minElement(self, nums: List[int]) -> int:\n mini=float(\'inf\')\n for num in nums:\n r=0\n
Durjoybarua5327
NORMAL
2024-09-28T16:01:40.505234+00:00
2024-09-28T16:09:48.023963+00:00
1,035
false
**Python**\n```\nclass Solution:\n def minElement(self, nums: List[int]) -> int:\n mini=float(\'inf\')\n for num in nums:\n r=0\n while num>0:\n r+= num%10\n num//=10\n mini= min(mini, r)\n return mini\n```\n**C++**\n```\nclass Solution {\npublic:\n int minElement(vector<int>& nums) {\n int mini =INT_MAX;\n for (int num : nums) {\n int r = 0;\n while (num > 0) {\n r += num % 10;\n num /= 10;\n }\n mini =min(mini, r);\n }\n return mini;\n }\n};\n```
10
0
['C', 'Python', 'Python3']
1
minimum-element-after-replacement-with-digit-sum
Minimum Digit Sum Finder | Beats 100% โœ…
minimum-digit-sum-finder-beats-100-by-sh-l9o7
Intuition\n- Calculate the sum of digits for each number and track the smallest sum.\n\n# Approach\n- Use a helper function to get the sum of digits.\n- Iterate
shubhamyadav32100
NORMAL
2024-09-28T16:04:52.417732+00:00
2024-09-28T16:25:07.849822+00:00
933
false
# Intuition\n- Calculate the sum of digits for each number and track the smallest sum.\n\n# Approach\n- Use a helper function to get the sum of digits.\n- Iterate through the array, updating the minimum digit sum.\n\n# Complexity\n- Time complexity: \n $$O(n \\cdot m)$$ \u2014 \\(n\\) is the number of elements, \\(m\\) is the average digits per number.\n \n- Space complexity: \n $$O(1)$$ \u2014 Constant space for the minimum sum.\n\n# Code\n```java\nclass Solution {\n private int sumOfDigits(int num) {\n int sum = 0;\n while (num > 0) {\n sum += num % 10;\n num /= 10;\n }\n return sum;\n }\n \n public int minElement(int[] nums) {\n int min = Integer.MAX_VALUE;\n for (int num : nums) {\n min = Math.min(min, sumOfDigits(num));\n }\n return min;\n }\n}\n```\n\n# Upvote if you found this helpful! \uD83D\uDE0A
9
0
['Java']
0
minimum-element-after-replacement-with-digit-sum
[Java/C++/Python] Standard Solution
javacpython-standard-solution-by-lee215-w6hi
Explanation\nMod 10 and calculate digits sum.\n\n\n# Complexity\nTime O(nlog1000)\nSpace O(1)\n\n\n\nJava\njava\n public int minElement(int[] nums) {\n
lee215
NORMAL
2024-09-28T16:25:38.776229+00:00
2024-09-28T16:26:54.890633+00:00
418
false
# **Explanation**\nMod 10 and calculate digits sum.\n<br>\n\n# **Complexity**\nTime `O(nlog1000)`\nSpace `O(1)`\n\n<br>\n\n**Java**\n```java\n public int minElement(int[] nums) {\n int res = Integer.MAX_VALUE;\n for (int a : nums) {\n int cur = 0;\n while (a > 0) {\n cur += a % 10;\n a /= 10;\n }\n res = Math.min(res, cur);\n }\n return res;\n }\n```\n\n**C++**\n```cpp\n int minElement(vector<int>& nums) {\n int res = INT_MAX;\n for (int a : nums) {\n int cur = 0;\n while (a > 0) {\n cur += a % 10;\n a /= 10;\n }\n res = min(res, cur);\n }\n return res;\n }\n```\n\n**Python**\n```py\n def minElement(self, nums: List[int]) -> int:\n res = inf\n for a in nums:\n cur = 0\n while a:\n cur += a % 10\n a //= 10\n res = min(res, cur)\n return res\n```\n**Python 1-line**\n```py\n\tdef minElement(self, A: List[int]) -> int:\n return min(sum(map(int, str(a))) for a in A)\n```\n
6
1
[]
1
minimum-element-after-replacement-with-digit-sum
Brute Force - Python
brute-force-python-by-tr1nity-kww4
Please check out LeetCode The Hard Way for more solution explanations and tutorials. If you like it, please give a star and watch my Github Repository. We also
__wkw__
NORMAL
2024-09-30T16:55:39.711445+00:00
2024-09-30T17:07:51.213294+00:00
128
false
Please check out LeetCode The Hard Way for more solution explanations and tutorials. If you like it, please give a star and watch my Github Repository. We also have a Discord server for daily problem discussion. Link in bio.\n\n---\n\nFor each element, we calculate the digit sum and use $res$ to keep track of the minimum one.\n\n```py\nclass Solution:\n def minElement(self, nums: List[int]) -> int:\n res = 10 ** 9\n for x in nums:\n s = 0\n while x > 0:\n s += x % 10\n x //= 10\n res = min(res, s)\n return res\n```
5
0
['Python3']
0
minimum-element-after-replacement-with-digit-sum
Simple || 100% Beats || Easy to Understand
simple-100-beats-easy-to-understand-by-k-lbgp
IntuitionApproachComplexity Time complexity: Space complexity: Code
kdhakal
NORMAL
2025-03-15T14:22:15.763256+00:00
2025-03-15T14:22:15.763256+00:00
91
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```java [] class Solution { public int minElement(int[] nums) { int min = Integer.MAX_VALUE; for(int i = 0; i < nums.length; i++) { int n = nums[i], sum = 0; while(n > 0) { sum += n % 10; n /= 10; } if(sum < min) min = sum; } return min; } } ```
4
0
['Java']
0
minimum-element-after-replacement-with-digit-sum
[Python3] Brute-Force - Detailed Explanation
python3-brute-force-detailed-explanation-1xgm
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n1. Initialization:\n\n-
dolong2110
NORMAL
2024-10-04T10:40:27.418062+00:00
2024-10-04T10:40:27.418085+00:00
99
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n**1. Initialization:**\n\n- `min_num` is initialized to `float("inf")` to store the minimum sum found so far.\n\n**2. Iteration:**\n\n- The loop iterates over each number `num` in the `nums` list.\n\n**3. Digit Sum Calculation:**\n\n- `new_num` is calculated by converting the number `num` to a string, iterating over each digit, converting the digit to an integer, and summing the digits.\n\n**4. Minimum Update:**\n\n- `min_num` is updated to the minimum of its current value and `new_num`.\n\n**5. Return Value:**\n\n- The final value of `min_num` is returned, which represents the minimum sum of digits among all numbers in `nums`.\n\n**Explanation:**\n\nThe code effectively calculates the minimum sum of digits for each number by converting the numbers to strings, iterating over their digits, and summing the digits. The `min_num` variable keeps track of the minimum sum found so far, ensuring that the function returns the correct result.\n\n# Complexity\n- Time complexity: $$O(N * S)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(S)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```python3 []\nclass Solution:\n def minElement(self, nums: List[int]) -> int:\n min_num = float("inf")\n for num in nums:\n new_num = sum(int(d) for d in str(num))\n min_num = min(min_num, new_num)\n return min_num\n```
4
0
['Array', 'String', 'Python3']
0
minimum-element-after-replacement-with-digit-sum
simple and easy C++ solution
simple-and-easy-c-solution-by-shishirrsi-o4r7
if it\'s help, please up \u2B06 vote! \u2764\uFE0F\n\n###### Let\'s Connect on LinkedIn: www.linkedin.com/in/shishirrsiam\n###### Let\'s Connect on Facebook: ww
shishirRsiam
NORMAL
2024-10-01T03:50:39.539143+00:00
2024-10-01T03:50:39.539175+00:00
360
false
# if it\'s help, please up \u2B06 vote! \u2764\uFE0F\n\n###### Let\'s Connect on LinkedIn: www.linkedin.com/in/shishirrsiam\n###### Let\'s Connect on Facebook: www.fb.com/shishirrsiam\n\n\n# Code\n```cpp []\nclass Solution {\npublic:\n int minElement(vector<int>& nums) \n {\n int ans = INT_MAX;\n for(auto val:nums)\n {\n int sum = 0;\n for(auto ch:to_string(val))\n sum += ch - \'0\';\n ans = min(ans, sum);\n }\n return ans;\n }\n};\n```
4
0
['Array', 'Math', 'C++']
3
minimum-element-after-replacement-with-digit-sum
Java's Tiny Treasure Hunt: Min Element Mastery! ๐Ÿ•ต๏ธโ€โ™‚๏ธ๐Ÿ’Ž
javas-tiny-treasure-hunt-min-element-mas-zggp
# Intuition \n\n\n# Approach\n\n- Initialize res: Start with the maximum possible integer value.\n\n- Loop Through Array: Iterate through each number in the a
Jenisssh
NORMAL
2024-11-09T15:57:00.772102+00:00
2024-11-21T11:05:05.611785+00:00
25
false
<!-- # Intuition -->\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- **Initialize res:** Start with the maximum possible integer value.\n\n- **Loop Through Array:** Iterate through each number in the array.\n\n- **Sum Digits for Numbers > 9:** For each number greater than 9, sum its digits.\n\n- **Update Element:** Replace the original number with its digit sum.\n\n- **Update Minimum Value:** Use Math.min to keep track of the smallest value.\n\n- **Return Result:** Return the minimum value found in the array.\n\n# Complexity\n- Time complexity: *O(n * d)*\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: *O(1)*\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java []\nclass Solution {\n public int minElement(int[] nums) {\n // Use the maximum possible value for initialization\n int res = Integer.MAX_VALUE; \n\n for (int i = 0; i < nums.length; i++) {\n if (nums[i] > 9) {\n int k = nums[i];\n int sum = 0;\n \n // Correct the logic for reducing the number\n while (k > 0) {\n // Sum the digits\n sum += k % 10; \n // Move to the next digit\n k /= 10;\n }\n\n nums[i] = sum;\n }\n // Update the result with the current minimum\n res = Math.min(res, nums[i]); \n }\n\n return res;\n }\n}\n\n```
3
0
['Java']
0
minimum-element-after-replacement-with-digit-sum
๐Ÿ’ขโ˜ ๐Ÿ’ซEasiest๐Ÿ‘พFasterโœ…๐Ÿ’ฏ Lesser๐Ÿง  ๐ŸŽฏ C++โœ…Python3๐Ÿโœ…Javaโœ…Cโœ…Python๐Ÿโœ…C#โœ…๐Ÿ’ฅ๐Ÿ”ฅ๐Ÿ’ซExplainedโ˜ ๐Ÿ’ฅ๐Ÿ”ฅ Beats 100
easiestfaster-lesser-cpython3javacpython-vahr
Intuition\n\n Describe your first thoughts on how to solve this problem. \njavascript []\n//JavaScript Solution\n/**\n * @param {number[]} nums\n * @return {num
Edwards310
NORMAL
2024-09-28T18:02:18.979485+00:00
2024-09-28T18:02:18.979511+00:00
703
false
# Intuition\n![0ehh83fsnh811.jpg](https://assets.leetcode.com/users/images/5b3640ff-cc15-4c82-adca-c192e39a7a15_1727546185.431474.jpeg)\n<!-- Describe your first thoughts on how to solve this problem. -->\n```javascript []\n//JavaScript Solution\n/**\n * @param {number[]} nums\n * @return {number}\n */\nvar minElement = function(nums) {\n let ans = Infinity;\n\n for (let num of nums) {\n const digitSum = num.toString().split(\'\').reduce((sum, digit) => sum + Number(digit), 0);\n ans = Math.min(ans, digitSum);\n }\n\n return ans;\n};\n```\n```C++ []\n//?C++ Solution\n#include <bits/stdc++.h>\nusing namespace std;\n\n#define FOR(a, b, c) for (int a = b; a < c; a++)\n#define FOR1(a, b, c) for (int a = b; a <= c; ++a)\n#define Rep(i, n) FOR(i, 0, n)\n#define Rep1(i, n) FOR1(i, 1, n)\n#define RepA(ele, nums) for (auto& ele : nums)\n\nclass Solution {\npublic:\n int minElement(vector<int>& nums) {\n int ans = INT_MAX;\n RepA (num, nums){\n int sum = 0;\n string str = to_string(num);\n RepA (c, str)\n sum += c - \'0\';\n ans = fmin(ans, sum);\n }\n return ans;\n }\n};\n```\n```Python3 []\n#Python3 Solution\nclass Solution:\n def minElement(self, nums: List[int]) -> int:\n return min(sum(int(d) for d in str(num)) for num in nums)\n```\n```Java []\n//Java Solution\nclass Solution {\n public int minElement(int[] nums) {\n int n = nums.length;\n int ans = Integer.MAX_VALUE;\n for (int x : nums) {\n int sum = 0;\n while (x != 0) {\n sum += (x % 10);\n x /= 10;\n }\n ans = Math.min(ans, sum);\n }\n return ans;\n }\n}\n```\n```C []\n//C Solution\n\nint minElement(int* nums, int numsSize) {\n int ans = INT_MAX;\n for (int i = 0; i < numsSize; ++i) {\n int sum = 0;\n while(nums[i]) {\n sum += nums[i] % 10;\n nums[i] /= 10;\n }\n ans = fmin(ans, sum);\n }\n return ans;\n}\n```\n```python []\n#Python SOlution\nclass Solution(object):\n def minElement(self, nums):\n """\n :type nums: List[int]\n :rtype: int\n """\n ans = float(\'inf\')\n for num in nums:\n ans = min(ans, sum(ord(x) - ord(\'0\') for x in str(num)))\n return ans\n```\n```C# []\n//C# Solution\npublic class Solution {\n public int MinElement(int[] nums) {\n var ans = Int32.MaxValue;\n foreach (var x in nums) {\n var sum = 0;\n var num = x;\n while (num != 0) {\n sum += num % 10;\n num /= 10;\n }\n ans = Math.Min(ans, sum);\n }\n return ans;\n }\n}\n```\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n![th.jpeg](https://assets.leetcode.com/users/images/dd5f4074-350b-4dbf-b5aa-8b63eab0e81a_1727546531.1698544.jpeg)\n
3
0
['Math', 'String', 'C', 'Python', 'C++', 'Java', 'Python3', 'JavaScript', 'C#']
0
minimum-element-after-replacement-with-digit-sum
[Python3] 1-line
python3-1-line-by-ye15-f9di
Please pull this commit for solutions of biweekly 140. \n\n\nclass Solution:\n def minElement(self, nums: List[int]) -> int:\n return min(sum(map(int,
ye15
NORMAL
2024-09-28T16:59:14.725192+00:00
2024-09-29T21:53:48.336894+00:00
141
false
Please pull this [commit](https://github.com/gaosanyong/leetcode/commit/b9fe03c491aa04ce243646bccf4b5ccab15f2d53) for solutions of biweekly 140. \n\n```\nclass Solution:\n def minElement(self, nums: List[int]) -> int:\n return min(sum(map(int, str(x))) for x in nums)\n```
3
0
['Python3']
0
minimum-element-after-replacement-with-digit-sum
Easy solution in c++ beat 100% ....simple
easy-solution-in-c-beat-100-simple-by-ka-g31v
Code
Kaviyant7
NORMAL
2025-01-21T05:43:36.625922+00:00
2025-01-21T05:43:36.625922+00:00
154
false
# Code ```cpp [] class Solution { public: int minElement(vector<int>& nums) { vector<int> v; for (int n : nums) { int s = 0; while (n) { s += n % 10; n /= 10; } v.push_back(s); } sort(v.begin(), v.end()); return v[0]; } }; ```
2
0
['C++']
0
minimum-element-after-replacement-with-digit-sum
best solution
best-solution-by-lohieth-rvrl-383b
IntuitionKPR institute of engineering and technologyCode
lohieth-rvrl
NORMAL
2024-12-28T17:01:56.694546+00:00
2024-12-28T17:01:56.694546+00:00
261
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> KPR institute of engineering and technology # Code ```java [] class Solution { public int minElement(int[] nums) { int min = Integer.MAX_VALUE; for(int i=0;i<nums.length;i++){ int sum = sum(nums[i]); nums[i] = sum; } for(int i=0;i<nums.length;i++){ min = Math.min(min,nums[i]); } return min; } public int sum(int n){ int sum = 0; while(n>0){ int rem = n%10; sum += rem; n /= 10; } return sum; } } ```
2
0
['Java']
1
minimum-element-after-replacement-with-digit-sum
very easy solution || c++
very-easy-solution-c-by-kabhishekkumarsi-rz03
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
kabhishekkumarsing
NORMAL
2024-10-06T05:25:18.166226+00:00
2024-10-06T05:25:18.166251+00:00
175
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```cpp []\nclass Solution {\npublic:\n int minElement(vector<int>& nums) {\n int ans=INT_MAX;\n for(int i=0;i<nums.size();i++){\n int n=nums[i];\n int digit_sum=0;\n while(n>0){\n digit_sum+=n%10;\n n/=10;\n }\n if(digit_sum<ans) ans=digit_sum;\n }\n return ans;\n }\n};\n```
2
0
['C++']
1
minimum-element-after-replacement-with-digit-sum
Java Clean Simple Solution
java-clean-simple-solution-by-shree_govi-4gq1
Complexity\n- Time complexity:O(n*X)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity:O(1)\n Add your space complexity here, e.g. O(n) \n\n# Co
Shree_Govind_Jee
NORMAL
2024-09-28T16:01:56.308584+00:00
2024-09-28T16:01:56.308617+00:00
350
false
# Complexity\n- Time complexity:$$O(n*X)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:$$O(1)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java []\nclass Solution {\n \n private int getSumDigits(int num){\n int sum = 0;\n while(num != 0){\n sum += num%10;\n num/=10;\n }\n return sum;\n }\n \n public int minElement(int[] nums) {\n int res=Integer.MAX_VALUE;\n for(int num:nums){\n res = Math.min(res, getSumDigits(num));\n }\n return res;\n }\n}\n```
2
0
['Math', 'Enumeration', 'Number Theory', 'Java']
0
minimum-element-after-replacement-with-digit-sum
EASY WAY - TRY THIS - THIS - THIS
easy-way-try-this-this-this-by-sairangin-gqrs
IntuitionWe need to find the minimum element after replacing each number with the sum of its digits.So, we should convert each number to a string, sum its digit
Sairangineeni
NORMAL
2025-04-06T07:19:44.581394+00:00
2025-04-06T07:19:44.581394+00:00
16
false
# Intuition We need to find the minimum element after replacing each number with the sum of its digits. So, we should convert each number to a string, sum its digits, and store it. --- # Approach Loop through the input array. Convert each nums[i] to a string and sum its digits. Store the digit sums in an ArrayList. Sort the list and return the smallest value (at index 0). # Code ```java [] class Solution { public static int minElement(int[] nums) { int n = nums.length; ArrayList<Integer> ans = new ArrayList<>(); for (int i = 0; i < n; i++) { String s = Integer.toString(nums[i]); int sum = 0; for (char ch : s.toCharArray()) { sum += ch - '0'; } ans.add(sum); } Collections.sort(ans); return ans.get(0); } } ```
1
0
['Java']
0
minimum-element-after-replacement-with-digit-sum
Simple Solution
simple-solution-by-pes_guy-7c4e
IntuitionThe problem requires finding the number in the list whose sum of digits is the smallest. The approach involves iterating through each number, extractin
PES_Guy
NORMAL
2025-04-04T03:57:47.143186+00:00
2025-04-04T03:57:47.143186+00:00
14
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> The problem requires finding the number in the list whose sum of digits is the smallest. The approach involves iterating through each number, extracting its digits, summing them up, and then returning the minimum sum. # Approach <!-- Describe your approach to solving the problem. --> 1. Initialize an empty list `a` to store the sum of digits for each number. 2. Iterate through each number in `nums`. 3. For each number, extract its digits and compute their sum. 4. Store the computed sum in the list `a`. 5. Return the minimum value from `a`. # Code ```python3 [] from typing import List class Solution: def minElement(self, nums: List[int]) -> int: a = [] for num in nums: s = 0 temp = num while temp > 0: ld = temp % 10 s += ld temp = temp // 10 a.append(s) return min(a) ```
1
0
['Python3']
0
minimum-element-after-replacement-with-digit-sum
100.00% || O(n) || Basic Solution
10000-on-basic-solution-by-thakur_avanee-fdnd
IntuitionApproachComplexity Time complexity: Space complexity: Code
Thakur_Avaneesh
NORMAL
2025-03-19T06:56:15.146274+00:00
2025-03-19T06:56:15.146274+00:00
75
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```cpp [] class Solution { public: int minElement(vector<int>& nums) { for (int i = 0; i < nums.size(); i++) if (nums[i] > 9) { int rem = 0, sum = 0; while (nums[i] > 9) { rem = nums[i] % 10; nums[i] /= 10; sum += rem; } nums[i] += sum; } return *min_element(nums.begin(), nums.end()); } }; ```
1
0
['Array', 'Math', 'C++']
0
minimum-element-after-replacement-with-digit-sum
Simple and easy to understand.
simple-and-easy-to-understand-by-dipanke-ui93
Code
dipankerz
NORMAL
2025-03-12T15:35:06.428100+00:00
2025-03-12T15:35:06.428100+00:00
41
false
# Code ```python3 [] class Solution: def minElement(self, nums: List[int]) -> int: min_e = 100 for e in nums: sum_e = 0 while e > 0: digit = e % 10 sum_e += digit e = e // 10 if sum_e < min_e: min_e = sum_e return min_e ```
1
0
['Python3']
0
minimum-element-after-replacement-with-digit-sum
Intuitive ones without converting to string and replacement
intuitive-ones-without-converting-to-str-pxht
IntuitionSimply use / % calculation of interger, without converting to string, and also without a real "replacement" by iterating the minimum sum of digits dire
ralf_phi
NORMAL
2025-03-07T01:13:19.827492+00:00
2025-03-07T01:13:19.827492+00:00
49
false
# Intuition Simply use / % calculation of interger, without converting to string, and also without a real "replacement" by iterating the minimum sum of digits directly. # Approach Calculate the digit sum for each number inside integer array, and iterate the sum of digits. # Complexity - Time complexity: O(N) - Space complexity: O(1) # Code ```cpp [] class Solution { public: int minElement(vector<int>& nums) { int ans = INT_MAX; for (int num : nums) { int dSum = 0; while (num > 0) { dSum += num % 10; num /= 10; } ans = min(ans, dSum); } return ans; } }; ```
1
0
['C++']
0
minimum-element-after-replacement-with-digit-sum
3300 . Minimum Element After Replacement With Digit Sum Beat 100%
3300-minimum-element-after-replacement-w-1k7d
IntuitionApproachComplexity Time complexity: O(Nlogn) Space complexity: O(N)Code
RaviKumarChaurasiya
NORMAL
2025-02-16T06:36:22.414484+00:00
2025-02-16T06:36:22.414484+00:00
84
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> O(Nlogn) - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> O(N) # Code ```cpp [] class Solution { public: int minElement(vector<int>& nums) { for(int i=0;i<nums.size();i++) { nums[i] = sum(nums[i]); } sort(nums.begin(),nums.end()); return nums[0]; } int sum(int n) { int s = 0; while(n != 0) { s += (n % 10); n /= 10; } return s; } }; ```
1
0
['Array', 'Math', 'C++']
0
minimum-element-after-replacement-with-digit-sum
[BEST SOLUTION] Beats 100% ๐Ÿฆ…
best-solution-beats-100-by-asmit_kandwal-9ova
IntuitionApproachComplexity Time complexity: Space complexity: Code
Asmit_Kandwal
NORMAL
2025-02-10T15:03:47.396130+00:00
2025-02-10T15:03:47.396130+00:00
82
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```cpp [] class Solution { public: int minElement(vector<int>& nums) { int mini = INT_MAX; // Store the minimum value for (auto i : nums) { if (i > 9) { // Compute digit sum if number > 9 int sum = 0; while (i != 0) { sum += i % 10; i /= 10; } i = sum; } mini = min(i, mini); // Track the minimum value } return mini; // Return the smallest processed value } }; ```
1
0
['C++']
0
minimum-element-after-replacement-with-digit-sum
Using Arrays.sort() in java
using-arrayssort-in-java-by-adarshrover-6lil
IntuitionApproachComplexity Time complexity: Space complexity: Code
adarshrover
NORMAL
2025-02-07T05:48:26.679886+00:00
2025-02-07T05:48:26.679886+00:00
6
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```java [] class Solution { public int minElement(int[] nums) { int [] ans = new int[nums.length]; // int sum_digit=0; for(int i=0;i<nums.length;i++){ if(nums[i]>=10){ while(nums[i]>0){ ans[i] +=nums[i]%10; nums[i]/=10; } } else{ ans[i]=nums[i]; } } Arrays.sort(ans); return ans[0]; } } ```
1
0
['Java']
0
minimum-element-after-replacement-with-digit-sum
BEATS 100%
beats-100-by-its_gokhul-w45p
IntuitionApproachComplexity Time complexity: Space complexity: Code
its_gokhul
NORMAL
2025-01-28T02:48:47.813735+00:00
2025-01-28T02:48:47.813735+00:00
117
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```java [] class Solution { public int minElement(int[] nums) { int min = Integer.MAX_VALUE; for(int num : nums){ min = Math.min(min,digitSum(num)); } return min; } private int digitSum(int n ){ int sum = 0; while(n > 0){ sum += n % 10; n /= 10; } return sum; } } ```
1
0
['Java']
0
minimum-element-after-replacement-with-digit-sum
Solution in C
solution-in-c-by-akcyyix0sj-yg6v
Code
vickyy234
NORMAL
2025-01-05T04:39:23.536870+00:00
2025-01-05T04:39:23.536870+00:00
63
false
# Code ```c [] int minElement(int* nums, int numsSize) { int x = 0; for (int i = 0; i < numsSize; i++) { while (nums[i] > 0) { x += nums[i] % 10; nums[i] /= 10; } nums[i] = x; x = 0; } x = nums[0]; for (int i = 1; i < numsSize; i++) { if (nums[i] < x) x = nums[i]; } return x; } ```
1
0
['C']
0
minimum-element-after-replacement-with-digit-sum
Solution in C
solution-in-c-with-comments-for-easy-und-1zxp
Approach Calculate the sum of digits for each number. Store the results in an array. Find the smallest value in that array. Return the smallest value. Code
VishalKannan_070
NORMAL
2025-01-03T13:30:46.828062+00:00
2025-01-09T05:48:26.047588+00:00
31
false
# Approach - Calculate the sum of digits for each number. - Store the results in an array. - Find the smallest value in that array. - Return the smallest value. # Code ```c [] int minElement(int* nums, int numsSize) { int temp = 0; int* result = (int*)calloc(numsSize, sizeof(int)); for (int i = 0; i < numsSize; i++) { while (nums[i] > 0) { temp = nums[i] % 10; nums[i] /= 10; result[i] += temp; } } int min = result[0]; for (int i = 0; i < numsSize; i++) { if (result[i] < min) { min = result[i]; } } free(result); return min; } ```
1
0
['Array', 'C']
0
minimum-element-after-replacement-with-digit-sum
Detailed Python Solution
detailed-python-solution-by-annibamwenda-fjm6
IntuitionThe goal is to replace each number in nums with the sum of its digits.ApproachCreate a helper function that calculates the digit sums then use it to re
AnniBamwenda
NORMAL
2024-12-31T15:58:12.395412+00:00
2024-12-31T15:59:54.512557+00:00
76
false
# Intuition The goal is to replace each number in nums with the sum of its digits. # Approach Create a helper function that calculates the digit sums then use it to return the minimum sum. For a more flowy solution, see the brute force # Complexity - Time complexity: For the Optimized solution, $$O(n.log(k))$$ For the Brute Force, $$O(n.log(k)) + O(n)$$ - Space complexity: For the Optimized solution, $$O(1)$$ For the Brute Force, $$O(n)$$ # Code ```python3 [] class Solution: def minElement(self, nums: List[int]) -> int: # Optimized solution # Step 01: We'll create a helper function that will calculate the digit sum of each # number in the nums list def digitSum(num): return sum(int(digit) for digit in str(num)) # Step 02: Return the minimum sum from the array return min(digitSum(num) for num in nums) # Time complexity for Optimized Solution # Step 01: It takes O(log(num)) to loop through the digits in the str and O(1) # to calculate the sum of all digits. Its O(log(num)) because the time # it takes depends on the number of digits rather than the size of the number. # Step 02: It takes O(n) to loop through the nums array and find the minimum. n is the # length of nusm array # Combined: It takes O(n.log(k)) to find the digit sums of all numbers and return the min. n = len(nums) and k is len(num) # Space Complexity: O(1) for the summation of digits # # Brute Force: # # Step 01: Use mapping to convert nums to a string array # nums = list(map(str, nums)) # numsSum = [] # where we'll store the sums of the digits # # Step 02: Loop the the new nums and add the digits in each position # curr_sum = 0 # for val in nums: # print(val) # for x in val: # print(x) # curr_sum += int(x) # print(curr_sum) # numsSum.append(curr_sum) # curr_sum = 0 #reset current sum to 0 # # Step 03: Return the minimum element in numsSum # return min(numsSum) # Time complexity for Brute Force: # Step 01: O(n) to map all numbers in num to strings. n is the length of nums # Step 02: O(n.log(k)) to loop through each digit of elements in nums. n is the # length of of nums and k is the number of digits in num # Step 03: O(n) to get the minimum sum n = len(nums) = len(numsSum) # Space complexity: O(n) to store the digit sums in numsSum array ```
1
0
['Python3']
0
minimum-element-after-replacement-with-digit-sum
Complete Solution with detailed Explanation beats 100%....
complete-solution-with-detailed-explanat-lufk
IntuitionAfter Reading the problem we understood that we have to return the minimum number after performing the operations.Approach Step 1: Declare the integer
shreyasbawaskar0812
NORMAL
2024-12-29T09:32:56.399888+00:00
2024-12-29T09:32:56.399888+00:00
22
false
# Intuition After Reading the problem we understood that we have to return the minimum number after performing the operations. # Approach 1. **Step 1:** Declare the integer which we would return hence I declared and defined `min` to any random value. 2. **Step 2:** Iterate along the given `nums` array while finding the sum. 3. **Step 3:** Compared it with our `min`. 4. **Step 4:** Return the minimum value. # Complexity - Time complexity: 1. Initialization: `int min=nums[0];`this operation takes $$O(1)$$ time. 2. For Loop: `for(int i=0;i<nums.length;i++)` - The loop runs `n` times, where *n* is the length of nums array. - Inside the loop, the statement `min>nums[i]%10+(nums[i]/10)%10+(nums[i]/100)%10+(nums[i]/1000)%10+(nums[i]/10000)` involves constant-time operations since each division and modulus operation is $$O(1)$$ Therefore the total Time complexity is $$O(1) + O(n) = O(n)$$ - Space complexity: 1. Variables: The variable min takes $$O(1)$$ space. 2. Input Array: The input array nums is provided as input and doesn't add to the space complexity. The algorithm doesn't use any additional data structures that grow with the size of the input array, so the space complexity is: $$O(1)$$ # Code ```java [] class Solution { public int minElement(int[] nums) { int min=nums[0]; for(int i=0;i<nums.length;i++){ if (min>nums[i]%10+(nums[i]/10)%10+(nums[i]/100)%10+(nums[i]/1000)%10+(nums[i]/10000)){ min = nums[i]%10+(nums[i]/10)%10+(nums[i]/100)%10+(nums[i]/1000)%10+(nums[i]/10000); } } return min; } } ```
1
1
['Array', 'Math', 'Java']
0
minimum-element-after-replacement-with-digit-sum
C++ Early Termination 100%
c-early-termination-100-by-michelusa-uy52
Iteration with early termination.\n\ncpp []\nclass Solution {\npublic:\n int minElement(vector<int>& nums) {\n int min_sum = std::numeric_limits<int>:
michelusa
NORMAL
2024-11-29T16:41:14.581656+00:00
2024-11-29T16:41:14.581693+00:00
46
false
Iteration with early termination.\n\n```cpp []\nclass Solution {\npublic:\n int minElement(vector<int>& nums) {\n int min_sum = std::numeric_limits<int>::max();\n for (size_t i = 0; i != nums.size(); ++i) {\n int sum = 0;\n int val = nums[i];\n while (val && sum < min_sum) {\n sum += val % 10;\n val /= 10; \n } \n min_sum = std::min(min_sum, sum); \n }\n return min_sum;\n }\n};\n```
1
0
['C++']
0
minimum-element-after-replacement-with-digit-sum
Easy to understand Solution with simplest function|| 100% BEATS
easy-to-understand-solution-with-simples-0clv
Proof 100% Beats\n\n\n# Intuition\nThe problem can be interpreted as transforming each number in the array into the sum of its digits and then finding the minim
Peush9
NORMAL
2024-10-15T19:19:28.775605+00:00
2024-10-15T21:43:52.855470+00:00
116
false
# Proof 100% Beats\n![Screenshot 2024-10-16 002039.png](https://assets.leetcode.com/users/images/4bc4a612-a340-4614-8169-4af1895b7bfd_1729019698.9495056.png)\n\n# Intuition\nThe problem can be interpreted as transforming each number in the array into the sum of its digits and then finding the minimum of these transformed values. To solve this, we break the task into two parts:\n\n1. For each element in the array, replace it with the sum of its digits.\n2. Find the minimum element after this transformation.\n\n# Approach\n1. Sum of Digits Calculation:\nThe function sum(int n) computes the sum of digits of a given integer n. This is done by continuously extracting the last digit using the modulo operator (n % 10) and adding it to the total. We then reduce n by dividing it by 10 (n = n / 10) to remove the last digit.\n\n2. Transform Array:\nIn the minElement method, we iterate over the input array nums. For each element nums[i], we calculate its digit sum using the sum() function and replace the element with this value.\n\n3. Find Minimum:\nAfter transforming all elements of the array, we iterate over it again to find the smallest transformed value by using Math.min().\n\n# Summary\n- Time Complexity: O(n * log(m)), where n is the size of the input array and m is the largest number in the array.\n- Space Complexity: O(1), constant space used apart from the input array.\n\n# Complexity\n1. Sum of Digits Function: The function sum(int n) takes O(d), where d is the number of digits in n. For large integers, d can be approximated as log(n) since the number of digits grows logarithmically with n.\n\n2. Transform Array: We traverse the array of length n and for each element, we compute the sum of digits. So, the overall complexity of this step is O(n * log(m)), where n is the size of the array, and m is the maximum number in the array (log(m) is the time for sum of digits).\n\n3. Finding Minimum: The second loop just scans through the array to find the minimum value, which is O(n).\n\nThus, the total time complexity is:\n\nO(n * log(m)), where n is the number of elements in the array and m is the maximum number in the array.\n\n# Space complexity:\nThe space complexity is O(1), as we are modifying the input array in place and using only a few extra variables (total, a, min). No additional space is used apart from the input array, so the space complexity is constant\n\n# Code\n```java []\nclass Solution {\n public int sum(int n){\n int total = 0;\n while(n > 0){\n total += n%10;\n n = n/10;\n }\n return total;\n }\n public int minElement(int[] nums) {\n int n = nums.length;\n for(int i=0; i<n; i++){\n int a = sum(nums[i]);\n nums[i] = a;\n }\n \n int min = Integer.MAX_VALUE;\n for(int i=0; i<n; i++){\n min = Math.min(min, nums[i]);\n }\n return min;\n }\n}\n \n \n```
1
0
['Array', 'Math', 'Iterator', 'Java']
1
minimum-element-after-replacement-with-digit-sum
Finding Minimum Element After Replacement With Digit Sum using Dart GOAT ๐Ÿ language.
finding-minimum-element-after-replacemen-46nz
Intuition\n- From the given description it is clear that, all we need to do is find the sum of all digits in the given list and return the minimum of it.\n\n# A
Abdusalom_16
NORMAL
2024-10-12T04:37:16.296215+00:00
2024-10-12T04:37:16.296237+00:00
12
false
# Intuition\n- **From the given description it is clear that, all we need to do is find the sum of all digits in the given list and return the minimum of it.**\n\n# Approach\n1. First of all I used for loop to iterate through the given argument list.\n2. In each iteration I converted each iterated value to String and used another for loop to iterate through that converted value. But before that I created a variable to store the digits sum of iterated number. In each iteration I added that iterated value to the sum variable. After that loop ends I replaced the value of the iterated value with sum value.\n3. At the end I sorted the list using sort() method and returned the minimum value of the list.\n\n# Code\n```dart []\nclass Solution {\n int minElement(List<int> nums) {\n for(int i = 0; i < nums.length; i++){\n String conv = nums[i].toString();\n int sum = 0;\n for(int j = 0; j < conv.length; j++){\n sum+=int.parse(conv[j]);\n }\n nums[i] = sum;\n }\n\n nums.sort((a, b) => a.compareTo(b));\n return nums.first;\n}\n}\n```
1
0
['Array', 'Math', 'Dart']
0
minimum-element-after-replacement-with-digit-sum
EASY JS SOLUTION ๐Ÿซ 
easy-js-solution-by-joelll-lv46
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
Joelll
NORMAL
2024-10-08T11:22:30.721161+00:00
2024-10-08T11:22:30.721189+00:00
23
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```javascript []\n/**\n * @param {number[]} nums\n * @return {number}\n */\nvar minElement = function(nums) {\n let arr = []\n for(let i =0;i<nums.length;i++){\n let odi = nums[i].toString().split("").map(Number)\n let sum = odi.reduce((acc,curr)=>acc+curr,0)\n console.log(sum)\n arr.push(sum)\n }\n return Math.min(...arr)\n};\n```
1
0
['JavaScript']
0
minimum-element-after-replacement-with-digit-sum
Beginner friendly solution using Python
beginner-friendly-solution-using-python-ae2wn
\n\n# Code\npython3 []\nclass Solution:\n def minElement(self, nums: List[int]) -> int:\n\n sol = []\n\n for i in nums:\n s = str(i)
vigneshvaran0101
NORMAL
2024-10-03T16:19:20.747590+00:00
2024-10-03T16:19:20.747615+00:00
27
false
\n\n# Code\n```python3 []\nclass Solution:\n def minElement(self, nums: List[int]) -> int:\n\n sol = []\n\n for i in nums:\n s = str(i)\n c = 0\n print(i)\n for j in s:\n c += int(j)\n sol.append(c)\n\n return min(sol)\n\n \n```
1
0
['Python3']
0
minimum-element-after-replacement-with-digit-sum
Rust Solution
rust-solution-by-abhineetraj1-7jrj
Complexity\n- Time complexity: O(k.d) , where kk is the number of elements in nums and dd is the average number of digits in the numbers.\n\n Add your time comp
abhineetraj1
NORMAL
2024-10-03T01:53:33.998722+00:00
2024-10-03T01:53:33.998761+00:00
12
false
# Complexity\n- Time complexity: O(k.d) , where kk is the number of elements in nums and dd is the average number of digits in the numbers.\n\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```rust []\nimpl Solution {\n fn sum_of_digits(mut num: i32) -> i32 {\n let mut sum = 0;\n while num > 0 {\n sum += num % 10;\n num /= 10;\n }\n sum\n }\n pub fn min_element(nums: Vec<i32>) -> i32 {\n nums.iter()\n .map(|&num| Self::sum_of_digits(num))\n .min()\n .unwrap_or(0)\n }\n}\n\n```
1
0
['Rust']
0
minimum-element-after-replacement-with-digit-sum
easy and intuitive
easy-and-intuitive-by-harsh_saini_3878-4i2y
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
HARSH_SAINI_3878
NORMAL
2024-10-01T05:38:42.694630+00:00
2024-10-01T05:38:42.694667+00:00
162
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```cpp []\nclass Solution {\npublic:\nint fun(int num){\n int ans=0;\n while(num){\n ans+=num%10;\n num/=10;\n }\n return ans;\n}\n int minElement(vector<int>& nums) {\n int ans=1e8;\n for(auto it :nums){\n int x=fun(it);\n ans=min(x,ans);\n }\n return ans;\n }\n};\n```
1
0
['C++']
0
minimum-element-after-replacement-with-digit-sum
Swift๐Ÿ’ฏ
swift-by-upvotethispls-svcy
Good Interview Answer (accepted answer)\n\nclass Solution {\n func minElement(_ nums: [Int]) -> Int {\n var smallest = Int.max\n for var num in
UpvoteThisPls
NORMAL
2024-09-30T23:42:45.309448+00:00
2024-09-30T23:45:26.504918+00:00
10
false
**Good Interview Answer (accepted answer)**\n```\nclass Solution {\n func minElement(_ nums: [Int]) -> Int {\n var smallest = Int.max\n for var num in nums {\n var digitSum = 0\n while num > 0 {\n digitSum += num % 10\n num /= 10\n }\n smallest = min(smallest, digitSum) \n }\n return smallest\n }\n}\n```\n\n**BONUS: Rewritten as one-liner (accepted answer)**\n```\nclass Solution {\n func minElement(_ nums: [Int]) -> Int {\n nums.map{num in [1,10,100,1000,10000].map{(num/$0) % 10}.reduce(0,+)}.min()!\n }\n}\n```
1
0
['Swift']
0
minimum-element-after-replacement-with-digit-sum
Beat 100%๐Ÿ’ฏ || EASY TO UNDERSTAND๐Ÿ’ฏ๐Ÿ”ฅโœ…
beat-100-easy-to-understand-by-tanish-hr-a9ap
\n# Code\njava []\nimport java.util.Arrays;\nclass Solution {\n public int minElement(int[] nums) {\n int n= nums.length;\n int[] ans = new int
tanish-hrk
NORMAL
2024-09-30T18:25:36.599149+00:00
2024-09-30T18:25:36.599185+00:00
60
false
\n# Code\n```java []\nimport java.util.Arrays;\nclass Solution {\n public int minElement(int[] nums) {\n int n= nums.length;\n int[] ans = new int[n];\n for(int i=0;i<n;i++){\n int sum=0;\n while(nums[i]>0){\n int x=nums[i]%10;\n sum+=x;\n nums[i]/=10;\n }\n ans[i]=sum;\n }\n return Arrays.stream(ans).min().getAsInt();\n }\n}\n```
1
0
['Java']
0
minimum-element-after-replacement-with-digit-sum
easy solution
easy-solution-by-leet1101-fetp
Intuition\nThe problem asks to replace each element in the array with the sum of its digits and then return the minimum element. To solve this, we need to proce
leet1101
NORMAL
2024-09-30T07:32:52.943973+00:00
2024-09-30T07:32:52.944001+00:00
106
false
# Intuition\nThe problem asks to replace each element in the array with the sum of its digits and then return the minimum element. To solve this, we need to process each element by computing the sum of its digits and then find the smallest among the transformed values.\n\n# Approach\n1. **Sum of Digits**: Create a helper function `digitsSum` that takes an integer and returns the sum of its digits by repeatedly extracting the last digit using modulo (`%`) and adding it to a running total.\n2. **Transform Array**: Iterate through each element in the `nums` array and replace it with the sum of its digits.\n3. **Find Minimum**: After transforming the array, iterate through it again to find the minimum value.\n\n# Complexity\n- **Time complexity**: \n - Computing the sum of digits for each number takes time proportional to the number of digits, i.e., $$O(d)$$ where $$d$$ is the number of digits.\n - For an array of size $$n$$, the overall time complexity is $$O(n \\cdot d)$$, where $$d$$ is the average number of digits.\n \n- **Space complexity**: \n $$O(1)$$, since we are modifying the input array in place and only using a constant amount of extra space.\n\n# Code\n```cpp\nclass Solution {\npublic:\n int minElement(vector<int>& nums) {\n // Replace each number with the sum of its digits\n for (int i = 0; i < nums.size(); i++) {\n nums[i] = digitsSum(nums[i]);\n }\n \n // Find the minimum element in the transformed array\n int ans = INT_MAX;\n for (int i = 0; i < nums.size(); i++) {\n ans = min(ans, nums[i]);\n }\n \n return ans;\n }\n \n // Helper function to compute the sum of digits of a number\n int digitsSum(int x) {\n int total = 0;\n while (x) {\n total += x % 10; // Add the last digit\n x /= 10; // Remove the last digit\n }\n return total;\n }\n};\n```
1
0
['C++']
0
minimum-element-after-replacement-with-digit-sum
for beginners
for-beginners-by-batch89-myv0
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
batch89
NORMAL
2024-09-29T13:40:28.704360+00:00
2024-09-29T13:40:28.704394+00:00
24
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```cpp []\nclass Solution {\npublic:\n int ans(int &val){\n int ans=0;\n while(val!=0){\n ans=ans+val%10;\n val=val/10;\n }\n return ans;\n }\n int minElement(vector<int>& nums) {\n vector<int>arr;\n int val;\n int n=nums.size();\n for(int i=0; i<n; i++){\n val=nums[i];\n arr.push_back(ans(val));\n }\n sort(arr.begin(),arr.end());\n return arr[0];\n }\n};\n```
1
0
['C++']
0
minimum-element-after-replacement-with-digit-sum
JS One Line beats 100%
js-one-line-beats-100-by-sergeygerax-fklm
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \nGet min number from map
SergeyGerax
NORMAL
2024-09-29T12:04:52.885973+00:00
2024-09-29T12:04:52.885998+00:00
124
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nGet min number from mapped array, numbers converted to string and findSum\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```javascript []\n/**\n * @param {number[]} nums\n * @return {number}\n */\nvar minElement = function(nums) {\n return Math.min(...nums.map(n => String(n).split(\'\').reduce((a, b) => Number(a) + Number(b), 0)))\n};\n```
1
0
['JavaScript']
1
minimum-element-after-replacement-with-digit-sum
Different approach than mod, Simple Python Solution for beginners
different-approach-than-mod-simple-pytho-xytp
Intuition\nAt first glance, looking at the problem said to sum the digits of each number on the given list. hence i proceded with a different way than mod, prob
Himanshi_Maheshwari
NORMAL
2024-09-29T09:43:13.497211+00:00
2024-09-29T09:43:13.497246+00:00
54
false
# Intuition\nAt first glance, looking at the problem said to sum the digits of each number on the given list. hence i proceded with a different way than mod, probably comparitively lesser lines of code but definitely a little lost on time complexity.\n\n# Approach\nConverting each number on the list to string, then taking the sum of the int conversion of each charachter of that string.\n\n# Complexity\n- Time complexity:\nO(N\u2217M)\n\n- Space complexity:\nO(1)\n\n# Code\n```python3 []\nclass Solution:\n def minElement(self, nums: List[int]) -> int:\n for i in range(0,len(nums)):\n s=str(nums[i])\n sum1=0\n for j in s:\n sum1+=int(j)\n nums[i]=sum1\n return min(nums)\n```
1
0
['Python', 'Python3']
0
minimum-element-after-replacement-with-digit-sum
Easiest Java / C++ / C Solution (With Approach) ๐Ÿ˜Ž๐Ÿ˜Ž -> ๐Ÿ‘Œ๐Ÿ‘Œ
easiest-java-c-c-solution-with-approach-yn66j
Intuition\n Describe your first thoughts on how to solve this problem. \n- My first thought is to first calculate sum of digits of element the compare its value
jeeleej
NORMAL
2024-09-29T08:12:38.578848+00:00
2024-09-29T08:12:38.578882+00:00
61
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n- My first thought is to first calculate sum of digits of element the compare its value with previous minimum number.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Initialize three *int* `n=nums.length`, `sum` is for to calculate sum of digits of given element, in Java `mimum=Integer.MAX_VALUE`(in C++ `minimum=INT_MAX`) for to give maximum value of *int* data type.\n2. Start one `for` from *index=0* to *index<n* and everytime initialize `sum=0` for every element the sum of its digits starts from *0*.\n3. Use one `while` loop for to calculate sum of digits its use until `nums[index]!=0`, after loop check that which is smallest value `sum` or `minimum` and give it to `minimum`.\n4. At the end return `minimum`.\n\n# Complexity\n- Time complexity: $$O(n*log(M))$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n \n- Space complexity: $$O(1)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java []\nclass Solution {\n public int minElement(int[] nums) {\n int n=nums.length;\n int sum, minimum=Integer.MAX_VALUE;\n\n for(int i=0; i<n; i++){\n sum=0;\n\n while(nums[i]!=0){\n sum=sum+nums[i]%10;\n nums[i]=nums[i]/10;\n }\n\n minimum=Math.min(sum, minimum);\n }\n\n return minimum;\n }\n}\n```\n```C++ []\nclass Solution {\npublic:\n int minElement(vector<int>& nums) {\n int n=nums.size();\n int sum, minimum=INT_MAX;\n\n for(int i=0; i<n; i++){\n sum=0;\n\n while(nums[i]!=0){\n sum=sum+nums[i]%10;\n nums[i]=nums[i]/10;\n }\n\n minimum=min(sum, minimum);\n }\n\n return minimum;\n }\n};\n```\n```C []\nint minElement(int* nums, int numsSize) {\n int sum, minimum=INT_MAX;\n\n for(int i=0; i<numsSize; i++){\n sum=0;\n\n while(nums[i]!=0){\n sum=sum+nums[i]%10;\n nums[i]=nums[i]/10;\n }\n\n if(sum<minimum)\n minimum=sum;\n }\n\n return minimum;\n}\n```\n
1
0
['C', 'C++', 'Java']
0
minimum-element-after-replacement-with-digit-sum
Transform-Reduce
transform-reduce-by-votrubac-gm5i
C++\ncpp\nint minElement(vector<int>& nums) {\n return transform_reduce(begin(nums), end(nums), INT_MAX, [](int a, int b){ return min(a, b); }, [](int n) {\n
votrubac
NORMAL
2024-09-29T06:54:37.015049+00:00
2024-09-29T06:54:37.015076+00:00
84
false
**C++**\n```cpp\nint minElement(vector<int>& nums) {\n return transform_reduce(begin(nums), end(nums), INT_MAX, [](int a, int b){ return min(a, b); }, [](int n) {\n int sum = 0;\n for (; n; n /= 10)\n sum += n % 10;\n return sum;\n });\n}\n```
1
0
['C']
0
minimum-element-after-replacement-with-digit-sum
Simple Brute force Approach .
simple-brute-force-approach-by-sainathv-dol8
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
sainathv
NORMAL
2024-09-29T04:45:18.906671+00:00
2024-09-29T04:45:18.906696+00:00
4
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(nlogn)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java []\nclass Solution {\n public int minElement(int[] nums) {\n int n = nums.length;\n for(int i=0; i<n; i++){\n int rep = nums[i];\n nums[i] = digitsum(rep);\n }\n Arrays.sort(nums);\n return nums[0];\n }\n public static int digitsum(int num){\n int sum = 0;\n while(num != 0){\n int res = num % 10;\n sum += res;\n num /= 10;\n }\n return sum;\n }\n}\n```
1
0
['Array', 'Java']
0
minimum-element-after-replacement-with-digit-sum
๐Ÿ’ฏVery Easy Brute Force Solution || ๐ŸŽฏ Array and Maths
very-easy-brute-force-solution-array-and-aq3x
Intuition\n Describe your first thoughts on how to solve this problem. \nWe will compute the sum of the digits of each elements of the array and replace it with
chaturvedialok44
NORMAL
2024-09-29T04:01:21.345268+00:00
2024-09-29T04:01:21.345298+00:00
94
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe will compute the sum of the digits of each elements of the array and replace it with the corresponding element, then get the minimum of this computed array.\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. **Helper Function sum(int n):**\n - The function takes an integer \'n\' and computes the sum of its digits.\n - It does this by extracting each digit (\'n % 10\') and adding it to the \'total\' while dividing \'n by 10\' in each iteration to move to the next digit.\n - This process continues until \'n becomes 0\'.\n\n2. **Main Function minElement(int[] nums):**\n - Step 1: The input is an array nums of integers. For each element in the array, the sum of its digits is calculated using the \'sum()\' function, and the array element is replaced with this \'sum\'.\n - Step 2: Once the sums of digits replace the elements in \'nums\', the code searches for the \'minimum\' value in the modified array.\n - Step 3: The function returns the \'smallest sum of digits\' from the array.\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n$$O(n*logk)$$, where k is the maximum number in the nums.\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n$$O(1)$$.\n# Code\n```java []\nclass Solution {\n public int sum(int n){\n int total = 0;\n while(n > 0){\n total += n%10;\n n = n/10;\n }\n return total;\n }\n public int minElement(int[] nums) {\n int n = nums.length;\n for(int i=0; i<n; i++){\n int a = sum(nums[i]);\n nums[i] = a;\n }\n \n int min = Integer.MAX_VALUE;\n for(int i=0; i<n; i++){\n min = Math.min(min, nums[i]);\n }\n return min;\n }\n}\n```
1
0
['Array', 'Math', 'Java']
1
minimum-element-after-replacement-with-digit-sum
Easy cpp solution || 100% Fast
easy-cpp-solution-100-fast-by-marlboro_m-kwsf
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
Marlboro_Man_10
NORMAL
2024-09-28T22:19:10.072497+00:00
2024-09-28T22:19:10.072521+00:00
12
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:O(32*N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```cpp []\nclass Solution {\n int digitSum(int n){\n string s= to_string(n);\n int sum=0;\n for(auto it:s){\n sum+=(it-\'0\');\n }\n return sum;\n }\npublic:\n int minElement(vector<int>& nums) {\n int ans =1e9;\n\n for(auto it: nums){\n ans = min(ans, digitSum(it));\n }\n return ans;\n \n }\n};\n```
1
0
['C++']
0
minimum-element-after-replacement-with-digit-sum
Easy solution beats 98% ๐Ÿ’€๐Ÿ”ฅ๐Ÿ”ฅ๐Ÿ”ฅ๐Ÿ“๐Ÿ“
easy-solution-beats-98-by-l4nc3l07-y4pn
Approach\n Describe your approach to solving the problem. \nVery easy problem. The only "challenge" is to find the digits which can be done easily by the algori
L4nc3l07
NORMAL
2024-09-28T16:03:19.841862+00:00
2024-09-28T16:03:19.841902+00:00
184
false
# Approach\n<!-- Describe your approach to solving the problem. -->\nVery easy problem. The only "challenge" is to find the digits which can be done easily by the algorithm bellow. \n\n# Code\n```python []\nclass Solution(object):\n def minElement(self, nums):\n """\n :type nums: List[int]\n :rtype: int\n """\n length = len(nums)\n for i in range(length):\n num = nums[i]\n sum = 0\n while num > 0:\n mod = num % 10\n num = num // 10\n sum += mod\n nums[i] = sum\n return min(nums)\n```
1
0
['Python']
2
minimum-element-after-replacement-with-digit-sum
โœ…Simple C++ Solution
simple-c-solution-by-lokesh8577-4zsw
IntuitionApproachComplexity Time complexity: Space complexity: Code
Lokesh8577
NORMAL
2025-04-12T05:38:08.331970+00:00
2025-04-12T05:38:08.331970+00:00
1
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```cpp [] class Solution { public: int minElement(vector<int>& nums) { int min=INT_MAX; for(int i=0;i<nums.size();i++){ int sum=0; while(nums[i]!=0){ int digit=nums[i]%10; sum+=digit; nums[i]/=10; } if(sum<min){ min = sum; } } return min; } }; ```
0
0
['C++']
0
minimum-element-after-replacement-with-digit-sum
PYTHON || BEATS 100 % || EASY FOR BEGINNERS || SPACE COMPLEXITY || TIME COMPLEXITY
python-beats-100-easy-for-beginners-spac-a84i
IntuitionApproachComplexity Time complexity: Space complexity: Code
Tharun-Varshan-S
NORMAL
2025-04-09T08:49:40.474947+00:00
2025-04-09T08:49:40.474947+00:00
1
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```cpp [] class Solution { public: int minElement(vector<int>& nums) { for (int i=0;i<nums.size();i++){ string no=to_string(nums[i]); int sum=0; for (char j:no){ sum+=j-'0'; } nums[i]=sum; } int min=nums[0]; for (int h=1;h<nums.size();h++){ if (nums[h]<min){ min=nums[h]; } } return min; } }; ```
0
0
['C++']
0
minimum-element-after-replacement-with-digit-sum
beats 100% runtime using two loops (python)
beats-100-runtime-using-two-loops-python-8nu9
IntuitionApproachComplexity Time complexity: Space complexity: Code
dpasala
NORMAL
2025-04-09T05:32:17.455354+00:00
2025-04-09T05:32:17.455354+00:00
2
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```python3 [] class Solution: def minElement(self, nums: List[int]) -> int: mn = 10000000 for n in nums: total = 0 while n > 0: total += n % 10 n = n // 10 mn = min(mn, total) return mn ```
0
0
['Python3']
0
minimum-element-after-replacement-with-digit-sum
C++ | O(N) solution
c-on-solution-by-serrabassaoriol-as0o
IntuitionPretty much, just do what the problem tells you. There is no room for optimizations. Iterate the input Calculate sum of digits for current number If i
serrabassaoriol
NORMAL
2025-04-08T19:21:59.928552+00:00
2025-04-08T19:24:34.617606+00:00
2
false
# Intuition Pretty much, just do what the problem tells you. There is no room for optimizations. 1. Iterate the input 2. Calculate sum of digits for current number - If input numbers had more digits, or the input had strings containing numbers, we could consider terminating early the sum of digits whenever it goes above the minimum sum of digits 3. Update your minimum sum of digits if current sum is lower # Complexity - Time complexity: O(5 * N) = O(N) - Have to iterate all numbers in input - Each loop iteration takes about 5 operations since each number can have up to 5 digits as stated in the problem's constraint - Space complexity: O(1) - No additional space required, we can solve this problem with just a few state variables # Code ```cpp [] #include <vector> #include <cstdint> #include <algorithm> class Solution { public: uint8_t digit_sum(int32_t number) { uint8_t sum = 0; while (number != 0) { sum += number % 10; number /= 10; } return sum; } int minElement(vector<int>& nums) { const uint8_t maximum_digits = 5; const uint8_t maximum_sum = 9 * maximum_digits; uint8_t minimum_sum = maximum_sum; for (const int32_t& number : nums) { minimum_sum = std::min( minimum_sum, this->digit_sum(number) ); } return minimum_sum; } }; ```
0
0
['C++']
0
minimum-element-after-replacement-with-digit-sum
easiest java solution
easiest-java-solution-by-dpasala-rcrh
IntuitionApproachComplexity Time complexity: Space complexity: Code
dpasala
NORMAL
2025-04-08T18:27:08.817470+00:00
2025-04-08T18:27:08.817470+00:00
1
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```java [] class Solution { public int minElement(int[] nums) { int min = Integer.MAX_VALUE; for (int n : nums) { int total = 0; while (n > 0) { total += n % 10; n /= 10; } min = Math.min(min, total); } return min; } } ```
0
0
['Java']
0
minimum-element-after-replacement-with-digit-sum
Minimum Element After Replacement With Digit Sum
minimum-element-after-replacement-with-d-hx47
IntuitionApproachComplexity Time complexity: Space complexity: Code
CSE_4039_SATHISH
NORMAL
2025-04-08T15:23:20.556942+00:00
2025-04-08T15:23:20.556942+00:00
1
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```java [] class Solution { public int minElement(int[] nums) { int min = Integer.MAX_VALUE; for(int i=0; i<nums.length; i++) { int num = nums[i]; int r = 0; while(num != 0) { int d = num%10; r += d; num /= 10; } if(r < min) min = r; } return min ; } } ```
0
0
['Java']
0
minimum-element-after-replacement-with-digit-sum
C++ Solution
c-solution-by-malseji-hqb8
IntuitionApproachComplexity Time complexity: Space complexity: Code
malseji
NORMAL
2025-04-07T19:41:54.322750+00:00
2025-04-07T19:41:54.322750+00:00
1
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```cpp [] class Solution { public: int minElement(vector<int>& nums) { for(int i=0;i<nums.size();i++) { int sum=0; while(nums[i]>0) { sum +=nums[i]%10; nums[i] /=10; } nums[i] = sum; } return *min_element(nums.begin(),nums.end()); } }; ```
0
0
['C++']
0
minimum-element-after-replacement-with-digit-sum
BEATS 100% | Easy java solution | Simple approach
beats-100-easy-java-solution-simple-appr-a2fc
IntuitionThe minElement method loops through an integer array.For each number, it calculates the sum of its digits using getDigitSum.It keeps track of the minim
geetaseshapalli
NORMAL
2025-04-05T12:03:43.146473+00:00
2025-04-05T12:03:43.146473+00:00
4
false
# Intuition The minElement method loops through an integer array. For each number, it calculates the sum of its digits using getDigitSum. It keeps track of the minimum digit sum found. Returns the smallest digit sum among all numbers in the array. # Complexity - Time complexity: minElement() loops n times, and each getDigitSum() call runs in O(d). So, Time Complexity = O(n * d) ~ O(n) - Space complexity: No extra space used except a few variables. O(1) # Code ```java [] class Solution { public int minElement(int[] nums) { int min = Integer.MAX_VALUE; for (int num : nums) { int result = getDigitSum(num); min = Math.min(min, result); } return min; } public int getDigitSum(int number) { int sum = 0; while (number != 0) { sum += number % 10; number /= 10; } return sum; } } ```
0
0
['Java']
0
minimum-element-after-replacement-with-digit-sum
Minimum Element After Replacement with digit sum
minimum-element-after-replacement-with-d-8agg
Great! Let's break down your code step by step to understand the intuition, approach, and time complexity.โœ… Problem Intuition:Your code is solving a modified mi
_PriyanshuUpadhyay_23
NORMAL
2025-04-05T06:43:45.427307+00:00
2025-04-05T06:43:45.427307+00:00
2
false
Great! Let's break down your code step by step to understand the **intuition, approach, and time complexity**. --- ### โœ… **Problem Intuition:** Your code is solving a **modified minimum problem**: > From a given array `nums`, replace each element with the **sum of its digits**, and then return the **minimum** of these new values. ๐Ÿง  For example, If `nums = [123, 40, 9]`, then after processing: - 123 โ†’ 1+2+3 = 6 - 40 โ†’ 4+0 = 4 - 9 โ†’ 9 So final array = `[6, 4, 9]`, and **min = 4** --- ### ๐Ÿ” **Approach Explained:** 1. **Transform step**: - Iterate through each element in the array. - Replace each element with the **sum of its digits** using the `add()` method. 2. **Minimum finding step**: - Start with `min = nums[0]`. - Iterate again through the array. - If any element is smaller than `min`, update `min`. 3. **Return the minimum value.** --- ### ๐Ÿ’ก Breakdown of `add()` method: ```java public int add(int n) { int sum = 0; while(n > 0) { int rem = n % 10; // extract last digit sum += rem; // add it to sum n /= 10; // remove last digit } return sum; } ``` This gives you the **digit sum** of a number. --- ### ๐Ÿงฎ **Time Complexity Analysis:** Let: - `n` = number of elements in the array - `d` = average number of digits in each number #### 1. Transform step: - Each number is processed digit by digit โ†’ `O(d)` - For `n` numbers: total = `O(n * d)` #### 2. Finding minimum: - One pass over `n` elements โ†’ `O(n)` โœ… So total **Time Complexity = O(n * d)** If numbers are small (e.g. within 32-bit int), `d` is bounded, so you can consider this โ‰ˆ **O(n)**. --- ### ๐Ÿ“ฆ **Space Complexity:** - Only using a few variables: `sum`, `min`, etc. - โœ… **Space Complexity = O(1)** (constant extra space) --- ### โœ… Summary: | Part | Description | |-------------------|-----------------------------------------| | **Purpose** | Find min of digit-summed array | | **Steps** | Digit-sum each element โ†’ find minimum | | **Time Complexity** | `O(n * d)` โ†’ โ‰ˆ `O(n)` for small numbers | | **Space Complexity** | `O(1)` (in-place operations) | Let me know if you want to optimize this or modify it for a different case! # Code ```java [] class Solution { public int minElement(int[] nums) { for(int i=0;i<nums.length;i++){ nums[i]=add(nums[i]); } int min=nums[0]; for(int i=0;i<nums.length;i++){ if(nums[i]<min) min=nums[i]; }return min; }public int add(int n){ int sum=0; while(n>0){ int rem=n%10; sum+=rem; n/=10; }return sum; } } ```
0
0
['Java']
0
minimum-element-after-replacement-with-digit-sum
Minimum element
minimum-element-by-nylrem-w96h
IntuitionWe are given an array of integers, and we want to find the number with the smallest digit sum. Instead of comparing the original numbers, we convert ea
Nylrem
NORMAL
2025-04-04T18:25:51.686638+00:00
2025-04-04T18:25:51.686638+00:00
1
false
# Intuition We are given an array of integers, and we want to find the number with the **smallest digit sum**. Instead of comparing the original numbers, we convert each number to the sum of its digits and find the minimum among those. --- # Approach 1. Loop through the array. 2. For each number, convert it to a string to access each digit. 3. Convert each digit character back to an integer and compute the sum of its digits. 4. Replace the original number in the array with this digit sum. 5. Use Javaโ€™s built-in `Arrays.stream().min().getAsInt()` to find the minimum digit sum. --- # Complexity - **Time complexity:** $$O(n \cdot d)$$ where \( n \) is the number of elements in the array, and \( d \) is the average number of digits per element. - **Space complexity:** $$O(1)$$ We are modifying the original array in-place and using only a few extra variables. --- # Code ```java import java.util.Arrays; class Solution { public int minElement(int[] nums) { for (int i = 0; i < nums.length; i++) { String numStr = String.valueOf(nums[i]); int temp = 0; for (int j = 0; j < numStr.length(); j++) { temp += Character.getNumericValue(numStr.charAt(j)); } nums[i] = temp; } return Arrays.stream(nums).min().getAsInt(); } }
0
0
['Java']
0
minimum-element-after-replacement-with-digit-sum
๐Ÿ” Minimum Digit Sum in an Array || Beats 100% || Java
minimum-digit-sum-in-an-array-beats-100-tln98
โœ… Intuition The goal is to find the minimum sum of digits for all numbers in an array. We iterate through each number, calculate its digit sum, and track the mi
solaimuthu
NORMAL
2025-04-04T18:15:23.690850+00:00
2025-04-04T18:15:23.690850+00:00
1
false
### โœ… **Intuition** - The goal is to **find the minimum sum of digits** for all numbers in an array. - We iterate through each number, calculate its **digit sum**, and track the **minimum**. --- ### ๐Ÿ”ฅ **Approach** 1. **Compute digit sum for each number**: - Extract each digit using `temp % 10`. - Add it to `digitSum`. - Remove the last digit using `temp /= 10`. - Store the computed digit sum in the array. 2. **Find the minimum digit sum**: - Iterate through the updated array and find the smallest value. --- ### โฑ๏ธ **Complexity Analysis** - **Time Complexity**: - **Digit Sum Calculation**: $$O(d)$$ per number (where `d` is the number of digits). - **Finding the Minimum**: $$O(n)$$ - **Total Complexity**: $$O(n \cdot d)$$ (for standard numbers, `d` is small, making it close to $$O(n)$$) - **Space Complexity**: $$O(1)$$ (in-place modification of `nums`) --- ### ๐Ÿ› ๏ธ **Code** ```java class Solution { public int minElement(int[] nums) { for (int i = 0; i < nums.length; i++){ int temp = nums[i], digitSum = 0; while (temp != 0){ digitSum += temp % 10; temp /= 10; } nums[i] = digitSum; // Replace number with its digit sum } int minSum = nums[0]; for (int num : nums){ if (num < minSum) minSum = num; } return minSum; } } ``` --- ### ๐Ÿ“ **Example Walkthrough** #### **Example 1** ```java Input: nums = [19, 82, 46] Output: 8 Explanation: - 19 โ†’ 1 + 9 = 10 - 82 โ†’ 8 + 2 = 10 - 46 โ†’ 4 + 6 = 8 (smallest) ``` #### **Example 2** ```java Input: nums = [55, 32, 11] Output: 2 Explanation: - 55 โ†’ 5 + 5 = 10 - 32 โ†’ 3 + 2 = 5 - 11 โ†’ 1 + 1 = 2 (smallest) ``` --- ### โœ… **Optimized & Clean Solution! ๐Ÿš€**
0
0
['Java']
0
minimum-element-after-replacement-with-digit-sum
JavaScript, 1 line
javascript-1-line-by-najwer23-ud5w
null
najwer23
NORMAL
2025-04-02T22:55:11.199856+00:00
2025-04-02T22:55:11.199856+00:00
4
false
```javascript [] /** * @param {number[]} nums * @return {number} */ var minElement = function(nums) { return +Math.min(...nums.map(x=>(''+x).split("").reduce((a,b) => a + +b, 0))) }; ```
0
0
['JavaScript']
0
minimum-element-after-replacement-with-digit-sum
Beats 100% | Simple short code
beats-100-simple-short-code-by-shwns-q0h0
IntuitionApproachComplexity Time complexity:O(n) Space complexity:O(1) Code
shwns
NORMAL
2025-03-30T17:11:11.131439+00:00
2025-03-30T17:11:11.131439+00:00
1
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity:O(n) <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity:O(1) <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```python3 [] class Solution: def minElement(self, nums: List[int]) -> int: def get_sum(n): res = 0 while n : res += n % 10 n = n//10 return res res = float("inf") for n in nums: res = min(res, get_sum(n)) return res ```
0
0
['Python3']
0
minimum-element-after-replacement-with-digit-sum
Java || Runtime - 100% || Memory - 87.98%
java-runtime-100-memory-8798-by-mohanraj-6glw
Complexity Time complexity: O(n) Space complexity: O(n) Code
Mohanraj-R
NORMAL
2025-03-30T13:14:08.413733+00:00
2025-03-30T13:14:08.413733+00:00
1
false
# Complexity - Time complexity: $$O(n)$$ <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: $$O(n)$$ <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```java [] class Solution { public int minElement(int[] nums) { int min = Integer.MAX_VALUE; for(int num : nums){ int sum = 0; while(num > 0){ int digit = num % 10; sum += digit; num/=10; } min = Math.min(min , sum); } return min; } } ```
0
0
['Array', 'Math', 'Java']
0
minimum-element-after-replacement-with-digit-sum
1 ms Beats 100.00%
1-ms-beats-10000-by-shoreshan-kxn3
IntuitionApproachComplexity Time complexity: Space complexity: Code
shoreshan
NORMAL
2025-03-30T07:40:53.465791+00:00
2025-03-30T07:40:53.465791+00:00
2
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```java [] class Solution { public int minElement(int[] nums) { int min = Integer.MAX_VALUE; for(int num : nums){ min = Math.min(min,digitSum(num)); } return min; } public int digitSum(int num){ int sum = 0; while (num > 0) { int digit = num % 10; sum += digit; num /= 10; } return sum; } } ```
0
0
['Java']
0
minimum-element-after-replacement-with-digit-sum
Beasts 100% || C++
beasts-100-c-by-jerry_82-dzkm
IntuitionApproachComplexity Time complexity: Space complexity: Code
Jerry_82
NORMAL
2025-03-27T17:05:20.384671+00:00
2025-03-27T17:05:20.384671+00:00
3
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```cpp [] class Solution { public: int solve(int &num) { int sum = 0; while(num > 0){ sum += num % 10; num /= 10; } return sum; } int minElement(vector<int>& nums) { int n = nums.size(); int ans = INT_MAX; for(int i=0; i<n; i++){ int num = solve(nums[i]); ans = min(num,ans); } return ans; } }; ```
0
0
['C++']
0