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
minimum-deletions-to-make-array-beautiful
✅cpp easy with intuition || O(N) Time, O(1) space
cpp-easy-with-intuition-on-time-o1-space-4gn3
```\nINTUITION\n1. since we have to find the minimum deletions, we dont have to \n\tactually delete the elements , we just have to count those elements.\n2. Now
namanvijay814
NORMAL
2022-03-27T04:08:38.169631+00:00
2022-03-27T04:37:23.114091+00:00
1,257
false
```\nINTUITION\n1. since we have to find the minimum deletions, we dont have to \n\tactually delete the elements , we just have to count those elements.\n2. Now if we delete the element and shift all the elements towards left , it will\n\tcause time limit exceeded.\n3. To handle above case we can observe one thing that, if we delete some element\n\tat a certain position then the indices of all the elements towards the right \n\twill get inverted means the odd index will become even and the even will become odd.\n4. In the end checking if the vector size if even or odd, if it is even simply return or else if it \n\tis odd then decrement result by 1 since we have to remove one element to have the vector size even.\nclass Solution {\npublic:\n int minDeletion(vector<int>& nums) {\n int res=0,n=nums.size(),i;\n int flag=1;\n for(i=0;i<nums.size()-1;i++){\n if(i%2==0 and nums[i]==nums[i+1] and flag){\n res++;\n flag=0;\n }\n else if(i%2==1 and nums[i]==nums[i+1] and flag==0){\n res++;\n flag=1;\n }\n }\n int x=n-res;\n if(x%2==0){\n return res;\n }\n return res+1;\n }\n};
19
1
['Greedy', 'C']
7
minimum-deletions-to-make-array-beautiful
C++ / Java || With explanation O(1) space
c-java-with-explanation-o1-space-by-kami-odif
\nclass Solution {\npublic:\n int minDeletion(vector<int>& nums) {\n \n int deletion(0), n(size(nums));\n \n for (int i=0; i<n-1;
kamisamaaaa
NORMAL
2022-03-27T04:01:34.169232+00:00
2022-03-27T05:14:03.265486+00:00
1,384
false
```\nclass Solution {\npublic:\n int minDeletion(vector<int>& nums) {\n \n int deletion(0), n(size(nums));\n \n for (int i=0; i<n-1; ) {\n int newIndex = i-deletion; // index will alter by number of deletions done.\n if ((newIndex % 2 == 0) and nums[i] == nums[i+1]) deletion++;\n else i++;\n }\n return ((n-deletion) % 2 == 0) ? deletion : deletion+1;\n // if size of array after removing satisfying 2nd condition is odd then we need to delete one more element to make it even.\n }\n};\n```\n\n**Java Solution**\n\n```\nclass Solution {\n public int minDeletion(int[] nums) {\n \n int deletion = 0, n = nums.length;\n \n for (int i=0; i<n-1; ) {\n int newIndex = i-deletion;\n if ((newIndex % 2 == 0) && nums[i] == nums[i+1]) deletion++;\n else i++;\n }\n return ((n-deletion) % 2 == 0) ? deletion : deletion+1;\n }\n}\n```\n**Upvote if it helps :)**\n
18
0
['C', 'Java']
4
minimum-deletions-to-make-array-beautiful
Python | Greedy
python-greedy-by-mikey98-s5gk
```\nclass Solution:\n def minDeletion(self, nums: List[int]) -> int:\n # Greedy !\n # we first only consider requirement 2: nums[i] != nums[i
Mikey98
NORMAL
2022-03-27T04:01:07.012650+00:00
2022-03-27T04:01:07.012678+00:00
1,287
false
```\nclass Solution:\n def minDeletion(self, nums: List[int]) -> int:\n # Greedy !\n # we first only consider requirement 2: nums[i] != nums[i + 1] for all i % 2 == 0\n # at the begining, we consider the num on the even index\n # when we delete a num, we need consider the num on the odd index\n # then repeat this process\n # at the end we check the requirement 1: nums.length is even or not\n \n n = len(nums)\n count = 0\n # flag is true then check the even index\n # flag is false then check the odd index\n flag = True\n \n for i in range(n):\n # check the even index\n if flag:\n if i % 2 == 0 and i != n -1 and nums[i] == nums[i + 1]:\n count += 1\n flag = False\n # check the odd index\n elif not flag:\n if i % 2 == 1 and i != n -1 and nums[i] == nums[i + 1]:\n count += 1\n flag = True\n \n curLength = n - count\n \n return count if curLength % 2 == 0 else count + 1
16
0
['Greedy', 'Python', 'Python3']
4
minimum-deletions-to-make-array-beautiful
C++ Two Pointers + Greedy O(N) Time O(1) Space
c-two-pointers-greedy-on-time-o1-space-b-tmbw
See my latest update in repo LeetCode\n\n## Solution 1. Two Pointers + Greedy\n\nWe can greedily find the even and odd pairs from left to right.\n\nWhy greedy?
lzl124631x
NORMAL
2022-03-27T04:01:29.777824+00:00
2022-03-27T06:33:45.490095+00:00
1,580
false
See my latest update in repo [LeetCode](https://github.com/lzl124631x/LeetCode)\n\n## Solution 1. Two Pointers + Greedy\n\nWe can greedily find the even and odd pairs from left to right.\n\n**Why greedy**? Think if the first two elements, if they are equal, we have to delete one of them -- deleting the rest of the numbers won\'t change anything to these first two equal numbers. So, when we see two equal numbers, we have to delete one of them, until we find two non-equal numbers.\n\nUse two pointers `i` the write pointer and `j` the read pointer.\n\nWe keep the following process until `j` exhausts the array.\n* Write `A[j]` to the even-indexed `A[i]` and increment both pointers.\n* Try to find the odd-indexed value. Keep skipping/deleting if `A[j] == A[i-1]`.\n* If found a valid odd-indexed value, write `A[j]` to `A[i]` and increment both pointers.\n\nLastly, if the resultant array has an odd length (i.e. `i % 2 != 0`), we delete the last element.\n\n```cpp\n// OJ: https://leetcode.com/problems/minimum-deletions-to-make-array-beautiful/\n// Author: github.com/lzl124631x\n// Time: O(N)\n// Space: O(1) extra space\nclass Solution {\npublic:\n int minDeletion(vector<int>& A) {\n int i = 0, N = A.size(), j = 0, ans = 0;\n while (j < N) {\n A[i++] = A[j++]; // Write `A[j]` to the even-indexed `A[i]`, increment both pointers\n while (j < N && A[i - 1] == A[j]) ++j, ++ans; // Trying to find the odd-indexed value. Keep skipping/deleting if `A[j] == A[i-1]`\n if (j < N) A[i++] = A[j++]; // If found, write `A[j]` to `A[i]` and increment both pointers\n }\n if (i % 2) ++ans; // If the resultant array is of odd length, delete the last element\n return ans;\n }\n};\n```\n\n## Solution 2. Greedy\n\nSimilar to Solution 1, just find non-equal pairs `A[i]` and `A[i + 1]`. If `A[i] == A[i + 1]`, delete `A[i]` and `++i`.\n\nIf in the end, there is no such pair, delete `A[i]`.\n\n```cpp\n// OJ: https://leetcode.com/problems/minimum-deletions-to-make-array-beautiful/\n// Author: github.com/lzl124631x\n// Time: O(N)\n// Space: O(1)\nclass Solution {\npublic:\n int minDeletion(vector<int>& A) {\n int i = 0, N = A.size(), ans = 0;\n for (; i < N; i += 2) {\n while (i + 1 < N && A[i] == A[i + 1]) ++i, ++ans; // delete `A[i]`\n if (i + 1 == N) ++ans; // can\'t find a pair, delete `A[i]`\n }\n return ans;\n }\n};\n```
14
1
[]
1
minimum-deletions-to-make-array-beautiful
C++ | 5-6 lines code | O(n) time, O(1) space
c-5-6-lines-code-on-time-o1-space-by-om_-5yjd
Time Complexity: O(n)\nSpace Complexity: O(1)\n``` \nint minDeletion(vector& nums) {\n int i=0, n=nums.size(), count=0;\n while(i<n){\n
om_1609
NORMAL
2022-03-27T04:30:06.857103+00:00
2022-03-27T06:33:19.780218+00:00
636
false
Time Complexity: O(n)\nSpace Complexity: O(1)\n``` \nint minDeletion(vector<int>& nums) {\n int i=0, n=nums.size(), count=0;\n while(i<n){\n while(i<n-1 && nums[i]==nums[i+1])i++, count++; \n i+=2; \n }\n\t\t//if length of remaining sequence\xA0 is odd remove last element \n if((n-count)&1)count++;\n\t\t\n return count;\n \n }\n\t
10
0
['Greedy', 'C']
0
minimum-deletions-to-make-array-beautiful
Python Simple Solution O(n) Time and O(1) Space
python-simple-solution-on-time-and-o1-sp-07rt
We iterate over nums from left to right and greedily choose elements which satisfy the given condition in our final array. While iterating, we need to keep a co
mrunankmistry52
NORMAL
2022-03-27T04:09:32.161938+00:00
2022-03-27T04:14:34.367914+00:00
634
false
We iterate over nums from left to right and **greedily choose elements** which satisfy the given condition in our final array. While iterating, we need to **keep a count** of how many elements we have chosen so far to make greedy choices.\n\n### Idea:\n1. Iterate from left to right uptil second last element:\n 1.1 If `count` is even and current element is same as next one, we delete it.\n 1.2 Otherise we take that element and increase `count`.\n2. Take the last element as it is and increment `count`.\n3. If our final `count` is odd, delete the last element as well.\n\n**Time Complexity:** *O(n)*\n**Space Complexity:** *O(1)*\n\n```\nclass Solution:\n def minDeletion(self, nums: List[int]) -> int:\n n = len(nums)\n dels = cnt = 0\n\n for i in range(n - 1):\n if (cnt % 2) == 0 and (nums[i] == nums[i + 1]):\n dels += 1\n else:\n cnt += 1\n \n cnt += 1 # Take the last element as it is.\n dels += cnt & 1 # If final count is odd, delete last element.\n return dels\n```\n\n***Do upvote if you like the solution. Have a nice day!***\n\n\n
9
0
['Greedy', 'Python', 'Python3']
1
minimum-deletions-to-make-array-beautiful
Simple Java Solution - One pass
simple-java-solution-one-pass-by-pavanku-yxx2
```\nclass Solution {\n public int minDeletion(int[] nums) {\n if(nums.length==0)\n return 0;\n Integer prev = null;\n int ci
pavankumarchaitanya
NORMAL
2022-03-27T04:05:27.901274+00:00
2022-03-27T04:05:27.901313+00:00
501
false
```\nclass Solution {\n public int minDeletion(int[] nums) {\n if(nums.length==0)\n return 0;\n Integer prev = null;\n int ci = 0;\n for(int i=0;i<nums.length;i++){\n \n if(prev!=null)\n {\n if(nums[i]!=prev){\n ci+=2;\n prev = null;\n } \n }else{\n prev = nums[i];\n }\n }\n return nums.length-ci;\n }\n}
8
1
[]
0
minimum-deletions-to-make-array-beautiful
✅ [C++] | EASY SOLUTION | TC : O(N) | SC : O(1)
c-easy-solution-tc-on-sc-o1-by-jaysudani-x3v6
\nclass Solution {\npublic:\n int minDeletion(vector<int>& nums) {\n \n int ans=0;\n int sz=nums.size();\n \n for(int i=0;
jaysudani
NORMAL
2022-03-29T04:04:05.246857+00:00
2022-03-29T06:31:17.258393+00:00
364
false
```\nclass Solution {\npublic:\n int minDeletion(vector<int>& nums) {\n \n int ans=0;\n int sz=nums.size();\n \n for(int i=0;i<sz-1;i++){\n //if effective index is even and it has same number at its index + 1 then we have to remove it \n if((i-ans)%2==0 and nums[i+1]==nums[i]){\n ans++;\n }\n \n }\n\t\t//if after deleting optimizely new size is odd then we have to remove one more element to make size even\n if((sz-ans)%2) ans++;\n return ans;\n \n }\n};\n```
7
0
['C++']
0
minimum-deletions-to-make-array-beautiful
o(n) solution Easy
on-solution-easy-by-sathish_mccall-w86h
Please upvote if you found the answer useful\nSo we have delete a value at i+1 if i is even and then nums[i]==nums[i+1],\nso according to that if we make one de
Sathish_McCall
NORMAL
2022-06-28T06:31:29.866702+00:00
2022-06-28T06:31:29.866749+00:00
271
false
**Please upvote if you found the answer useful**\nSo we have delete a value at i+1 if i is even and then nums[i]==nums[i+1],\nso according to that if we make one deletion all the indexes of the array will move ahead by 1,\nSo if we have n no of deletions, the elements of array will move ahead by n.\nAccording to that , we can understand the code.\n```\nclass Solution:\n def minDeletion(self, nums: List[int]) -> int:\n count=0\n i=0\n for i in range(0,len(nums)-1):\n if (i-count)%2==0 and nums[i]==nums[i+1]:\n count+=1\n return count+1 if (len(nums)-count)%2!=0 else count\n```
6
0
[]
0
minimum-deletions-to-make-array-beautiful
ANSWERED ALL QUES | SIMPLE | EXPLAINED ALL CONCEPTS
answered-all-ques-simple-explained-all-c-fnhf
Intuition\n Describe your first thoughts on how to solve this problem. \nSNOWBALL METHOD:\nAs we go through the nums array if we got valid element to remove we
prathmeshdeshpande101
NORMAL
2022-11-24T18:39:09.637333+00:00
2022-11-24T18:39:09.637382+00:00
715
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nSNOWBALL METHOD:\nAs we go through the nums array if we got valid element to remove we are going to increase the size of eleSize;\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nUsed SNOWBALL METHOD:\nif we got the element which needed to remove we will increase the eleSize by one and for next all position need to remove the nos of element which were previously removed as we go through this we get the removed element count;\n\nWHY i=i+2?\nif we needed to check if we needed to remove the element we needed to just jump by two as we are going to check only at even position\n\nWhy i--?\nWhenever we got the element we are going to increase the size of removed element and then our arr get shrink at that pos when this happens even position where we are going to check vaild or not element to remove also need to reduce by one;\n\nAfter all this if after removing all element size is not even we are going to increase the count 0f removed element as we needed to remove any element from any position\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(n)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(1)\n# Code\n```\nclass Solution {\n public int minDeletion(int[] nums) {\n int eleSize = 0;\n for(int i=0; i<nums.length-1; i = i+2){\n if((i-eleSize)%2 == 0 && (nums[i] == nums[i+1])){\n eleSize++;\n i--;\n }\n }\n if((nums.length - eleSize)%2 == 0)\n return eleSize;\n return eleSize +1;\n }\n}\n```
5
0
['Array', 'Two Pointers', 'Greedy', 'Sliding Window', 'Java']
1
minimum-deletions-to-make-array-beautiful
Easy To Understand || C++ || O(n) TIme
easy-to-understand-c-on-time-by-pranjal_-6gry
\n\n\nint minDeletion(vector<int>& nums) {\n int n=nums.size();\n int cnt=0, si=0;\n \n for(int i=0;i<n-1;i++) {\n if(num
pranjal_gaur
NORMAL
2022-03-27T07:12:40.665825+00:00
2022-03-27T18:30:18.161435+00:00
302
false
\n\n```\nint minDeletion(vector<int>& nums) {\n int n=nums.size();\n int cnt=0, si=0;\n \n for(int i=0;i<n-1;i++) {\n if(nums[i]==nums[i+1] && si%2==0) cnt++;\n else si++;\n }\n if(si%2==0) cnt++;\n \n return cnt;\n }\n```
5
0
['C']
0
minimum-deletions-to-make-array-beautiful
Time-O(n) Space-O(1) || Beginners friendly
time-on-space-o1-beginners-friendly-by-s-b9hr
\nclass Solution {\npublic:\n int minDeletion(vector<int>& nums) {\n int result=0;\n int i;\n for( i=0;i<nums.size()-1;)\n {\n
Shishir_Sharma
NORMAL
2022-03-27T04:03:57.513218+00:00
2022-03-27T06:16:09.479083+00:00
351
false
```\nclass Solution {\npublic:\n int minDeletion(vector<int>& nums) {\n int result=0;\n int i;\n for( i=0;i<nums.size()-1;)\n {\n \n if(nums[i]==nums[i+1])\n {\n i++;\n result++;\n }\n else\n {\n i+=2;\n \n }\n }\n if(i==nums.size()-1)\n {\n result++;\n \n }\n \n \n return result;\n }\n};\n```\n**Like it? Please Upvote ;-)**
5
0
['C', 'C++']
0
minimum-deletions-to-make-array-beautiful
Easy solution ✅✅
easy-solution-by-coder_96677-pqsp
\n# Code\n\n#include <iostream>\n#include <vector>\n\nclass Solution {\npublic:\n int minDeletion(vector<int>& nums) {\n if(nums.size()==1 ){\n
Coder_96677
NORMAL
2023-09-12T18:30:05.087033+00:00
2023-09-12T18:30:05.087057+00:00
493
false
\n# Code\n```\n#include <iostream>\n#include <vector>\n\nclass Solution {\npublic:\n int minDeletion(vector<int>& nums) {\n if(nums.size()==1 ){\n return 1;\n }\n int i=0;\n int count =0;\n \n while(i<nums.size()-1){\n if(nums[i]==nums[i+1]){\n count++;\n i++;\n }\n else{\n i+=2;\n } \n }\n if(i == nums.size()-1){\n count++;\n }\n return count; \n }\n};\n```
4
0
['C++']
0
minimum-deletions-to-make-array-beautiful
C++ || O(n) time O(1) space || Clean and Concise code
c-on-time-o1-space-clean-and-concise-cod-74dh
\n int minDeletion(vector<int>& nums) {\n int n=nums.size(),ans=0,i=0;\n while(i<n)\n {\n if(i<n-1 && nums[i]==nums[i+1]) ans
ashay028
NORMAL
2022-06-28T05:12:48.444691+00:00
2022-06-28T05:12:48.444736+00:00
189
false
```\n int minDeletion(vector<int>& nums) {\n int n=nums.size(),ans=0,i=0;\n while(i<n)\n {\n if(i<n-1 && nums[i]==nums[i+1]) ans++, i--;\n i+=2;\n }\n if((n-ans)%2) ans++;\n return ans;\n }\n```
4
0
[]
1
minimum-deletions-to-make-array-beautiful
Java Solution with Intuition
java-solution-with-intuition-by-arcpri-f2qi
Intuition\n\nIf we analyse the problem a little, we\'ll see that its indirectly asking us to create a new array after the deletions. Since its not asking for an
arcpri
NORMAL
2022-03-31T21:01:56.123227+00:00
2022-03-31T21:02:33.270008+00:00
323
false
**Intuition**\n\nIf we analyse the problem a little, we\'ll see that its indirectly asking us to create a new array after the deletions. Since its not asking for an actual array to be returned, but rather the number of deletions, we need not create an actual array, we can just simulate its creation. \n\nFor this purpose, I am using `j` as the `starting index of the new array`. Before adding any value to my new array from `nums` by incrementing `j`, I first check whether index `j` is satisfying the second condition. In the end, we keep a length check to satisfy the first condition.\n\n```\n public int minDeletion(int[] nums) {\n int count = 0, j = 0;\n \n for (int i = 0; i < nums.length - 1; i++) { \n \n if (j % 2 == 0 && nums[i] == nums[i + 1]) {\n count++;\n } else {\n j++;\n }\n }\n \n // odd length array\n if ((j + 1) % 2 == 1) {\n count++;\n }\n \n return count;\n }\n```
4
0
['Java']
0
minimum-deletions-to-make-array-beautiful
C++/Python | Intuituitive | Easy to Understand | Two Pointers | O(n) Solution
cpython-intuituitive-easy-to-understand-l1e3k
Intuition\n We have to take care of new index formed after deletion so let\'s mantain another pointer j\n We have pointer i for traversing in the array\n As sta
TejPratap1
NORMAL
2022-03-27T10:44:14.176785+00:00
2022-03-27T11:01:23.163336+00:00
304
false
**Intuition**\n* We have to take care of new index formed after deletion so let\'s mantain another pointer j\n* We have pointer i for traversing in the array\n* As stated, if nums[i] == nums[i+1] and j%2 == 0 we have to perform deletion, as we are oerforming deletion size reduces so no need to increament j\n* At last after traversing whole array if size is odd then we have to perform one more deletion\n\n***C++***\n```\nclass Solution {\npublic:\n int minDeletion(vector<int>& nums) {\n int n = nums.size();\n int j = 0, i = 0;\n int del = 0;\n while(i < n) {\n if(i+1 < n && nums[i] == nums[i+1] && j%2 == 0) {\n ++del;\n } else {\n ++j;\n }\n ++i;\n }\n if(j%2 != 0)\n ++del;\n return del;\n }\n};\n```\n\n***Python***\n```\nclass Solution:\n def minDeletion(self, nums: List[int]) -> int:\n j = 0\n i = 0\n delete = 0\n n = len(nums)\n while i < n:\n if (i+1 < n and nums[i] == nums[i+1] and j%2 == 0) :\n delete += 1;\n else:\n j += 1;\n i += 1\n if j%2 != 0:\n delete += 1\n return delete\n \n```\n**If you find this helpful upvote it, if you have any doubt than comment**
4
0
['Two Pointers', 'C', 'Python']
0
minimum-deletions-to-make-array-beautiful
Intuition Easy (EVEN ODD)
intuition-easy-even-odd-by-iyershridhar-zlym
The main catch for the problem is nums.length should be even after all of the operation \nObserve a pattern \nafter the operation\n1 . nums.length is odd (5) -
iyershridhar
NORMAL
2022-03-27T04:58:38.928095+00:00
2022-03-27T05:07:53.543472+00:00
185
false
The main catch for the problem is nums.length should be even after all of the operation \nObserve a pattern \nafter the operation\n1 . nums.length is odd (5) -> 0 1 2 3 4\n2 . nums.length is even (6) -> 0 1 2 3 4 5\nnow lets suppose we have done some operation optimally and our ans vector size at this point of time is odd thats means we have a even index at the end so we need to make sure that the next element we are inserting in our ans vector should not be same as the last one\nand is its even we can simply insert the element because at the even size our last element will always be at odd index (we dont care about it).. and simply after traversing the whole array we have our ans if ans size is odd (total - ans.size()-1) else {total-ans.size()}\n\nSpace optimization : easy (try it yourself)\n```\nclass Solution {\npublic:\n int minDeletion(vector<int>& nums) {\n int n = nums.size();\n vector<int>ar;\n ar.push_back(nums[0]);\n for (int i = 1 ; i<n ; i++){\n if (ar.size()%2 == 1){\n if (ar.back() == nums[i]){continue;}\n else{\n ar.push_back(nums[i]);\n }\n }\n else{\n ar.push_back(nums[i]);\n }\n }\n if (ar.size()&1) ar.pop_back();\n return (n-(int)ar.size());\n }\n};\n```\n
4
0
['Greedy']
1
minimum-deletions-to-make-array-beautiful
Simple C++ Solution with comments - O(N) time, O(1) space
simple-c-solution-with-comments-on-time-9qdew
\nclass Solution {\npublic:\n int minDeletion(vector<int>& nums) {\n int n = nums.size();\n int vindex = 0, ans = 0; // vindex -> virtual inde
the_og_rises
NORMAL
2022-03-27T04:19:40.387722+00:00
2022-03-27T04:19:40.387750+00:00
132
false
```\nclass Solution {\npublic:\n int minDeletion(vector<int>& nums) {\n int n = nums.size();\n int vindex = 0, ans = 0; // vindex -> virtual index after deletion of the the element from array\n \n for(int i=0; i<n; i++){\n if(vindex%2==0){ // if vindex is even then only you need to check further conditions\n if(i+1!=n && nums[i] == nums[i+1]){\n ans++;\n vindex--; // after deleting the element vindex will shift left by one\n }\n }\n vindex++; // if deletion of the element is not there vindex will shift right by one\n }\n if((n-ans) % 2) ans++; // if after deletions array is of odd length, delete one more element\n return ans;\n }\n};\n```
4
0
['C']
0
minimum-deletions-to-make-array-beautiful
C++ | O(N) time O(1) space
c-on-time-o1-space-by-anugamsiddhartha-teca
\n\n\nclass Solution {\npublic:\n int minDeletion(vector<int>& nums) {\n int count=0;\n int n=nums.size();\n int dele=0;\n for(in
anugamsiddhartha
NORMAL
2022-03-27T04:12:01.544507+00:00
2022-03-27T04:12:01.544534+00:00
173
false
\n\n```\nclass Solution {\npublic:\n int minDeletion(vector<int>& nums) {\n int count=0;\n int n=nums.size();\n int dele=0;\n for(int i=0;i<nums.size()-1;i++)\n {\n if((i-dele)%2==0)\n {\n if(nums[i]==nums[i+1])\n {\n count++;\n dele++;\n }\n }\n }\n if((n-dele)%2)return count+1;\n return count;\n }\n};\n```
4
0
['C']
0
minimum-deletions-to-make-array-beautiful
C++ | easy to understood
c-easy-to-understood-by-smit3901-fw75
\tint minDeletion(vector& nums) {\n\t\t\tint size = nums.size();\n\t\t\tint cnt = 0;\n\t\t\tfor(int i=0;i< size-1;)\n\t\t\t{\n\t\t\t\tif(nums[i] == nums[i+1])\n
smit3901
NORMAL
2022-03-27T04:04:32.557822+00:00
2022-03-27T04:04:32.557859+00:00
230
false
\tint minDeletion(vector<int>& nums) {\n\t\t\tint size = nums.size();\n\t\t\tint cnt = 0;\n\t\t\tfor(int i=0;i< size-1;)\n\t\t\t{\n\t\t\t\tif(nums[i] == nums[i+1])\n\t\t\t\t{\n\t\t\t\t\ti++;\n\t\t\t\t\tcnt++;\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\ti += 2;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(size % 2 == 0 and cnt % 2 != 0)\n\t\t\t{\n\t\t\t\tcnt++;\n\t\t\t}\n\t\t\tif(size % 2 == 1 and cnt % 2 == 0)\n\t\t\t{\n\t\t\t\tcnt++;\n\t\t\t}\n\t\t\treturn cnt;\n\t\t}
4
0
['C']
0
minimum-deletions-to-make-array-beautiful
✅ C++ | 3 lines | explanation | O(n) time | O(1) space
c-3-lines-explanation-on-time-o1-space-b-t18w
As we do not need to sort the array, just compare the consecutive elements\n if they are equal, increment the ans\n else just skip\n last check would be, if the
arihantjain01
NORMAL
2022-03-27T04:01:07.163342+00:00
2022-06-22T09:50:43.973587+00:00
350
false
* As we do not need to sort the array, just compare the consecutive elements\n* if they are equal, increment the ```ans```\n* else just skip\n* last check would be, if the vector size is even or not\n* as ```ans = number of deletions```, ```size of vector - ans``` would mean the final vector size after deletions. if it is odd, increase ```ans```, as that would mean we deleted one more element to make size of ```nums``` even\n```\nclass Solution {\npublic:\n int minDeletion(vector<int>& nums) {\n int ans=0, n=nums.size();\n for(int i=0;i<n-1;i++) nums[i]==nums[i+1] ? ans++ : i++;\n return ans + ((n - ans) % 2);\n }\n};\n```
4
1
['C']
3
minimum-deletions-to-make-array-beautiful
C++ || super easy 4 lines;
c-super-easy-4-lines-by-shradhaydham24-17nd
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
shradhaydham24
NORMAL
2023-09-12T19:35:31.424120+00:00
2023-09-12T19:35:31.424147+00:00
102
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```\nclass Solution {\npublic:\n int minDeletion(vector<int>& nums) \n {\n int count=0;\n for(int i=0;i<nums.size()-1;i++)\n {\n if(nums[i]==nums[i+1]&&(i-count)%2==0)\n count++;\n } \n int ans=(nums.size()-count)%2;\n //ans%=2;\n return ans+count;\n }\n};\n```
3
0
['C++']
1
minimum-deletions-to-make-array-beautiful
Simple Python Solution with O(n) time complexity and O(1) space complexity
simple-python-solution-with-on-time-comp-jumn
\nclass Solution(object):\n def minDeletion(self, nums):\n """\n :type nums: List[int]\n :rtype: int\n """\n \n if
SumanthBharadwaj
NORMAL
2022-06-28T15:19:44.144213+00:00
2022-06-28T15:19:44.144259+00:00
183
false
```\nclass Solution(object):\n def minDeletion(self, nums):\n """\n :type nums: List[int]\n :rtype: int\n """\n \n if len(nums) > 1:\n# The \'pointer\' points to the left hand side array element in comparision ie nums[i]\n# The \'high\' points to the right hand side array element in comparision ie nums[i+1]\n# The \'del_count\' will store the elements deleted in the process\n pointer, high, del_count = 0, 1, 0\n \n while high < len(nums):\n if nums[pointer] == nums[high]:\n# Delete the right hand side element\n del_count += 1\n# Make the next element in the array as right hand side of the comparision. This high will always point at the odd index post deletion. Since actual deletion is not required, just moving the pointer\n high += 1\n \n \n else:\n# Increment the left hand side array element to next even index.\n pointer = high + 1\n# The high will point to the next element of the right hand of comparision. \n high += 2\n \n# Check if the upgraded array has even or odd length. If it is odd, then delete the last element. \n \n \n return del_count + ((len(nums) - del_count) % 2 == 1) \n \n elif len(nums) == 1: return 1\n \n else: return 0\n \n```\nComplexity:\nTime - O(n)\nSpace - O(1)
3
0
['Python']
1
minimum-deletions-to-make-array-beautiful
c++ solution || two approaches|| O(1) space
c-solution-two-approaches-o1-space-by-sr-2s80
Using stack :\n\nclass Solution {\npublic:\n int minDeletion(vector<int>& a) {\n stack<int>s;\n int n=a.size(),index=-1,count=0;\n for(i
srinidhi_kanugula
NORMAL
2022-06-16T14:03:08.832227+00:00
2022-06-16T14:04:15.619930+00:00
196
false
Using stack :\n```\nclass Solution {\npublic:\n int minDeletion(vector<int>& a) {\n stack<int>s;\n int n=a.size(),index=-1,count=0;\n for(int i=0;i<n;i++){\n if(!s.empty() && a[i]==s.top() && index%2==0){\n count++;\n }\n else{\n index++;\n s.push(a[i]);\n }\n }\n if(index%2==0)\n count++;\n return count;\n }\n};\n```\nUsing two pointer .. O(1) space:\n```\nclass Solution{\npublic:\nint minDeletion(vector<int>&a){\nint n=a.size();\nint count=0;\nfor(int i=0;i<n-1;i++){\n\t\tint index=i-count;\n\t// since the index keeps changing after every deletion \n\t\tif(a[i+1]==a[i] && index%2==0)\n\t\t\tcount++;\n}\n\t\tif((n-count)%2!=0)\n\t\t\tcount++;\n\t\treturn count;\n\t}\n};\n```
3
0
['Stack', 'C']
0
minimum-deletions-to-make-array-beautiful
3 Different Solutions [Simple, Greedy and Faster ]
3-different-solutions-simple-greedy-and-tofst
This solution is based on a approch that we need a pair who has distinct elements (That\'s what the ques saying basically)\n\'\'\'\n\n def minDeletion(self,
vi_ek
NORMAL
2022-04-15T06:36:20.556186+00:00
2022-04-15T06:36:20.556227+00:00
195
false
This solution is based on a approch that we need a pair who has distinct elements (That\'s what the ques saying basically)\n\'\'\'\n\n def minDeletion(self, nums: List[int]) -> int:\n ans =0 \n l=None\n for i in nums:\n if l is None:\n l=i\n elif l!=i:\n l=None\n ans+=2\n return len(nums)-ans\n\'\'\'\n#This is a Stack solution where we append the first value and second value(only if it is different)\n\'\'\'\n\n def minDeletion(self, nums: List[int]) -> int:\n stack=[]\n l=len(nums)\n \n for i in nums:\n if len(stack)%2==0:\n stack.append(i)\n elif stack[-1]!=i:\n stack.append(i)\n if len(stack)%2==0:\n return l-len(stack)\n else:\n return l-len(stack)+1\n\'\'\'\n#In This we take a shift index that tells us the index of element if we delete that element .\n\'\'\'\n \n\tdef minDeletion(self, nums: List[int]) -> int:\n count=0\n for i in range(len(nums)-1):\n shift_index=i-count\n if nums[i]==nums[i+1] and shift_index%2==0:\n count+=1\n if(len(nums)-count)%2==1:\n count+=1\n return count\n\'\'\'
3
0
['Greedy', 'C', 'Python']
0
minimum-deletions-to-make-array-beautiful
C++,Simple O(n) and O(1) solution
csimple-on-and-o1-solution-by-aashuchoud-d8kh
In this First of all we will check if the give array is empty or of size 1, if so then we will simply return 0;\nNow, for the real test cases...\nwe will mainta
aashuchoudhary52
NORMAL
2022-04-08T08:10:18.511877+00:00
2022-04-08T08:10:18.511904+00:00
56
false
In this First of all we will check if the give array is empty or of size 1, if so then we will simply return 0;\nNow, for the real test cases...\nwe will maintain a "K" which will store the no. of elements we will delete.\nNow for every index we will check for (i-k)%2 coz ,index of elements will be sifted to left when we delete any element so for that i-k will give us the new and correct position.\n\nand at last we just need to check wheather the size of our new array is even or not if not we will delete one more element and that it.\n\nhope you understand.\n\n```\nclass Solution {\npublic:\n int minDeletion(vector<int>& nums) {\n if(nums.size()==0 && nums.size()==1)\n {\n return 0;\n }\n int k=0;\n for(int i=0;i<nums.size()-1;i++)\n {\n if((i-k)%2==0)\n {\n if(nums[i]==nums[i+1])\n {\n k++;\n }\n }\n }\n if((nums.size()-k)%2!=0)\n {\n k++;\n }\n return k;\n }\n};\n```
3
0
['C', 'Iterator']
0
minimum-deletions-to-make-array-beautiful
Java - Simple , Easy and 100% faster O(N) Time O(1) Space
java-simple-easy-and-100-faster-on-time-1gsdp
If nums length is 0 , no need to delete so return 0.\nelse\nstart with 0 index and check if i and i+1 are less than n and if nums[i] & nums[i+1] are not equals
shivamchhn
NORMAL
2022-03-29T18:13:13.365001+00:00
2022-04-05T17:43:29.001561+00:00
233
false
If nums length is 0 , no need to delete so return 0.\nelse\nstart with 0 index and check if i and i+1 are less than n and if nums[i] & nums[i+1] are not equals we don\'t need to delete i or i+1 so make i = i+2 .\nif nums[i] & nums[i+1] are equals then we have to delete either i or i+1 . Instead of doing deleting just increase the ans by 1 and make i = i+1.\nIn the end, check if size after deleting is even or not and if even return ans and if odd return ans+1.\n\nExample:\nConsider nums = [1,1,2,3,5]\n\nwhen i =0 & i+1 = 1 , both nums[i] & nums[i+1] are same so we need to delete any of them . Instead of directly deleting imagine you delete the number at i= 0, after that imagination your remaining elements [1,2,3,5] all will move leftwards by 1 index . Instead of wasting operation on deletion just increase your i by 1 as now element at index i=1 is the new start for comparision ( nums[i] != nums[i+1] ) .\n\n```\nclass Solution {\n public int minDeletion(int[] nums) {\n \n if(nums.length == 0)\n return 0;\n \n int n = nums.length;\n int i = 0;\n int ans = 0;\n \n while( i < n && i+1 < n)\n {\n if(nums[i] != nums[i+1])\n i = i+2;\n else\n {\n i++;\n ans++;\n }\n }\n \n if( (n - ans) %2 == 0)\n return ans;\n else\n return ans+1;\n }\n}\n\n```
3
0
['Greedy', 'Java']
2
minimum-deletions-to-make-array-beautiful
✅C++ | O(N) To O(1) Space
c-on-to-o1-space-by-shivam242424-sker
Solution 1: Using Stack Time Complexity : O(N) Space Complexity : O(N). Solution 2 : Optimal in Space Time Complexity : O(N) Space Complexity : O(1) If U li
shivam242424
NORMAL
2022-03-28T13:56:23.014100+00:00
2025-03-24T06:51:41.619883+00:00
58
false
# Solution 1: Using **Stack** ``` C++ [] class Solution { public: int minDeletion(vector<int>& nums) { int del = 0, i = 0, n = nums.size(); stack<pair<int, int>> st; // pair stores prev value and index after shifting while(i < n) { if(!st.empty()) { // index is even and prev and curr value same // increment deletion and pop prev element if(st.top().second % 2 == 0 && st.top().first == nums[i]) { del++; st.pop(); } } st.push({nums[i], i - del}); i++; } // if size is odd , increment del by 1 return del + (st.size() % 2) ; } }; ``` - Time Complexity : O(N) - Space Complexity : O(N). --- # Solution 2 : Optimal in Space ``` C++ [] class Solution { public: int minDeletion(vector<int> &nums) { int del = 0, n = nums.size(); for (int i = 0; i < n - 1; i++) { if ((i - del) % 2 == 0 && nums[i] == nums[i + 1]) del++; } return del + (n - del) % 2; } }; ``` - Time Complexity : O(N) - Space Complexity : **O(1)** # If U like solution please UPVOTE 💻 If there are any suggestions/questions in my post, comment below 👇
3
0
['C']
0
minimum-deletions-to-make-array-beautiful
[Typescript] Readable Solution with Code Documentation
typescript-readable-solution-with-code-d-677r
\n// Leetcode: https://leetcode.com/problems/minimum-deletions-to-make-array-beautiful/\n\nvar minDeletion = (nums: number[]): number => {\n let deletions =
hardanish-singh
NORMAL
2022-03-28T11:16:16.098894+00:00
2024-07-05T18:17:04.609925+00:00
220
false
```\n// Leetcode: https://leetcode.com/problems/minimum-deletions-to-make-array-beautiful/\n\nvar minDeletion = (nums: number[]): number => {\n let deletions = 0;\n\n for (let i = 0; i < nums.length; i += 2) {\n if (nums[i] === nums[i + 1]) {\n i--;\n deletions++;\n }\n }\n\n return ((nums.length - deletions) % 2) !== 0 ? deletions + 1 : deletions;\n};\n\n```
3
0
['TypeScript']
2
minimum-deletions-to-make-array-beautiful
C++ || Simple & Easy Code || 3 lines || Explained
c-simple-easy-code-3-lines-explained-by-74xsm
\nclass Solution {\npublic:\n int minDeletion(vector<int>& nums) {\n int cnt=0;\n for(int i=0;i<nums.size()-1;i++){\n if(nums[i] ==
agrasthnaman
NORMAL
2022-03-27T20:23:44.194129+00:00
2022-03-27T20:23:44.194171+00:00
207
false
```\nclass Solution {\npublic:\n int minDeletion(vector<int>& nums) {\n int cnt=0;\n for(int i=0;i<nums.size()-1;i++){\n if(nums[i] == nums[i+1] && (i-cnt)%2 == 0){cnt++;} \n //just check for nums[i] != nums[i + 1] for all i % 2 == 0\n // (i - cnt) is the index of element after deletion of the elements \n }\n return cnt + (nums.size()-cnt)%2; \n // now cnt is the count of elements removed from nums, so (nums.size()-cnt) gives the size of final array after deletion.\n // (nums.size()-cnt) % 2 will check is the final array size is even or not, if its even it will add 1 to the final as \n // otherwise it will add 0 \n }\n};\n```\nDo upvote if it helped :)\n
3
0
['Greedy', 'C', 'C++']
0
minimum-deletions-to-make-array-beautiful
Easy C++ solution
easy-c-solution-by-rajsaurabh-vcuf
``` \nint minDeletion(vector& v)\n{ \n int c = 0;\n int n = v.size();\n for (int i = 0; i <n-1 ; i++)\n if (v[i] == v[i + 1]
rajsaurabh
NORMAL
2022-03-27T05:37:30.505515+00:00
2022-03-27T05:38:16.405999+00:00
44
false
``` \nint minDeletion(vector<int>& v)\n{ \n int c = 0;\n int n = v.size();\n for (int i = 0; i <n-1 ; i++)\n if (v[i] == v[i + 1] && i% 2 == c%2 ) c++;\n \n return c + (n- c) % 2;\n }\n\t
3
0
[]
2
minimum-deletions-to-make-array-beautiful
C++ | O(n) solution with comments
c-on-solution-with-comments-by-ashutosht-vc1t
We just need to change the index after deletions to get the new index value in every iteration.\nUPVOTE if you like it!!\n\n\nclass Solution {\npublic:\n in
ashutosht1845
NORMAL
2022-03-27T05:30:36.853305+00:00
2022-03-27T06:01:02.865051+00:00
48
false
We just need to change the index after deletions to get the new index value in every iteration.\nUPVOTE if you like it!!\n\n```\nclass Solution {\npublic:\n int minDeletion(vector<int>& nums) {\n if(nums.size()==1) return 1;\n \n int count=0;\n int n=nums.size();\n \n for(int i=0; i<n-1; i++)\n {\n\t\t//since after every deletion numbers should shift backward \n\t\t//to avoid shifting we only consider their newIndex to find out whether they are \n\t\t//odd or even in further iterations\n int newIndex = i-count;\n if(newIndex%2 == 0 && nums[i]==nums[i+1])\n count++;\n }\n\t\t\n\t\t//if remaining length is even then print count else delete one more element so count-1\n if((n-count)%2 == 0)\n return count;\n else\n return count+1;\n }\n};\n```
3
0
['C']
0
minimum-deletions-to-make-array-beautiful
C++ || O(n) time O(1) space || commented solution
c-on-time-o1-space-commented-solution-by-s901
\'\'\'\n\n int minDeletion(vector& nums) {\n int n= nums.size();\n int n1=n;\n int i=0, j=0;\n int cnt=0;\n\t\t\n // j is
Pain01
NORMAL
2022-03-27T04:36:19.608987+00:00
2022-03-27T04:36:19.609026+00:00
129
false
\'\'\'\n\n int minDeletion(vector<int>& nums) {\n int n= nums.size();\n int n1=n;\n int i=0, j=0;\n int cnt=0;\n\t\t\n // j is virtual index after removing ith element\n while(i<n-1 && j<n1){\n if(j%2==0 && nums[i]==nums[i+1])\n {\n n1--; // new size after deleting elements\n cnt++; //counting deleted elements\n }\n else\n {\n j++;\n }\n i++;\n }\n // if the array size after deleting is odd the removing last element to make it even\n if(n1%2!=0){\n return cnt+1;\n }\n \n return cnt;\n }\n\t
3
0
['Greedy', 'C']
4
minimum-deletions-to-make-array-beautiful
JAVA | O(1) space | Easy to Understand
java-o1-space-easy-to-understand-by-chai-atd6
\nclass Solution {\n public int minDeletion(int[] nums) {\n \n int i = 0, n = nums.length;\n int currIndex = 0;\n if(nums.length=
Chaitanya1706
NORMAL
2022-03-27T04:02:45.617597+00:00
2022-03-27T04:02:45.617647+00:00
148
false
```\nclass Solution {\n public int minDeletion(int[] nums) {\n \n int i = 0, n = nums.length;\n int currIndex = 0;\n if(nums.length==1) return 1;\n int count = 0;\n while(i<n){\n if(currIndex%2==0){ // if index is even then delete all the continuous duplicate elements to the right of it\n \n int k = nums[i];\n while(i+1<nums.length && nums[i+1]==k){\n i++;\n count++;\n }\n \n }\n \n i++;\n currIndex++; // this is the virtual index of new array which will be created after shifting the elemnts to left\n \n \n }\n \n if(currIndex%2==0){ \n return count;\n }\n else{\n return count+1; // if the length of new array is odd then just delete last element to make length even\n }\n }\n}\n```
3
1
['Java']
0
minimum-deletions-to-make-array-beautiful
Easy understanding
easy-understanding-by-akshay3213-tf1m
All we have to is \n1. if (ind % 2 == 0) then nums[i] != nums[i + 1]\n2. final length of array must be even\n\n- For requirement 1,\nwe keep track of elements(i
akshay3213
NORMAL
2022-03-27T04:01:58.425748+00:00
2022-03-27T04:05:41.391769+00:00
286
false
All we have to is \n1. if (ind % 2 == 0) then nums[i] != nums[i + 1]\n2. final length of array must be even\n\n- For requirement 1,\nwe keep track of elements(index), we see if the 1st condition holds or not, if not remove them(deletion++).\n\n\n- For requirement 2,\nWe just have to check result array\'s length, if even - its fine, if not - deletion++;\n\nWe don\'t have to make result array here as we don\'t need it. We just need some information and that we can hold in variables. \nAs we are removing elements, we have to keep track of index in result array.\n\n**Understanding variables used in algo**\n\nindRes - As we are deleting element on going, we have to keep track of elements that we are keeping for final result, You can also take it as current index of result(beautiful) array as index\n\nind - index of given array\n\ndeletion - number of deletion, we are making\n\n```\n\nclass Solution {\n public int minDeletion(int[] nums) {\n int indRes = 0;\n int ind = 0; \n int deletion = 0;\n \n while (ind < nums.length - 1) {\n \n //check 1st condition\n if (indRes % 2 == 0) { //check what index is this in result array\n int next = ind + 1;\n while (next < nums.length && nums[ind] == nums[next]) {\n deletion++;\n next++;\n }\n ind = next;\n } else {\n ind++; //not doing anything for odd index\n }\n indRes++; //index in result array increses\n }\n \n \n //manage last elemets of given array\n if (ind < nums.length) {\n indRes++;\n }\n \n //2nd consition\n if (indRes % 2 != 0) {\n deletion++;\n }\n \n return deletion;\n }\n}\n```\n\nTC - O(N)\nSC - O(1)\n\nUpvote if you find this helpful.
3
1
[]
0
minimum-deletions-to-make-array-beautiful
Easy Java Solution || Stack
easy-java-solution-stack-by-ravikumar50-vong
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
ravikumar50
NORMAL
2024-05-02T20:33:01.359227+00:00
2024-05-02T20:33:01.359255+00:00
107
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```\nclass Solution {\n public int minDeletion(int[] arr) {\n int ans = 0;\n int n = arr.length;\n Stack<Integer> st = new Stack<>();\n\n for(int i=0; i<n; i++){\n if(st.size()==0) st.push(arr[i]);\n else{\n if(st.peek()==arr[i]) ans++;\n else st.pop();\n }\n }\n ans = ans+st.size();\n return ans;\n }\n}\n```
2
0
['Java']
0
minimum-deletions-to-make-array-beautiful
minimum-deletions-to-make-array-beautiful
minimum-deletions-to-make-array-beautifu-ppf9
Intuition\n Describe your first thoughts on how to solve this problem. \n- involves iterating through the input vector, checking adjacent elements for duplicate
TusharMaithani
NORMAL
2023-08-30T15:31:29.076294+00:00
2023-10-04T10:51:13.906915+00:00
52
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n- involves iterating through the input vector, checking adjacent elements for duplicates, and incrementing a deletion counter when necessary.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- OPTIMAL APPROACH:\nThe optimal force approach involves iterating through the input vector, checking adjacent elements for duplicates, and incrementing a deletion counter when necessary. This requires O(n) time, where n is the number of elements in the vector. However, due to the shifting of indices after deletions, the effective complexity could be worse.\n\n- BRUTE Approach:\nThe brute approach employs an unordered map to count occurrences of unique elements. It calculates deletions based on odd occurrence counts, eliminating the need for index adjustments. This solution has a linear O(n) time complexity for counting and requires O(u) space, where u is the number of unique elements in the vector. It is more efficient and avoids unnecessary index manipulations.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n- n\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n- 1\n\n# Code\n```\nclass Solution {\npublic:\n int minDeletion(vector<int>& nums) {\n\n int cnt=0; // Initialize a counter variable to keep track of deletions.\n\n // Loop through the vector until the second-to-last element.\n for(int i=0;i<nums.size()-1;i++)\n {\n // Check if the current index minus the count of deletions is even and\n // if the current element is the same as the next element.\n if((i-cnt)%2==0 && nums[i]==nums[i+1])\n {\n cnt++; // Increment the counter to indicate a deletion.\n }\n }\n\n // Check if the final count of elements after deletions is odd.\n // If it is odd, it means one more deletion is needed to make it even.\n if((nums.size()-cnt)%2)\n {\n cnt++; // Increment the counter.\n }\n\n return cnt; // Return the total count of deletions needed.\n }\n};\n\n```
2
0
['Array', 'Ordered Map', 'C++']
0
minimum-deletions-to-make-array-beautiful
C++ Time - O(N) Space - O(1)
c-time-on-space-o1-by-nayancode93-jn7q
Code\n\nclass Solution {\npublic:\n int minDeletion(vector<int>& nums) {\n vector<int>vec;\n int ans=0,s=nums.size(),j=0;\n if(s==1)\n
nayanCode93
NORMAL
2022-11-28T01:51:20.640021+00:00
2022-11-28T01:51:20.640049+00:00
872
false
# Code\n```\nclass Solution {\npublic:\n int minDeletion(vector<int>& nums) {\n vector<int>vec;\n int ans=0,s=nums.size(),j=0;\n if(s==1)\n return 1;\n \n for(int i=0;i<nums.size()-1;i++){\n if(j%2==0){\n if(nums[i]==nums[i+1]){\n ans++;\n s--;\n continue;\n }\n }\n j++;\n }\n\n if(s%2==1)\n ans++;\n\n return ans;\n }\n};\n```
2
0
['C++']
0
minimum-deletions-to-make-array-beautiful
Very basic C++ approach using flag
very-basic-c-approach-using-flag-by-fazi-ayu3
\nclass Solution {\npublic:\n int minDeletion(vector<int>& nums) {\n int c=0;\n bool flag=true;\n cout<<flag<<endl;\n for(int i=0
fazith
NORMAL
2022-07-25T07:21:29.112316+00:00
2022-07-25T07:21:29.112364+00:00
67
false
```\nclass Solution {\npublic:\n int minDeletion(vector<int>& nums) {\n int c=0;\n bool flag=true;\n cout<<flag<<endl;\n for(int i=0;i<nums.size()-1;i++){\n if(flag){\n if(nums[i]==nums[i+1]){c++;continue;}\n }\n flag=!flag;\n }\n if((nums.size()-c)%2)return c+1;\n return c;\n }\n};\n```
2
0
['C']
0
minimum-deletions-to-make-array-beautiful
[Python 3] Two Pointer Greedy Solution
python-3-two-pointer-greedy-solution-by-4u8xd
Approach:\nInitialise steps to 0.\nUse two pointers, one to iterate through the array and other to keep track of the index in the updated list after removing (a
hari19041
NORMAL
2022-04-01T14:08:18.795708+00:00
2022-04-01T14:08:18.795738+00:00
214
false
**Approach**:\nInitialise steps to 0.\nUse two pointers, one to iterate through the array and other to keep track of the index in the updated list after removing (as the indices will change because of shifting).\n\nIf the index is not divisible by 2, then increment the index by 1. Otherwise, taking care of an edge case of i=len(nums)-1 and if the element is equal to the next element, then increment steps by 1 and keep the index the same (as this element would be deleted), otherwise, increment the index by 1.\n\nIn the end, if the len of the updated list (index) is divisible by 2, then return steps, otherwise, we would need to delete another element and hence return steps+1.\n\n**Time Complexity**: O(n) - we iterate the list once\n**Space Complexity**: O(1) \n\n**DO UPVOTE IF YOU FOUND IT HELPFUL.**\n\n**Code**:\n```\nclass Solution:\n def minDeletion(self, nums: List[int]) -> int:\n i = index = steps = 0\n while i < len(nums):\n if index%2 != 0:\n index += 1\n else:\n if i == len(nums)-1:\n index += 1\n break\n if nums[i] == nums[i+1]:\n steps += 1\n else:\n index += 1\n i += 1\n \n return steps if index%2 == 0 else steps+1\n```
2
0
['Two Pointers', 'Greedy', 'Python', 'Python3']
0
minimum-deletions-to-make-array-beautiful
Very Easy Solution: 2 steps foward 1 step backward, with explanation on thinking process
very-easy-solution-2-steps-foward-1-step-02xg
\npublic int minDeletion(int[] nums) {\n\tint c = 0;\n\tint i = 1;\n\n\twhile (i < nums.length) {\n\t\tif (nums[i-1] == nums[i]) {\n\t\t\tc++;\n\t\t\ti--; // "
didado
NORMAL
2022-03-29T00:32:44.196407+00:00
2022-04-06T22:14:41.679652+00:00
43
false
```\npublic int minDeletion(int[] nums) {\n\tint c = 0;\n\tint i = 1;\n\n\twhile (i < nums.length) {\n\t\tif (nums[i-1] == nums[i]) {\n\t\t\tc++;\n\t\t\ti--; // "deleted" one element, to keep the check in pairs\n\t\t}\n\t\ti += 2;\n\t}\n\n\treturn (nums.length - c) % 2 == 0 ? c : c+1;\n}\n```\n===========================\nThinking process:\n\nMy first wrong draft was very straightforward:\n```\n public int minDeletion(int[] nums) {\n int c = 0;\n for (int i = 1; i < nums.length; i++) {\n if (nums[i-1] == nums[i])\n c++;\n }\n \n return (nums.length + c) % 2 == 0 ? c : c+1;\n }\n```\nI failed the case [1,1,2,2,3,3]. Expected answer is 2 but this code got 4.\nWhen `i = 1`, one of the 1 got deleted and it shifted rest of the elements forward, which become [1,2,2,3,3]. \nThe next optimal action is to remove one of the 3 at the end, which produces a "beautiful" [1,2,2,3].\nHowever [1,2,2,3] will still got delete by the my first draft code(the 2,2 case). At this point I realized I need to do comparison by pairs.
2
0
['Java']
1
minimum-deletions-to-make-array-beautiful
2216 | C++ | Easy O(N) solution
2216-c-easy-on-solution-by-yash2arma-iy0i
Code:\n\nclass Solution {\npublic:\n int minDeletion(vector<int>& nums) \n {\n int mini=0, i=0;\n \n //iterate till i is less than n-
Yash2arma
NORMAL
2022-03-28T06:37:11.140400+00:00
2022-03-28T06:37:11.140437+00:00
94
false
**Code:**\n```\nclass Solution {\npublic:\n int minDeletion(vector<int>& nums) \n {\n int mini=0, i=0;\n \n //iterate till i is less than n-1 index\n while(i<nums.size()-1)\n {\n //(i%2+min)==0 to check whether we are at even index or odd\n if((i%2+mini)==0 && nums[i] == nums[i+1])\n {\n mini++;\n i++;\n }\n //when nums[i] != nums[i+1] we just jump to the next even index by incrementing i by 2.\n else\n i += 2;\n }\n \n //if final nums size is odd we remove last element and increase mini by 1, to make it even.\n return (nums.size()-mini) % 2 ? mini+1 : mini;\n \n }\n};\n```
2
0
['C']
0
minimum-deletions-to-make-array-beautiful
🔥 O(1) space | easy java solution with explanation ✅
o1-space-easy-java-solution-with-explana-m85c
\nHere to Make the array Beautiful array have to satisfy two conditions : \n nums.length is even.\n nums[i] != nums[i + 1] for all i % 2 == 0.\n\nHere , we wil
Himanshu__ranjan
NORMAL
2022-03-28T06:17:14.205588+00:00
2022-03-28T11:02:56.765099+00:00
121
false
\nHere to Make the array Beautiful array have to satisfy two conditions : \n* nums.length is even.\n* nums[i] != nums[i + 1] for all i % 2 == 0.\n\nHere , we will first try to satisfy condition 2 and later when all elements have satisfied the 2nd condition.Then only, we will verify 1st condition.\n\n***Checking Second Condition*** \uD83D\uDC48\nFirst we will check even indexes . If we delete any element then all elements after that particular index will shift to left by 1 index. but we can\'t do this because it will take o(n) time for each deletion.\nSo, for this reason we will use a boolean variable. This boolean will tell us whether to check odd indexes or even indexes.\nIf no. of Deletions is even it will check even indexes and if no. of deletions is odd it will check odd indexes.\n\n***Checking First condition*** \uD83D\uDC48 \nSince all the elements satisfy Second condition. So , removing last elemnent from the array (if array length is odd) will not affect other elements & they will still satisfy second condition.\n\n\n\n\n\n```\n\tclass Solution {\n public int minDeletion(int[] nums) {\n int n = nums.length ,count = 0;\n boolean flag = true; // We will use this boolean variable to check whether to check any particular index or not\n\t\t\n \n\t\t//Checking first condition whether nums[i] != nums[i + 1] for any required index\n\t\t\n for(int i=0;i<(n-1);i++){\n if(flag == true){\n if((i&1) == 0 && nums[i] == nums[i+1]){\n count++;\n flag = false;\n }\n }else{\n if((i&1) == 1 && nums[i] == nums[i+1]){\n count++;\n flag = true;\n }\n }\n }\n\t\t//Checking whether length of array after deletions is odd or even if it is odd remove last element else do nothing \n if((n-count)%2 == 0) return count; // n-count -> length of array after deletion\n return (count + 1); \n }\n}\n```
2
0
['Two Pointers', 'Greedy', 'Java']
1
minimum-deletions-to-make-array-beautiful
[JavaScript] 2216. Minimum Deletions to Make Array Beautiful
javascript-2216-minimum-deletions-to-mak-aa17
---\n\n- Implementation, after we write, is easy\n\n---\n\nWeekly Contest 286\n- Q1 answer - https://leetcode.com/problems/find-the-difference-of-two-arrays/dis
pgmreddy
NORMAL
2022-03-28T01:59:02.729804+00:00
2022-03-28T23:20:19.126686+00:00
245
false
---\n\n- Implementation, after we write, is easy\n\n---\n\n**Weekly Contest 286**\n- **Q1** answer - https://leetcode.com/problems/find-the-difference-of-two-arrays/discuss/1889224/JavaScript-2215.-Find-the-Difference-of-Two-Arrays\n- **Q2** answer - https://leetcode.com/problems/minimum-deletions-to-make-array-beautiful/discuss/1890258/JavaScript-2216.-Minimum-Deletions-to-Make-Array-Beautiful\n - **below**\n- **Q3** answer - https://leetcode.com/problems/find-palindrome-with-fixed-length/discuss/1892167/JavaScript-2217.-Find-Palindrome-With-Fixed-Length\n\n---\n\nHope it is simple to understand.\n\n---\n\n```\nvar minDeletion = function (nums) {\n const n = nums.length;\n\n let deletes = 0;\n for (let i = 0; i < n - 1; i++) {\n let j = i - deletes; // new i\n if (\n j % 2 === 0 && //\n nums[i] === nums[i + 1]\n ) {\n deletes++;\n }\n }\n\n // nums.length must be is even, if odd delete 1 more\n return deletes + ((n - deletes) % 2);\n};\n```\n\n---\n
2
0
['JavaScript']
0
minimum-deletions-to-make-array-beautiful
✅✅✅✅Minimum Deletions to Make Array Beautiful
minimum-deletions-to-make-array-beautifu-zfiy
\nclass Solution {\npublic:\n int minDeletion(vector<int>& nums) \n {\n int n=nums.size();\n int count=0;\n vector<int>v;\n in
krishnakr44
NORMAL
2022-03-27T16:06:51.728086+00:00
2022-03-27T16:06:51.728114+00:00
227
false
```\nclass Solution {\npublic:\n int minDeletion(vector<int>& nums) \n {\n int n=nums.size();\n int count=0;\n vector<int>v;\n int f=1;\n for(int i=0;i<n-1;i++)\n {\n //cheak even position\n if(i%2==0 && nums[i]==nums[i+1] && f==1){\n count++;f=0;\n }\n //cheak odd position because of shifting\n if(i%2==1 && nums[i]==nums[i+1] && f==0){\n count++;f=1;\n }\n \n }\n \n //count =>we have to remove numbers\n \n \n // if count is even and size is odd then remove extra one to make size even (odd-even=odd)\n if(n%2==1 && count%2==0)\n return count+1;\n \n //similarly count+1==even\n else if(n%2==0 && count%2==1)\n return count+1;\n \n else\n return count;\n\n }\n \n};\n```
2
0
['C', 'C++']
0
minimum-deletions-to-make-array-beautiful
✅ C++ || Easy to Understand || Using a Single Loop
c-easy-to-understand-using-a-single-loop-vlmp
\nclass Solution {\npublic:\n int minDeletion(vector<int>& nums) \n {\n int size = nums.size();\n\t\tint ans = 0;\n\t\tfor(int i=0;i<size-1;i++)\n\
mayanksamadhiya12345
NORMAL
2022-03-27T08:40:41.850268+00:00
2022-03-27T08:40:41.850296+00:00
37
false
```\nclass Solution {\npublic:\n int minDeletion(vector<int>& nums) \n {\n int size = nums.size();\n\t\tint ans = 0;\n\t\tfor(int i=0;i<size-1;i++)\n\t\t{\n\t\t\tif(nums[i] == nums[i+1])\n ans++;\n \n\t\t\telse\n i++;\n\t\t}\n \n // for the even length both are even or odd \n // if they are not then we will remove the last element for making nums even\n // and for that removing increase the cnt by 1\n\t\tif(size % 2 == 0 and ans % 2 != 0)\n ans++;\n \n\t\tif(size % 2 == 1 and ans % 2 == 0)\n ans++;\n \n // if nums is already even then return it\n\t\treturn ans;\n }\n}\n```
2
0
[]
0
minimum-deletions-to-make-array-beautiful
Java/Python | Simple Greedy Solution Explained
javapython-simple-greedy-solution-explai-sx3r
Idea:\nif there are 2 consecutive elements that are equal and its a good index to delete then we delete it.\nevery step, good index to delete is:\nif we deleted
Avuvos
NORMAL
2022-03-27T07:13:37.711667+00:00
2022-03-27T07:14:22.859872+00:00
97
false
Idea:\nif there are 2 consecutive elements that are equal **and** its a good index to delete then we delete it.\nevery step, good index to delete is:\nif we deleted 0 elements, we want the **even** indices\nif we deleted 1 elements, we want the **odd** indicies\nif we deleted 2 elements, again we want the **even** indicies and so on.\nso every iteration just check if we need to delete the current index and the consecutive elements are equal.\n\nin the end dont forget to check if the length of the array is odd, meaning we need 1 more deletion to make it even.\n\n**Java code:**\n```\npublic int minDeletion(int[] nums) {\n int n = nums.length;\n int res = 0;\n \n for (int i = 0; i < n - 1; i++) {\n boolean deleteIdx = res % 2 == i % 2;\n if (deleteIdx && nums[i] == nums[i + 1]) {\n res++;\n }\n }\n if ((n - res) % 2 == 1) {\n res++;\n }\n return res;\n }\n```\n\n**Python code:**\n```\ndef minDeletion(self, nums: List[int]) -> int:\n\tn = len(nums)\n\tres = 0\n\n\tfor i in range(n - 1):\n\t\tdelete_idx = res % 2 == i % 2\n\t\tif delete_idx and nums[i] == nums[i + 1]:\n\t\t\tres += 1\n\n\tif (n - res) % 2 == 1:\n\t\tres += 1\n\treturn res\n```\n
2
0
['Greedy', 'Python', 'Java']
0
minimum-deletions-to-make-array-beautiful
C++ || O(N) || with comments
c-on-with-comments-by-shm_47-rsm1
\nclass Solution {\npublic:\n int minDeletion(vector<int>& nums) {\n int count = 0;\n \n for(int i = 0; i < nums.size()-1; i++){
shm_47
NORMAL
2022-03-27T07:10:33.441674+00:00
2022-03-27T07:10:33.441715+00:00
40
false
```\nclass Solution {\npublic:\n int minDeletion(vector<int>& nums) {\n int count = 0;\n \n for(int i = 0; i < nums.size()-1; i++){\n if((i- count) % 2 == 0 && nums[i]==nums[i+1]){ //checking dupliactes at even index\n count++; //count deletions;\n }\n }\n \n if( (nums.size()-count)%2) return count+1; //even size\n return count;\n }\n};\n```
2
0
[]
0
minimum-deletions-to-make-array-beautiful
My Solution
my-solution-by-123_tripathi-3ftl
\nint minDeletion(vector<int>& nums) {\n int ans=0;\n for(int i=0;i<nums.size()-1;i++){\n if(nums[i] == nums[i+1]){\n an
123_tripathi
NORMAL
2022-03-27T06:04:46.040074+00:00
2022-06-01T10:27:11.421479+00:00
248
false
```\nint minDeletion(vector<int>& nums) {\n int ans=0;\n for(int i=0;i<nums.size()-1;i++){\n if(nums[i] == nums[i+1]){\n ans++;\n }else{\n i++;\n }\n }\n if((nums.size()-ans) % 2 != 0){\n ans++;\n }\n return ans;\n }\n```
2
0
['Greedy', 'C', 'Python', 'C++', 'Java', 'Python3']
0
minimum-deletions-to-make-array-beautiful
Both O(n) and O(1) space complexity Java solution
both-on-and-o1-space-complexity-java-sol-pr8f
First we will see the O(n) space complexity solution then we will move forward to the optimized one\nThe only reason I\'m telling you both solutions is to get t
sgshines737
NORMAL
2022-03-27T05:53:12.213715+00:00
2022-03-27T06:10:32.259932+00:00
45
false
First we will see the O(n) space complexity solution then we will move forward to the optimized one\nThe only reason I\'m telling you both solutions is to get the intution behind it So make sure you read the whole post.\nFirst approach:\nIn this approach simply convert the array into arraylist\nand do what condition given in the question says\n\nTraverse through the arraylist using while loop and if at even index the ith element is equal to (i+1)th element then \nremove the ith element and increment the count.\nand just get back to previous index\nafter traversing the whole arraylist\njust make sure your arraylist length is even and if it\'s not then do count++\n```\nclass Solution {\n public int minDeletion(int[] nums) {\n ArrayList<Integer> num = new ArrayList<>();\n for(int i = 0; i < nums.length; i++){\n num.add(nums[i]);\n }\n int count = 0;\n int i = 0;\n while(i < num.size() - 1){\n if(i%2 == 0 && num.get(i).equals(num.get(i+1))){\n num.remove(i);\n i= i - 1;\n count++;\n }\n i++;\n }\n if(num.size() % 2 != 0){\n count++;\n }\n return count;\n }\n}\n```\nSo, here time complexity will be O(n)\nand space complexity will also be O(n)\n\nNote: don\'t use == operator for comparison of element as you will get WA.\n\nOptimised approach:\n\nNow here I want you to imagine the above solution without arraylist\nthen probably you don\'t have to remove the elements right?\nand since we have to check only even elements so we can directly jump to i+2 elements\nbut what about length of array\nhow we make check for that without removing elements?\nIt\'s simple just check if array length - count is even or not is it is odd then increment the count\nIt was simple right?\n```\nclass Solution {\n public int minDeletion(int[] nums) {\n int count = 0;\n int i = 0;\n while(i < nums.length - 1){\n if(nums[i] == nums[i + 1]){\n count++;\n i--;\n }\n i += 2;\n }\n if((nums.length - count) % 2 != 0){\n count++;\n }\n return count;\n }\n}\n```\nTC: O(n)\nSC: O(1)\n\n
2
1
[]
0
minimum-deletions-to-make-array-beautiful
Very Short C++ Code
very-short-c-code-by-maditya_01-e4l8
\nclass Solution {\npublic:\n int minDeletion(vector<int>& nums) { \n int n=nums.size();\n int cnt=0;\n\t\t//ind is our virtual index afte
maditya_01
NORMAL
2022-03-27T05:52:26.175791+00:00
2022-03-27T05:52:26.175829+00:00
24
false
```\nclass Solution {\npublic:\n int minDeletion(vector<int>& nums) { \n int n=nums.size();\n int cnt=0;\n\t\t//ind is our virtual index after deletion\n for(int i=0;i<n-1;i++){\n //ind--> we are shifting element to left to keep what will be the index of element after deleting element.\n //cnt--> tells how many elements we have deleted.\n int ind=i-cnt;\n if((ind)%2==0 and nums[i]==nums[i+1]){ \n cnt++;\n } \n }\n\t\t//Edge Case Condition.\n if((n-cnt)%2) cnt++;\n\t \n return cnt;\n }\n};\n```
2
0
[]
0
minimum-deletions-to-make-array-beautiful
C++ | O(n) time, O(1) space | Easy to Understand
c-on-time-o1-space-easy-to-understand-by-0unb
\n\nclass Solution {\npublic:\n int minDeletion(vector<int>& num) {\n int n = num.size(),ans=0;\n for(int i = 0; i < n ; i+=2) while(i+1<n and
Archit-Bikram
NORMAL
2022-03-27T04:50:54.923097+00:00
2022-03-27T05:04:26.497634+00:00
26
false
\n```\nclass Solution {\npublic:\n int minDeletion(vector<int>& num) {\n int n = num.size(),ans=0;\n for(int i = 0; i < n ; i+=2) while(i+1<n and num[i] == num[i+1]) i++,ans++; \n ans += (n-ans)&1;\n return ans;\n }\n};\n```
2
0
[]
0
minimum-deletions-to-make-array-beautiful
Java | Single Pointer | O(n) time, O(1) space
java-single-pointer-on-time-o1-space-by-82lej
We simply check if the next element is equal to the current.\n1. If it is equals, we delete the current element and add one to our ans.\n2. If it is not equal,
bumpyride12
NORMAL
2022-03-27T04:31:48.774169+00:00
2022-03-27T04:31:48.774195+00:00
71
false
We simply check if the next element is equal to the current.\n1. If it is equals, we delete the current element and add one to our `ans`.\n2. If it is not equal, we increase the current index by `2`.\n3. At the end, if index is the last element of the array, then we know we must remove it to make the final array even size.\n\n\n```\nclass Solution {\n public int minDeletion(int[] nums) {\n int i=0; int ans=0;\n while(i<nums.length-1) {\n if(nums[i] == nums[i+1]) {\n i++;\n ans++;\n } else {\n i+=2;\n }\n }\n if(i == nums.length-1) return ans+1;\n return ans;\n }\n}\n```\n\nUpvote if you like this solution
2
0
[]
0
minimum-deletions-to-make-array-beautiful
[Python 3] O(N) time O(1) space
python-3-on-time-o1-space-by-a33584080-txvm
Variable index is to keep track of the current index of nums after "deletion"\nDuring for loop if index%2==0 and nums[i] == nums[i+1] we just don\'t increment i
a33584080
NORMAL
2022-03-27T04:18:49.008494+00:00
2022-03-27T04:18:49.008523+00:00
41
false
Variable *index* is to keep track of the current index of *nums* after "deletion"\nDuring for loop if index%2==0 and nums[i] == nums[i+1] we just don\'t increment index.\n\n```\nclass Solution:\n def minDeletion(self, nums: List[int]) -> int:\n res=0\n index=0\n for i in range(len(nums)-1):\n if index%2==0 and nums[i] == nums[i+1]:\n res+=1\n else:\n index+=1\n return res if index%2 else res+1\n```
2
0
[]
0
minimum-deletions-to-make-array-beautiful
C++ Easy
c-easy-by-kishan_akbari-sav6
```\nclass Solution {\npublic:\n int minDeletion(vector& v){\n \n int ans = 0;\n int x = 0;\n for(int i=0; i<(int)v.size(); ++i)\
Kishan_Akbari
NORMAL
2022-03-27T04:14:28.232563+00:00
2022-03-27T04:16:16.449955+00:00
24
false
```\nclass Solution {\npublic:\n int minDeletion(vector<int>& v){\n \n int ans = 0;\n int x = 0;\n for(int i=0; i<(int)v.size(); ++i)\n {\n if(i%2==x && i+1<v.size())\n {\n if(v[i]==v[i+1]){\n ans++;\n x ^= 1;\n }\n }\n }\n \n if((v.size()-ans)%2==1)\n ans++;\n \n return ans; \n \n }\n};
2
0
['Greedy', 'C']
0
minimum-deletions-to-make-array-beautiful
C++ | Short and Easy | Shifting positions
c-short-and-easy-shifting-positions-by-c-2g3z
Let s=number of times to shift left. If we delete element from array, we have to increase it.\nthen i-s will be new position after shifting by s number of times
codingsuju
NORMAL
2022-03-27T04:01:48.148523+00:00
2022-03-27T04:05:21.398069+00:00
123
false
Let s=number of times to shift left. If we delete element from array, we have to increase it.\nthen i-s will be new position after shifting by s number of times to left\n```\nclass Solution {\npublic:\n int minDeletion(vector<int>& nums) {\n int ans=0;\n int s=0;\n int n=nums.size();\n for(int i=1;i<n;i++){\n int pos=i-s;\n if(pos%2==1){\n if(nums[i]==nums[i-1]){\n ans++;\n s++;\n }\n }\n }\n int m=n-ans;\n if(m%2==1)ans++;\n return ans;\n }\n};
2
1
['C']
0
minimum-deletions-to-make-array-beautiful
A simple dp solution
a-simple-dp-solution-by-infiniteflame-v5kp
\npublic int minDeletion(int[] nums) {\n\tint l = nums.length;\n\tint[] dp = new int[l];\n\tdp[0] = 0;\n\tfor(int i=1; i<l; i++) {\n\t\t// Check nums[i] != nums
infiniteflame
NORMAL
2022-03-27T04:01:44.818647+00:00
2022-03-27T04:04:55.589955+00:00
145
false
```\npublic int minDeletion(int[] nums) {\n\tint l = nums.length;\n\tint[] dp = new int[l];\n\tdp[0] = 0;\n\tfor(int i=1; i<l; i++) {\n\t\t// Check nums[i] != nums[i + 1] for all i % 2 == 0.\n\t\tif((nums[i] == nums[i-1]) && ((i-1-dp[i-1])%2 == 0)) {\n\t\t\tdp[i] = dp[i-1] + 1;\n\t\t}\n\t\telse\n\t\t\tdp[i] = dp[i-1];\n\t}\n\t\n\t// Check nums.length is even.\n\treturn (l-dp[l-1])%2 == 0 ? dp[l-1] : dp[l-1]+1;\n}\n```
2
0
[]
0
minimum-deletions-to-make-array-beautiful
🚀0ms Runtime || Beats 100% || O(1) space || best optimal approach
0ms-runtime-beats-100-o1-space-best-opti-tn5m
Intuition A "beautiful array" is defined as one where every even-indexed element is different from the next element. We iterate through the array, removing dupl
kaushik_aviral
NORMAL
2025-03-16T14:53:39.121177+00:00
2025-03-16T14:53:39.121177+00:00
41
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> - A "beautiful array" is defined as one where every even-indexed element is different from the **next element**. - We iterate through the array, removing duplicate elements that would violate this condition at even indices. # Approach <!-- Describe your approach to solving the problem. --> - ✅ Step 1: Use a counter ctr to track the number of deletions. - ✅ Step 2: Iterate through the array and check if nums[i] == nums[i+1] at even indices. - ✅ Step 3: If yes, increment ctr (delete nums[i]). - ✅ Step 4: If the final array has an odd length, delete one more element to maintain even length. # Complexity Time Complexity: $$O(n)$$ → Single pass through the array. Space Complexity: $$O(1)$$→ No extra memory used. ![Screenshot 2025-03-16 201553.png](https://assets.leetcode.com/users/images/bc35b9ea-e8fd-44d4-87fa-16c73ac16523_1742136552.551632.png) # Code ```cpp [] class Solution { public: int minDeletion(vector<int>& nums) { int ctr= 0,i=0; while(i<nums.size()-1){ if((i-ctr)%2==0 || i==0){ if(nums[i]==nums[i+1]){ ctr++; } } i++; } if((nums.size()-ctr)%2==0) return ctr; return ctr+1; } }; ```
1
0
['Array', 'Stack', 'Greedy', 'C++', 'Java', 'Python3']
0
minimum-deletions-to-make-array-beautiful
Using Two pointers Beats 94.71% Python3
using-two-pointers-beats-9471-python3-by-2xjb
Intuition\n Describe your first thoughts on how to solve this problem. \nThe problem requires ensuring that no two consecutive elements in an array are the same
amanabiy
NORMAL
2024-08-29T22:11:27.070948+00:00
2024-08-29T22:11:27.070975+00:00
6
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem requires ensuring that no two consecutive elements in an array are the same, and the array length is even. The simplest approach is to iterate from the left, removing duplicates when they appear next to each other. By carefully tracking deletions, we can ensure that the resulting array meets the required conditions.\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- Iterate from the left: We assume that each step leaves the left portion of the array valid, and we only delete elements when necessary to avoid duplicates.\n- Use two pointers: One pointer (current_ptr) tracks the current valid element, and the other (ptr) checks the next element. If the two elements are the same, we increment the count_to_delete and move the ptr forward.\n- Handle array length: After processing the array, we check if the number of deletions leads to an odd-length array. If so, we make one additional deletion to ensure the length is even.\n\n# Complexity\n- Time complexity: O(n) -> n is the length of the list\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```python3 []\nclass Solution:\n def minDeletion(self, nums: List[int]) -> int:\n """\n The length of the array should be even\n and (so if it have even length I can only delete even elements)\n if it have odd (length I can only delete odd length)\n """\n n = len(nums)\n count_to_delete = 0\n\n # pointers to track the non-deleted\n ptr = 1\n current_ptr = 0\n \n while ptr < n:\n if nums[current_ptr] == nums[ptr]:\n ptr += 1\n count_to_delete += 1\n else:\n current_ptr = ptr + 1\n ptr += 2\n\n if n % 2 != count_to_delete % 2:\n count_to_delete += 1\n \n return count_to_delete\n```
1
0
['Two Pointers', 'Greedy', 'Python3']
0
minimum-deletions-to-make-array-beautiful
C++ using stack
c-using-stack-by-sam_455-eyxp
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
sam_455
NORMAL
2024-06-15T07:21:02.072950+00:00
2024-06-15T07:21:02.072982+00:00
20
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(n)\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```\n// #include<stack>\nclass Solution {\npublic:\n int minDeletion(vector<int>& nums) {\n int deletions = 0;\n stack<int>st;\n for(int i=0;i<nums.size();++i){\n if(!st.empty() && st.top() == nums[i] && st.size() %2 !=0){\n deletions++;\n }else{\n st.push(nums[i]);\n }\n }\n if(st.size() % 2 != 0){\n deletions++;\n }\n return deletions;\n }\n};\n```
1
0
['C++']
0
minimum-deletions-to-make-array-beautiful
Intutive greedy stack approach
intutive-greedy-stack-approach-by-abhayd-5ju3
Literally do what the questions says\n\n# Code\n\nclass Solution {\n public int minDeletion(int[] nums) {\n return util1(nums);\n }\n public int
AbhayDutt
NORMAL
2023-10-03T16:01:40.904721+00:00
2023-10-03T16:01:40.904742+00:00
93
false
Literally do what the questions says\n\n# Code\n```\nclass Solution {\n public int minDeletion(int[] nums) {\n return util1(nums);\n }\n public int util1(int[] arr) {\n Stack<Integer> s = new Stack<>();\n int operations = 0;\n for (int i : arr) {\n if (s.isEmpty() == false && (s.peek() == i && (s.size() - 1) % 2 == 0)) {\n operations++;\n } else {\n s.push(i);\n }\n }\n while (s.size() % 2 != 0) {\n operations++;\n s.pop();\n }\n return operations;\n }\n}\n```\nyou can check out my github repository where i am uploading famous interview questions topic wise with solutions.\nlink-- https://github.com/Abhaydutt2003/DataStructureAndAlgoPractice \nkindly upvote if you like my solution. you can ask doubts below.\xAF\n \n
1
0
['Stack', 'Greedy', 'Java']
0
minimum-deletions-to-make-array-beautiful
Easy || Short || C++
easy-short-c-by-ashu_kharya-4dbw
\n# Approach\n Describe your approach to solving the problem. \n1. If it equals to next element, increment ans and i by 1.\n2. Else incremenr by 2.\n3. Finally
ashu_kharya
NORMAL
2023-09-12T06:45:00.898141+00:00
2023-09-12T06:45:00.898164+00:00
115
false
\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. If it equals to next element, increment ans and i by 1.\n2. Else incremenr by 2.\n3. Finally to make the array even size, again do +1. \n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n$$O(n)$$\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n$$O(1)$$\n\n# Code\n```\nclass Solution {\npublic:\n int minDeletion(vector<int>& nums) {\n int n=nums.size();\n int i=0; int ans=0;\n while(i < n-1) {\n if(nums[i] == nums[i+1]) i++, ans++;\n else i+=2;\n }\n if(i == n-1) return ans+1;\n return ans;\n }\n};\n```
1
0
['C++']
1
minimum-deletions-to-make-array-beautiful
C# Solution in O(N) time & without space
c-solution-in-on-time-without-space-by-m-ugh4
\n# Complexity\n- Time complexity:\nO(N)\n- Space complexity:\nO(1)\n# Code\n\npublic class Solution {\n public int MinDeletion(int[] nums) {\n int re
mohamedAbdelety
NORMAL
2023-04-02T11:06:17.347099+00:00
2023-04-02T11:06:17.347154+00:00
18
false
\n# Complexity\n- Time complexity:\nO(N)\n- Space complexity:\nO(1)\n# Code\n```\npublic class Solution {\n public int MinDeletion(int[] nums) {\n int res = 0, i = 0;\n while(i < nums.Length){\n int j = i + 1;\n while(j < nums.Length && nums[j] == nums[i]){\n res++;\n j++;\n }\n if(j == nums.Length)res++;\n i = j + 1;\n }\n return res;\n }\n}\n```
1
0
['Array', 'C#']
0
minimum-deletions-to-make-array-beautiful
Very Easy Approach using Stacks | Greedy Approach | Faster than 20%
very-easy-approach-using-stacks-greedy-a-r6q6
Intuition\n Describe your first thoughts on how to solve this problem. \n- At first I thought to arrange an array at every loop. But that approach was too costl
shubham-0707
NORMAL
2023-03-01T17:21:23.095797+00:00
2023-03-01T17:21:23.095860+00:00
47
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n- At first I thought to arrange an array at every loop. But that approach was too costly and caused me TLE.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- Then I thought of the stack approach.\n- Here I am checking that if the stack size is odd which means that the index is even.\n- I am checking for every element when the stack size is odd that the element is equal to the peek element or not.\n- When the element is equal to tht top element I am increasing my count and continuing my loop.\n- At last if the size of the stack is odd which means we have to delete one more element.\n- If not then we can simply return the count.\n\n# Complexity\n- Time complexity: $$O(N)$$\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```\nclass Solution {\n public int minDeletion(int[] nums) {\n //Arrays.sort(nums);\n\n Stack<Integer> st = new Stack<>();\n int count = 0;\n for(int i=0 ; i<nums.length ; i++){\n if(st.size()%2==1 && st.peek()==nums[i]){\n count++;\n continue;\n }\n else st.push(nums[i]);\n }\n\n if(st.size()%2==1)return count+1;\n\n return count;\n }\n}\n```
1
0
['Stack', 'Greedy', 'Java']
0
minimum-deletions-to-make-array-beautiful
C++ || Using Stack || Easy Solution
c-using-stack-easy-solution-by-sunnynand-vzhv
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
sunnynandan665
NORMAL
2023-01-13T14:05:06.053693+00:00
2023-01-13T14:12:02.193743+00:00
66
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(n)\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```\nclass Solution {\npublic:\n int minDeletion(vector<int>& nums) {\n int n = nums.size();\n int ans = 0;\n stack<int> st;\n st.push(nums[0]);\n\n for(int i=1;i<n;i++){\n if(st.size()%2 == 1 and st.top() == nums[i]){ // if stack size is odd means even postion of i index of nums and top of stack and nums[i] are equal then it delete the top no\n ans++;\n st.pop(); \n } \n st.push(nums[i]);\n }\n\n return (st.size()%2 == 1) ? ans+1: ans; // if stack size is odd then increase the ans by 1 to make array beautiful.\n \n }\n};\n```
1
0
['C++']
0
minimum-deletions-to-make-array-beautiful
JavaScript O(N) with easy explanation
javascript-on-with-easy-explanation-by-d-h5be
\nvar minDeletion = function(nums) {\n \n let n = nums.length;\n \n let k = 0;\n for(let i = 0; i<n-1; i++) {\n \n /*\n
dimensionless_
NORMAL
2022-11-11T18:53:10.224644+00:00
2022-11-11T18:53:10.224681+00:00
74
false
```\nvar minDeletion = function(nums) {\n \n let n = nums.length;\n \n let k = 0;\n for(let i = 0; i<n-1; i++) {\n \n /*\n if i delete one element then all elements to right of it shift by one \n likewise for k deleted elements, indexes of the elements of right to them will become i-k\n so we have to check i - k is even or not if even and adjacent elements are same then i \n will be deleting one more element.\n \n \n i am checking nums[i] === nums[i+1] for index i not for index i - k because i am not deleting it but \n just counting it that how many of them i will be deleting as of now.\n */\n \n if((i - k)%2 === 0 && nums[i] === nums[i + 1]) {\n k++;\n }\n \n }\n \n //after deletion if arrays length is odd then delete just one element from last.\n if((n-k)%2 == 1) k++;\n \n return k;\n \n \n};\n```
1
0
['JavaScript']
0
minimum-deletions-to-make-array-beautiful
Java solution | Easy
java-solution-easy-by-sourin_bruh-nfkq
Please Upvote !!!\n\nclass Solution {\n public int minDeletion(int[] nums) {\n int delet = 0;\n int n = nums.length;\n \n for (in
sourin_bruh
NORMAL
2022-09-19T17:42:34.658039+00:00
2022-09-19T17:42:34.658092+00:00
54
false
### **Please Upvote !!!**\n```\nclass Solution {\n public int minDeletion(int[] nums) {\n int delet = 0;\n int n = nums.length;\n \n for (int i = 0; i < n - 1; i++) {\n int newIndex = i - delet;\n if (newIndex % 2 == 0 && nums[i] == nums[i + 1]) {\n delet++;\n }\n }\n\n return (n - delet) % 2 == 0 ? delet : delet + 1;\n }\n}\n\n// TC: O(n), SC: O(1)\n```
1
0
['Java']
0
minimum-deletions-to-make-array-beautiful
CPP
cpp-by-zehbar_rayani-2kfx
\tclass Solution {\n\tpublic:\n\t\tint minDeletion(vector& nums) {\n\t\t\tif(nums.empty()) return 0;\n\t\t\tint c= 0;\n\t\t\tfor(int i = 0;i<nums.size()-1;i+=2)
zehbar_rayani
NORMAL
2022-09-05T19:20:45.070277+00:00
2022-09-05T19:20:45.070321+00:00
20
false
\tclass Solution {\n\tpublic:\n\t\tint minDeletion(vector<int>& nums) {\n\t\t\tif(nums.empty()) return 0;\n\t\t\tint c= 0;\n\t\t\tfor(int i = 0;i<nums.size()-1;i+=2){\n\t\t\t\tif(nums[i] == nums[i+1]) {\n\t\t\t\t\ti--;\n\t\t\t\t\tc++;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn c + (nums.size()-c)%2 ;\n\t\t}\n\t};
1
0
[]
0
minimum-deletions-to-make-array-beautiful
[Python] Simple Intuitive solution
python-simple-intuitive-solution-by-gp05-04ll
In the approach, we are actaully doing pop element which does the left shift\n\nclass Solution:\n def minDeletion(self, nums: List[int]) -> int:\n i =
Gp05
NORMAL
2022-07-31T10:33:29.624160+00:00
2022-07-31T10:33:29.624205+00:00
77
false
In the approach, we are actaully doing pop element which does the left shift\n```\nclass Solution:\n def minDeletion(self, nums: List[int]) -> int:\n i = 0\n count = 0\n while i + 1 < len(nums):\n if i % 2 == 0 and nums[i] == nums[i+1]:\n count += 1\n\t\t\t\t # left shift\n nums.pop(i)\n continue\n i += 1\n return count if len(nums) % 2 == 0 else count + 1\n \n```\n\nHere, we are avoiding the pop and using the duplicate count value to check for the index (if nums would have be popped count times)\nThis improves the time complexity\n\n```\nclass Solution:\n def minDeletion(self, nums: List[int]) -> int:\n i = 0\n count = 0\n while i + 1 < len(nums):\n if (i - count) % 2 == 0 and nums[i] == nums[i+1]:\n count += 1\n i += 1\n return count + (len(nums) - count) % 2\n```
1
0
['Stack', 'Python']
0
minimum-deletions-to-make-array-beautiful
Coded a solution in 1 minute and it's no worse than any others
coded-a-solution-in-1-minute-and-its-no-7qffk
I coded this solution in literally 1 minute. And I checked the top voted solutions in the discuss section. Surprisingly none is better than mine!\n\nO(1) space
tyler-liu
NORMAL
2022-07-19T05:54:23.188508+00:00
2022-07-19T05:57:24.727393+00:00
35
false
I coded this solution in literally 1 minute. And I checked the top voted solutions in the discuss section. Surprisingly none is better than mine!\n\n**O(1) space and O(n) time**\n\n```py\ndef minDeletion(self, nums: List[int]) -> int:\n res = i = 0\n while i + 1 < len(nums):\n if nums[i] != nums[i + 1]:\n i += 2\n else:\n res += 1\n i += 1\n if (len(nums) - res) % 2 == 1:\n res += 1\n return res\n```\n\nYes there are shorter solutions, but readability counts!
1
0
[]
0
minimum-deletions-to-make-array-beautiful
Beautiful array using 2 methods
beautiful-array-using-2-methods-by-spide-7is0
Method 1: using shifting of indexes\nclass Solution {\n public int minDeletion(int[] nums) {\n int count = 0;\n for(int i=0;i<nums.length-1;i++
Spidey_Edith
NORMAL
2022-06-29T07:45:02.703237+00:00
2022-06-29T07:45:02.703279+00:00
63
false
Method 1: using shifting of indexes\nclass Solution {\n public int minDeletion(int[] nums) {\n int count = 0;\n for(int i=0;i<nums.length-1;i++){\n int m = i - count;\n if(m%2 == 0 && nums[i] == nums[i+1]){\n count++;\n }\n }\n \n if((nums.length - count) % 2 == 0)\n return count;\n return count+1;\n \n }\n}\n\n\n\nMethod 2 : Using arraylist(relatively high run time)\n\nclass Solution {\n public int minDeletion(int[] nums) {\n List<Integer> result = new ArrayList<Integer>();\n int count = 0;\n \n for(int i : nums)\n result.add(i);\n \n for(int i=0;i<result.size()-1;i++){\n int x = result.get(i);\n int y = result.get(i+1);\n if(i%2 == 0 && x == y){\n result.remove(i);\n count++;\n i = i - 1;\n }\n }\n\n if(result.size() % 2 == 0)\n return count;\n return count+1;\n }\n}
1
0
['Java']
0
minimum-deletions-to-make-array-beautiful
Simple Java Solution || faster than 71.91% of Java online submissions
simple-java-solution-faster-than-7191-of-dvvq
\n\nclass Solution {\n public int minDeletion(int[] nums) {\n int delete=0;\n for(int i =0; i < nums.length -1;i++){\n int ind = i-d
Shreyaspatil1903
NORMAL
2022-06-28T13:48:21.371229+00:00
2022-06-28T13:48:21.371256+00:00
59
false
```\n\nclass Solution {\n public int minDeletion(int[] nums) {\n int delete=0;\n for(int i =0; i < nums.length -1;i++){\n int ind = i-delete;\n if(ind%2 == 0 && nums[i] == nums[i+1]){\n delete++;\n \n }\n \n }\n \n return ((nums.length-delete)%2 == 0)? delete : delete+1;\n \n }\n}\n```
1
0
['Greedy', 'Java']
0
minimum-deletions-to-make-array-beautiful
C++ | Shortest solution | Easy to comprehend | O(n) time | constant space
c-shortest-solution-easy-to-comprehend-o-qhi7
\nclass Solution {\npublic:\n int minDeletion(vector<int>& nums) {\n int n=nums.size(), ctr=0, op=0;\n for(int i=0; i<n-1; i++,ctr++) {\n
__ikigai__
NORMAL
2022-06-28T09:27:12.327049+00:00
2022-06-28T09:30:58.517215+00:00
15
false
```\nclass Solution {\npublic:\n int minDeletion(vector<int>& nums) {\n int n=nums.size(), ctr=0, op=0;\n for(int i=0; i<n-1; i++,ctr++) {\n if(!(ctr&1)) while(i+1<n&&nums[i+1]==nums[i]) i++, op++;\n }\n return (ctr&1)?op+1:op;\n }\n};\n```
1
0
[]
0
minimum-deletions-to-make-array-beautiful
Easy to understand two pointer solution in Golalng
easy-to-understand-two-pointer-solution-h0vtk
go\nfunc minDeletion(nums []int) int {\n left, right := 0, 1\n var deletions int\n for right < len(nums) {\n // Move the right pointer as much as needed s
user0440H
NORMAL
2022-06-28T08:19:43.065720+00:00
2022-06-28T08:19:43.065754+00:00
29
false
```go\nfunc minDeletion(nums []int) int {\n left, right := 0, 1\n var deletions int\n for right < len(nums) {\n // Move the right pointer as much as needed so that the elements at\n\t// left and right are different\n for right < len(nums) && nums[right] == nums[left] {\n right++\n deletions++\n }\n\t// Make right and left adjacent\n right += 2\n left = right - 1\n }\n if (len(nums)-deletions)%2 == 1 { // length of the array after deletions is odd\n deletions++\n }\n return deletions\n}\n```
1
0
['Two Pointers', 'Go']
0
minimum-deletions-to-make-array-beautiful
C++ Solution Using Deque
c-solution-using-deque-by-kunal_patil-8qyc
\'\'\'\n\n int minDeletion(vector& nums) {\n deque p,q;\n int ans=0;\n \n for(int i=0;i<nums.size();i++)\n {\n
kunal_patil
NORMAL
2022-05-25T07:45:38.169065+00:00
2022-05-25T07:45:38.169096+00:00
63
false
\'\'\'\n\n int minDeletion(vector<int>& nums) {\n deque<int> p,q;\n int ans=0;\n \n for(int i=0;i<nums.size();i++)\n {\n p.push_back(nums[i]);\n }\n \n q.push_back(p.front());\n p.pop_front();\n \n while(!p.empty())\n {\n while(!p.empty() && q.back()==p.front())\n {\n p.pop_front();\n ans++;\n }\n while(!p.empty() && q.back()!=p.front())\n {\n q.push_back(p.front());\n p.pop_front();\n if(!p.empty())\n {\n q.push_back(p.front());\n p.pop_front();\n }\n }\n }\n \n if(q.size()%2==1)\n {\n q.pop_back();\n ans++;\n }\n \n return ans;\n }\n\t\n\t\'\'\'
1
0
['Queue', 'C']
0
minimum-deletions-to-make-array-beautiful
[Java] Stack
java-stack-by-teemo25-i5mb
\nclass Solution {\n\n public int minDeletion(int[] nums) {\n Deque<Integer> stack = new ArrayDeque();\n int delete = 0;\n for (int num : nums) {\n
teemo25
NORMAL
2022-05-21T17:59:48.184927+00:00
2022-05-21T17:59:48.184973+00:00
57
false
```\nclass Solution {\n\n public int minDeletion(int[] nums) {\n Deque<Integer> stack = new ArrayDeque();\n int delete = 0;\n for (int num : nums) {\n if (stack.size() % 2 == 0) {\n stack.push(num);\n } else {\n if (stack.peek() == num) {\n delete++;\n } else {\n stack.push(num);\n }\n }\n }\n return stack.size() % 2 == 0 ? delete : delete + 1;\n }\n}\n```
1
0
['Java']
0
minimum-deletions-to-make-array-beautiful
Find the Shift Index After Removal
find-the-shift-index-after-removal-by-sr-ovuw
\nclass Solution {\n public int minDeletion(int[] nums) {\n int del = 0;\n int n = nums.length;\n \n for(int i = 0; i < nums.leng
Srijit_1998
NORMAL
2022-05-04T16:11:28.726680+00:00
2022-05-04T16:11:28.726798+00:00
25
false
```\nclass Solution {\n public int minDeletion(int[] nums) {\n int del = 0;\n int n = nums.length;\n \n for(int i = 0; i < nums.length-1; i++){\n int shiftIndex = (i - del);\n if(nums[i] == nums[i+1] && shiftIndex % 2 == 0){\n del++;\n }\n }\n if((n - del) % 2 == 1){\n del++;\n }\n return del;\n }\n}\n```
1
0
[]
0
minimum-deletions-to-make-array-beautiful
Java | O(N)
java-on-by-pratham_ghule-v9c2
\nclass Solution {\n public int minDeletion(int[] nums) {\n int count=0;\n \n for(int i=0;i<nums.length;i+=2){\n if(i<nums.len
pratham_ghule
NORMAL
2022-04-16T09:19:17.135202+00:00
2022-04-16T09:19:17.135249+00:00
89
false
```\nclass Solution {\n public int minDeletion(int[] nums) {\n int count=0;\n \n for(int i=0;i<nums.length;i+=2){\n if(i<nums.length-1 && nums[i]==nums[i+1]){\n count++;\n i--;\n }\n }\n \n if((nums.length-count)%2!=0)\n count++;\n \n return count;\n }\n}\n```
1
0
['Java']
1
minimum-deletions-to-make-array-beautiful
c solution with faster 95% and less memory 80%
c-solution-with-faster-95-and-less-memor-745z
\n/*\nShare\nYou are given a 0-indexed integer array nums. The array nums is beautiful if:\n\nnums.length is even.\nnums[i] != nums[i + 1] for all i % 2 == 0.\n
jason01200120
NORMAL
2022-04-12T09:53:10.811077+00:00
2022-04-14T12:36:51.866373+00:00
25
false
```\n/*\nShare\nYou are given a 0-indexed integer array nums. The array nums is beautiful if:\n\nnums.length is even.\nnums[i] != nums[i + 1] for all i % 2 == 0.\nNote that an empty array is considered beautiful.\n\nYou can delete any number of elements from nums. When you delete an element, all the elements to the right of the deleted element will be shifted one unit to the left to fill the gap created and all the elements to the left of the deleted element will remain unchanged.\n\nReturn the minimum number of elements to delete from nums to make it beautiful.\n\n\n\nExample 1:\n\nInput: nums = [1,1,2,3,5]\nOutput: 1\nExplanation: You can delete either nums[0] or nums[1] to make nums = [1,2,3,5] which is beautiful. It can be proven you need at least 1 deletion to make nums beautiful.\nExample 2:\n\nInput: nums = [1,1,2,2,3,3]\nOutput: 2\nExplanation: You can delete nums[0] and nums[5] to make nums = [1,2,2,3] which is beautiful. It can be proven you need at least 2 deletions to make nums beautiful.\n\n\nConstraints:\n\n1 <= nums.length <= 105\n0 <= nums[i] <= 105\n\n*/\n\nint minDeletion(int* nums, int numsSize){\n int del = 0;\n \n for (int i = 0; (i + 1) < numsSize; i = i + 2){\n if (nums[i] != nums[i+1]){\n //Yes\n } else {\n del++;\n i = i -1 ;\n }\n }\n if ((numsSize - del)%2 == 0){\n return del;\n } else {\n return del + 1;\n }\n}\n```
1
0
['C']
0
minimum-deletions-to-make-array-beautiful
Easy Stack Solution with comments
easy-stack-solution-with-comments-by-the-ak3u
\nclass Solution {\n public int minDeletion(int[] nums) {\n int currIdx = -1;\n int ans = 0;\n // stores beautiful array on the go\n
theashishmalik
NORMAL
2022-04-10T10:09:51.930233+00:00
2022-04-10T10:09:51.930276+00:00
132
false
```\nclass Solution {\n public int minDeletion(int[] nums) {\n int currIdx = -1;\n int ans = 0;\n // stores beautiful array on the go\n Stack<Integer> st = new Stack<>();\n for(int i : nums) {\n if (st.isEmpty() || st.peek()!=i || currIdx%2!=0) {\n st.push(i);\n currIdx++;\n } else if(currIdx % 2 == 0 && st.peek()==i) {\n ans++; \n }\n }\n // if odd ...remove last element\n if(st.size()%2==1) {\n ans++;\n }\n \n return ans;\n \n }\n}\n```
1
0
['Stack', 'Java']
0
minimum-deletions-to-make-array-beautiful
Easy Java Solution
easy-java-solution-by-ujjwalsharma-f0ei
\nclass Solution {\n public int minDeletion(int[] nums) {\n int count=0;\n \n for(int i=0;i<nums.length-1;i++){\n // shifting
UjjwalSharma
NORMAL
2022-04-06T13:00:01.766538+00:00
2022-04-06T13:00:01.766587+00:00
32
false
```\nclass Solution {\n public int minDeletion(int[] nums) {\n int count=0;\n \n for(int i=0;i<nums.length-1;i++){\n // shifting the index if the 1st element is removed than \n // the index of the 2nd element will be 2-1=1 where \n // 1 is the number of count\n // So the new index of the element at 2nd will be now 1\n int shiftIndex=i-count;\n if(nums[i]==nums[i+1] && shiftIndex%2==0){\n count++;\n }\n }\n if((nums.length-count)%2!=0) count++;\n return count;\n }\n}\n```
1
0
['Java']
0
minimum-deletions-to-make-array-beautiful
JAVA O(1) SPACE || EASY TO UNDERSTAND
java-o1-space-easy-to-understand-by-magi-m28a
\nclass Solution {\n public int minDeletion(int[] nums) {\n int n=nums.length;\n\n int dlt=0;\n for(int i=0;i<n-1;i++){\n if(
magic04
NORMAL
2022-04-04T09:27:28.146237+00:00
2022-07-21T12:57:41.183417+00:00
41
false
```\nclass Solution {\n public int minDeletion(int[] nums) {\n int n=nums.length;\n\n int dlt=0;\n for(int i=0;i<n-1;i++){\n if(nums[i]==nums[i+1] && (i-dlt)%2==0){ // i-dlt indicates the index shift in the left\n dlt++;\n }\n }\n if((n-dlt)%2==1){ // to have a even length array\n dlt++;\n }\n return dlt;\n }\n}\n```
1
0
[]
1
minimum-deletions-to-make-array-beautiful
Easy to Understand||C++
easy-to-understandc-by-codekarle-al2b
class Solution {\npublic:\n\n int minDeletion(vector& nums) {\n int ans=0;\n for(int i=1;i<nums.size();i++){\n if(((i-1)-ans)%2==0 &
codekarle
NORMAL
2022-04-02T06:12:26.179754+00:00
2022-04-02T06:12:26.179785+00:00
102
false
class Solution {\npublic:\n\n int minDeletion(vector<int>& nums) {\n int ans=0;\n for(int i=1;i<nums.size();i++){\n if(((i-1)-ans)%2==0 && nums[i]==nums[i-1] ){\n ans++;\n }\n //12233\n }\n //12233 but size is odd so we have to remove one but from end not from middle because if we remove from middle it will affect the index of other element.\n if((nums.size()-ans)%2){\n ans++;\n }\n return ans;\n }\n};``
1
0
['C', 'C++']
0
minimum-deletions-to-make-array-beautiful
Minimum Deletions to Make Array Beautiful
minimum-deletions-to-make-array-beautifu-9r5u
\n# First I am looking for value at index even and repeating at the next index. \n# If it occurs means one deletion means now all values shifts to even, and eve
prabhatm580
NORMAL
2022-04-02T05:49:01.874095+00:00
2022-04-02T05:49:01.874130+00:00
63
false
```\n# First I am looking for value at index even and repeating at the next index. \n# If it occurs means one deletion means now all values shifts to even, and even shifted to odd.\n# for example [1,1,2,2,3,3] at 0th index we found a repeating element \n# Now after deletion [1,2,2,3,3] becomes new array and now 2 is not at even index so we can\'t delete it as well as for 3.\n# after doing all this we have deleted one index and size is now 5 so we have deleted one element from end \n# that will not disturb the index of other values\n# So for doing this we need 2deletions\n# for maitiaining shift we are subtracting deletion count with current index(KEY)\n```\n```\nclass Solution \n{\n public:\n int minDeletion(vector<int>& nums) \n {\n int n=nums.size();\n int ans=0;\n for(int i=0;i<n-1;i++)\n {\n if((i-ans)%2==0 && nums[i+1]==nums[i])\n ans++; \n }\n if((n-ans)%2)\n ans++;\n return ans;\n }\n};\n```
1
0
['Greedy', 'C', 'C++']
0
minimum-deletions-to-make-array-beautiful
✅ 3ms fasterthan100 || greedy || TC: O(n), SC: O(1) || 2 ways
3ms-fasterthan100-greedy-tc-on-sc-o1-2-w-0elv
If you\'ll like, do UpVote :)\n\nNote for Coding Interviews:\n\t\n\t1. I mentioned 2 solutions keeping Coding interviews in mind, where you can first talk abou
karankhara
NORMAL
2022-03-31T20:12:00.960223+00:00
2022-04-01T15:29:48.722271+00:00
79
false
If you\'ll like, do **UpVote** :)\n\n**Note for Coding Interviews:**\n\t\n\t1. I mentioned 2 solutions keeping Coding interviews in mind, where you can first talk about Solution 1 and,\n\t\t then optimize it right after using Solution 2 :) \n\t2. It is always recommended to start with brute force and then bring more optimized approach in Coding interviews.\n\t3. Solution 2 is almost similar to first, just without creating additional List and without deletion of elements.\n\t\n\t\n## Solution 1 (less optimized, with deletions)\n\tclass Solution {\n\t\tpublic int minDeletion(int[] nums) {\n\t\t\tList<Integer> list = new ArrayList<>();\n\t\t\tfor(int num : nums) { list.add(num); }\n\t\t\tif(allSameElements(list)){ return nums.length; }\n\n\t\t\tint steps = 0; \n\t\t\tsteps = makeEvenIndexUnique(list, steps);\n\t\t\tsteps = makeEvenLength(list, steps);\n\t\t\treturn steps;\n\t\t}\n\n\t\tpublic int makeEvenIndexUnique(List<Integer> list, int steps){\n\t\t\tfor(int i = 0; i < list.size()-1; i+=2){\n\t\t\t\tif (i%2 != 0) continue;\n\t\t\t\tif(list.get(i).equals(list.get(i+1))){ \n\t\t\t\t\tlist.remove(i);\n\t\t\t\t\tsteps++; \n\t\t\t\t\ti-=2;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn steps;\n\t\t}\n\t\tpublic int makeEvenLength(List<Integer> list, int steps){\n\t\t\tif(list.size() % 2 != 0){ steps++; }\n\t\t\treturn steps;\n\t\t}\n\n\t\tpublic boolean allSameElements(List<Integer> list){\n\t\t\tSet<Integer> set = new HashSet<>();\n\t\t\t\tset.addAll(list);\n\t\t\t\tif(set.size() == 1){ return true; }\n\t\t\t\treturn false;\n\t\t\t}\n\t\t}\n\t//\n\t//\n\t//\n\n## \tSolution 2 (more optimized, no deletions)\n\tclass Solution {\n\t\tpublic int minDeletion(int[] nums) {\n\t\t\tint steps = 0; \n\t\t\tsteps = makeEvenIndexUnique(nums, steps);\n\t\t\tsteps = makeEvenLength(nums, steps);\n\t\t\treturn steps;\n\t\t}\n\n\t\tpublic int makeEvenIndexUnique(int[] nums, int steps){\n\t\t\tfor(int i = 0; i < nums.length-1; i+=2){\n\t\t\t\tif(nums[i] == nums[i+1]){ \n\t\t\t\t\tsteps++; \n\t\t\t\t\ti--;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn steps;\n\t\t}\n\t\tpublic int makeEvenLength(int[] nums, int steps){\n\t\t\tif( (nums.length - steps) % 2 != 0){ steps++; }\n\t\t\treturn steps;\n\t\t}\n\t}\n\t\nFor coding interviews:\nYou can discuss Solution 1 first, and then optimize using Solution 2 (instead of directly jumpting to optimized solution). \n\nIf you need more explanation or, if got even more optimized way, do share.\nIf you liked it, do **UpVote** :)\n\n
1
0
['Java']
1
minimum-deletions-to-make-array-beautiful
O(n) Varun
on-varun-by-vchawla100-hk7l
Elegant solutionnnnn.\n\nclass Solution {\n public int minDeletion(int[] arr) { // odd even // 1 1 1 2 3 4 4;\n if(arr.length == 1) return 1;\n
vchawla100
NORMAL
2022-03-31T17:28:19.960237+00:00
2022-03-31T17:28:19.960275+00:00
47
false
Elegant solutionnnnn.\n```\nclass Solution {\n public int minDeletion(int[] arr) { // odd even // 1 1 1 2 3 4 4;\n if(arr.length == 1) return 1;\n \n Queue<Integer> even = new ArrayDeque<>(); // \n Queue<Integer> odd = new ArrayDeque<>(); // \n int cnt = 0;\n boolean flag = true;\n for(int val : arr){\n if(flag){\n even.add(val);\n\n } else{\n odd.add(val);\n\n }\n flag = !flag;\n }\n \n \n int count = 0;\n while(even.size() > 0 && odd.size() > 0){\n\n if(even.peek() == odd.peek()){\n \n even.remove();\n count++;\n \n Queue<Integer> temp = even;\n even = odd;\n odd = temp;\n \n } else{\n even.remove();\n odd.remove();\n }\n }\n \n if(even.size() != 0){\n even.remove();\n count++;\n } else if(odd.size() != 0){\n odd.remove();\n count++;\n } \n \n return count; \n }\n \n public static void swap(Queue<Integer> even, Queue<Integer> odd){\n Queue<Integer> temp = even;\n even = odd;\n odd = temp;\n }\n}\n```
1
0
[]
0
minimum-deletions-to-make-array-beautiful
C++ || Greedy || Clean Code
c-greedy-clean-code-by-niks07-ec4a
\nclass Solution {\npublic:\n int minDeletion(vector<int>& nums) {\n int n=nums.size();\n int deletions=0;\n for(int i=0;i+deletions+1<n
niks07
NORMAL
2022-03-31T12:13:30.166623+00:00
2022-03-31T12:13:30.166668+00:00
71
false
```\nclass Solution {\npublic:\n int minDeletion(vector<int>& nums) {\n int n=nums.size();\n int deletions=0;\n for(int i=0;i+deletions+1<n;i+=2){\n while(i+deletions+1<n and nums[i+deletions]==nums[i+deletions+1])\n deletions++;\n }\n \n \n \n \n int len=n-deletions;\n if(len%2==0){\n return deletions;\n }\n \n return deletions+1;\n }\n};\n\n```
1
0
['Greedy', 'C']
0
minimum-deletions-to-make-array-beautiful
JAVA || STACK || EASY || EXPLAINATION
java-stack-easy-explaination-by-bhaskar2-3md0
Here in order to solve this question we need to consider some cases and need to keep those in our mind:\n If stack has even elements that\'s if(stk.size()%2==0)
bhaskar20inn
NORMAL
2022-03-30T19:56:11.118421+00:00
2022-03-30T19:56:11.118467+00:00
125
false
Here in order to solve this question we need to consider some cases and need to keep those in our mind:\n* If stack has even elements that\'s `if(stk.size()%2==0)` then we know that only even indexes are needed to be considered as per real-time arraty so we just add that number in stack\n* Now second case is when we have odd numbers in stack then we just need to check two more thing:\n\t\t\t1. if stk peek element is same then we don\'t put the current element and increase the count\n\t\t\t2. if not same then simply put that value\n\n**Read it carefully it\'s a beautiful question too LOL**\n\n```\nclass Solution {\n public int minDeletion(int[] nums) {\n Stack<Integer> stk = new Stack<>();\n int n = nums.length;\n int count=0;\n for(int i=0;i<nums.length;i++){\n if(!stk.isEmpty()){\n if(stk.size()%2==0){\n stk.add(nums[i]);\n }else{\n if(stk.peek()==nums[i]) count++;\n else stk.add(nums[i]);\n }\n }else{\n stk.add(nums[i]);\n }\n }\n if(stk.size()%2==1) count++;\n return count;\n }\n}\n```
1
0
['Stack', 'Greedy', 'Java']
0
minimum-deletions-to-make-array-beautiful
Python - Easy
python-easy-by-lokeshsk1-kt7k
\nclass Solution:\n def minDeletion(self, nums: List[int]) -> int:\n \n res = 0\n i = 0\n n = len(nums)\n \n while
lokeshsk1
NORMAL
2022-03-29T17:17:30.276433+00:00
2022-03-29T17:17:30.276469+00:00
107
false
```\nclass Solution:\n def minDeletion(self, nums: List[int]) -> int:\n \n res = 0\n i = 0\n n = len(nums)\n \n while i < n-1:\n if nums[i] == nums[i+1]:\n res += 1\n i += 1\n else:\n i += 2\n \n if nums[n-1] == nums[n-2]:\n res += 1\n \n if (n - res) % 2:\n res += 1\n \n return res\n \n```
1
0
['Python', 'Python3']
0
minimum-deletions-to-make-array-beautiful
Minimum Deletions to Make Array Beautiful O(N) single pass C++
minimum-deletions-to-make-array-beautifu-yanz
\nclass Solution {\npublic:\n int minDeletion(vector<int>& nums) {\n bool even=true;\n int count=0;\n for(int i=1;i<nums.size();i++){\n
arpitpachauri220
NORMAL
2022-03-29T14:35:07.556302+00:00
2022-03-29T14:35:07.556340+00:00
45
false
```\nclass Solution {\npublic:\n int minDeletion(vector<int>& nums) {\n bool even=true;\n int count=0;\n for(int i=1;i<nums.size();i++){\n if(even==true && i%2==1){\n if(nums[i]==nums[i-1]){\n count++;\n even=false;\n }\n }\n else if(even==false && i%2==0){\n if(nums[i]==nums[i-1]){\n count++;\n even=true;\n }\n }\n }\n if(nums.size()==0) return count;\n if((nums.size()-count)%2==1) count++;\n return count;\n }\n};\n```
1
0
[]
0
minimum-deletions-to-make-array-beautiful
Simple Solution in Java || Elegant and Concise
simple-solution-in-java-elegant-and-conc-atus
\nclass Solution {\n public int minDeletion(int[] nums) {\n \n boolean evenIndex = true;\n int ans = 0; \n int i = 0;\n
PRANAV_KUMAR99
NORMAL
2022-03-29T04:57:34.954030+00:00
2022-03-29T05:01:00.715731+00:00
15
false
```\nclass Solution {\n public int minDeletion(int[] nums) {\n \n boolean evenIndex = true;\n int ans = 0; \n int i = 0;\n while(i < nums.length){\n if(evenIndex == false){\n // The current number is at odd index, the next will be at even index\n evenIndex = !evenIndex;\n i++;\n continue;\n }\n \n int j = i+1;\n while(j<nums.length && nums[j] == nums[j-1]){\n j++;\n }\n \n if(j == nums.length){ \n // The starting index was at even position and it\'s repetition is till the end of the array, we have to delete all the numbers \n // From starting to the end \n ans += j - i;\n }\n else ans += (j - i - 1);\n \n // The current number is at even index, the next will be at odd index\n evenIndex = !evenIndex;\n \n i = j;\n }\n \n return ans;\n }\n}\n```
1
0
[]
0
minimum-deletions-to-make-array-beautiful
Beginner friendly Java Solution!!!
beginner-friendly-java-solution-by-kabil-lxot
\nclass Solution {\n public int minDeletion(int[] nums) {\n int deletions=0,shiftedIndex=0,numslen=nums.length;\n for(int i=0;i<numslen;i++){\n
kabiland
NORMAL
2022-03-28T13:42:44.997000+00:00
2022-03-28T13:42:44.997028+00:00
25
false
```\nclass Solution {\n public int minDeletion(int[] nums) {\n int deletions=0,shiftedIndex=0,numslen=nums.length;\n for(int i=0;i<numslen;i++){\n shiftedIndex=i-deletions;\n if(shiftedIndex%2==0){\n if(i!=numslen-1 && nums[i]==nums[i+1])\n deletions+=1;\n }\n }\n if((numslen-deletions)%2!=0){\n deletions+=1;\n }\n return deletions;\n }\n}\n```
1
0
['Array', 'Java']
0
minimum-deletions-to-make-array-beautiful
C++ easy to understand
c-easy-to-understand-by-shanushan-oc1e
Just skip untill adjacent pair is equal.\n\n\nclass Solution {\npublic:\n int minDeletion(vector<int>& nums) {\n int ans = 0;\n int n = nums.si
shanushan
NORMAL
2022-03-28T11:30:02.595033+00:00
2022-03-28T11:30:02.595061+00:00
29
false
Just skip untill adjacent pair is equal.\n\n```\nclass Solution {\npublic:\n int minDeletion(vector<int>& nums) {\n int ans = 0;\n int n = nums.size();\n int i = 0;\n while(i < n){\n int j = i + 1;\n while(j < n and nums[i] == nums[j]){\n j += 1;\n }\n if(j == n){\n ans += j - i;\n }\n else{\n ans += j - i - 1;\n }\n i = j + 1;\n \n }\n return ans;\n }\n};\n```
1
0
['Two Pointers', 'Greedy']
0
minimum-deletions-to-make-array-beautiful
c++ || stack
c-stack-by-ayushanand245-4wju
\nclass Solution {\npublic:\n int minDeletion(vector<int>& nums) {\n bool k = false;\n int n = nums.size();\n if(n%2==0) k=true;\n
ayushanand245
NORMAL
2022-03-28T07:54:03.482027+00:00
2022-03-28T07:54:03.482061+00:00
23
false
```\nclass Solution {\npublic:\n int minDeletion(vector<int>& nums) {\n bool k = false;\n int n = nums.size();\n if(n%2==0) k=true;\n \n stack<int> st;\n int p=0;\n st.push(nums[p++]);\n int count=0;\n while(p < n){\n if((st.size()-1)%2==0 && nums[p]==st.top()){\n count++;\n p++;\n k = !k;\n }\n else{\n st.push(nums[p]);\n p++;\n }\n }\n if(k) return count;\n else return count+1;\n }\n};\n```
1
0
[]
0
abbreviating-the-product-of-a-range
Modulo and Double
modulo-and-double-by-votrubac-lizi
First, after any multiplication, we get rid of trailing zeros, counting them in c.\n\nNow, how to find out the last 5 digits of the product? For that, we multip
votrubac
NORMAL
2021-12-25T20:48:55.808879+00:00
2022-01-09T00:20:03.995591+00:00
2,604
false
First, after any multiplication, we get rid of trailing zeros, counting them in `c`.\n\nNow, how to find out the last 5 digits of the product? For that, we multiply numbers, remove trailing zeros, and keep last `n` digits using the modulo operation. Simple.\n\nWhat about the first 5 digits? We could use `double`, multiply numbers, and *divide* the result by 10 until we only have 5 digits before the decimal point. The precision seems to be sufficient enough.\n\n**C++**\n```cpp\nstring abbreviateProduct(int left, int right) {\n long long suff = 1, c = 0, total = 0, max_suff = 100000000000;\n double pref = 1.0;\n for (int i = left; i <= right; ++i) {\n pref *= i;\n suff *= i;\n while (pref >= 100000) {\n pref /= 10;\n total = total == 0 ? 6 : total + 1; \n }\n while (suff % 10 == 0) {\n suff /= 10;\n ++c;\n }\n suff %= max_suff;\n }\n string s = to_string(suff);\n return to_string((int)pref) + (total - c <= 10 ? "" : "...") \n + (total - c < 5 ? "" : s.substr(s.size() - min(5LL, total - c - 5))) + "e" + to_string(c);\n}\n```
57
4
['C']
6
abbreviating-the-product-of-a-range
✅ [C++/Java/Python] Scientific Notation || Very Detailed Explanation || With and Without Logarithm
cjavapython-scientific-notation-very-det-eo2l
\nPLEASE UPVOTE if you like \uD83D\uDE01 If you have any question, feel free to ask. \n\n\nThere is no free lunch, both logarithmic sum and scientific notation
linfq
NORMAL
2021-12-25T16:01:56.731911+00:00
2022-01-18T06:40:58.360411+00:00
2,486
false
\n**PLEASE UPVOTE if you like** \uD83D\uDE01 **If you have any question, feel free to ask.** \n<br>\n\nThere is no free lunch, both logarithmic sum and scientific notation will definitely lose accuracy. we can only discard the last digits and keep the most important prefix as many as possible, actually `float` even `double` are also imprecise.\n```\nTime Complexity: O(N)\nSpace Complexity: O(1)\n```\n\n* **Solution 1: use float or double to record prefix, inspired by scientific notation**\n\t* keep `0.1 <= prod < 1.0`, so `len(str(int(prod * 100000))) == 5`, just like **scientific notation**\n\t\t* so we can easily get `pre` via `pre = str(int(prod * 100000))`\n\t* `org_digits` is the number of digits of the original very very large product number, `zeros` is the number of its trailing zeros, so that `org_digits - zeros` is the number of its actual non-zero digits.\n\t* The error of `suf` can only come from the trailing zeros.\n\t\t* Although we remove all of the trailing zeros every time and then take the modulus, but 2 and 5 as thedivision of suf may squeeze the effective digit length.\n\t\t* so if we want to keep the exactly accurate last 5 Digits, we need to increase the digit length.\n\t\t* Since `n` is less than or equal to `10 ** 4`, there are no more than 5 trailing zeros that can be eliminated at one time. At this time, `n = 5 ** 5`, and the eliminated invalidity factor is `2 ** 5 = 32`, so we waste up to 5 digits for recording useless information at one time, so `suf %= 10 ** 14` is large enough to keep accurate 5 digits of `suf`.\n\t\t* Another reason we chose `10 ** 14` is that `(10 ** 14) * (10 ** 4) = 10 ** 18` is in the range of `int64`, so it can be easily migrated to other programming languages such as Java and C++.\n\n\n\t\t**Python**\n\t\t```\n\t\tclass Solution(object):\n\t\t\tdef abbreviateProduct(self, left, right):\n\t\t\t\tprod, suf, zeros, org_digits = 1.0, 1, 0, 0\n\t\t\t\tfor n in range(left, right + 1):\n\t\t\t\t\tprod *= n\n\t\t\t\t\twhile prod >= 1: # keep 0.1 <= prod < 1.0, so len(str(int(prod * 100000))) == 5\n\t\t\t\t\t\tprod /= 10\n\t\t\t\t\t\torg_digits += 1 # add 1 while remove 1 digit\n\t\t\t\t\tsuf *= n\n\t\t\t\t\twhile suf % 10 == 0: # count and remove the trailing zeros\n\t\t\t\t\t\tzeros += 1\n\t\t\t\t\t\tsuf //= 10\n\t\t\t\t\tif suf > 10 ** 14:\n\t\t\t\t\t\tsuf %= 10 ** 14\n\t\t\t\tif org_digits - zeros <= 10:\n\t\t\t\t\treturn str(int(prod * (10 ** (org_digits - zeros)) + 0.5)) + \'e\' + str(zeros)\n\t\t\t\telse:\n\t\t\t\t\t# you may find that I add 0.5 before cast to int above, but not here.\n\t\t\t\t\t# It is because when org_digits - zeros <= 10, int(prod * (10 ** (org_digits - zeros)) + 0.5) is the actual\n\t\t\t\t\t# value, we add 0.5 to the last non-zero digits for rounding, 0.5 just means 0.5 over there.\n\t\t\t\t\t# However here int(prod * 100000) is the first 5-digit prefix, the 6-th digit is also a valid digit not\n\t\t\t\t\t# error.If we add 0.5 to 6-th digit, how do we calculate the first 6-digit or 7-digit prefix?\n\t\t\t\t\treturn str(int(prod * 100000)) + \'...\' + (\'0000\' + str(suf))[-5:] + \'e\' + str(zeros)\n\t\t```\n\t\t**C++**\n\t\t```\n\t\tclass Solution {\n\t\tpublic:\n\t\t\tstring abbreviateProduct(int left, int right) {\n\t\t\t\tlong long suf = 1;\n\t\t\t\tint zeros = 0, org_digits = 0;\n\t\t\t\tdouble prod = 1.0;\n\t\t\t\tfor (int n = left; n <= right; n ++) {\n\t\t\t\t\tprod *= n;\n\t\t\t\t\twhile (prod >= 1.0) { // keep 0.1 <= prod < 1.0, so len(str(int(prod * 100000))) == 5\n\t\t\t\t\t\tprod /= 10.0;\n\t\t\t\t\t\torg_digits ++; // add 1 while remove 1 digit\n\t\t\t\t\t}\n\t\t\t\t\tsuf *= n;\n\t\t\t\t\twhile (suf % 10 == 0) { // count and remove the trailing zeros\n\t\t\t\t\t\tzeros ++;\n\t\t\t\t\t\tsuf /= 10;\n\t\t\t\t\t}\n\t\t\t\t\tif (suf > pow(10, 14)) suf %= (long long)pow(10, 14);\n\t\t\t\t}\n\t\t\t\tif (org_digits - zeros <= 10) {\n\t\t\t\t\treturn to_string((long long)(prod * pow(10, org_digits - zeros) + 0.5)) + \'e\' + to_string(zeros);\n\t\t\t\t} else {\n\t\t\t\t\t// you may find that I add 0.5 before cast to int above, but not here.\n\t\t\t\t\t// It is because when org_digits - zeros <= 10, int(prod * (10 ** (org_digits - zeros)) + 0.5) is the actual\n\t\t\t\t\t// value, we add 0.5 to the last non-zero digits for rounding, 0.5 just means 0.5 over there.\n\t\t\t\t\t// However here int(prod * 100000) is the first 5-digit prefix, the 6-th digit is also a valid digit not\n\t\t\t\t\t// error.If we add 0.5 to 6-th digit, how do we calculate the first 6-digit or 7-digit prefix?\n\t\t\t\t\tstring str_suf = "0000" + to_string(suf);\n\t\t\t\t\treturn to_string((int)(prod * 100000)) + "..." + str_suf.substr(str_suf.length() - 5) + \'e\' + to_string(zeros);\n\t\t\t\t}\n\t\t\t}\n\t\t};\n\t\t```\n\t\t**Java**\n\t\t```\n\t\tclass Solution {\n\t\t\tpublic String abbreviateProduct(int left, int right) {\n\t\t\t\tlong suf = 1;\n\t\t\t\tint zeros = 0, org_digits = 0;\n\t\t\t\tdouble prod = 1.0;\n\t\t\t\tfor (int n = left; n <= right; n ++) {\n\t\t\t\t\tprod *= n;\n\t\t\t\t\twhile (prod >= 1.0) { // keep 0.1 <= prod < 1.0, so len(str(int(prod * 100000))) == 5\n\t\t\t\t\t\tprod /= 10.0;\n\t\t\t\t\t\torg_digits ++; // add 1 while remove 1 digit\n\t\t\t\t\t}\n\t\t\t\t\tsuf *= n;\n\t\t\t\t\twhile (suf % 10 == 0) { // count and remove the trailing zeros\n\t\t\t\t\t\tzeros ++;\n\t\t\t\t\t\tsuf /= 10;\n\t\t\t\t\t}\n\t\t\t\t\tif (suf > Math.pow(10, 14)) suf %= (long)Math.pow(10, 14);\n\t\t\t\t}\n\t\t\t\tif (org_digits - zeros <= 10) {\n\t\t\t\t\treturn (long)(prod * Math.pow(10, org_digits - zeros) + 0.5) + "e" + zeros;\n\t\t\t\t} else {\n\t\t\t\t\t// you may find that I add 0.5 before cast to int above, but not here.\n\t\t\t\t\t// It is because when org_digits - zeros <= 10, int(prod * (10 ** (org_digits - zeros)) + 0.5) is the actual\n\t\t\t\t\t// value, we add 0.5 to the last non-zero digits for rounding, 0.5 just means 0.5 over there.\n\t\t\t\t\t// However here int(prod * 100000) is the first 5-digit prefix, the 6-th digit is also a valid digit not\n\t\t\t\t\t// error.If we add 0.5 to 6-th digit, how do we calculate the first 6-digit or 7-digit prefix?\n\t\t\t\t\tString str_suf = "0000" + Long.toString(suf);\n\t\t\t\t\treturn (int)(prod * 100000) + "..." + str_suf.substring(str_suf.length() - 5) + \'e\' + zeros;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t```\n\n* **Solution 2\uFF1Ause int64 to record prefix**\n\n\t* `pre `and `suf` retain at most 13 digits each\n\t* They update separately\n\t* at any time, once the trailing `0` appears it will not disappear.\n\t\t* so we count trailing zeros in `suf` immediately via `zeros += 1`, then remove them from suf via `suf //= 10`\n\t* My naive motivation is as long as `MAX` is large enough, the code above would work well.\n\t\t```\n\t\tclass Solution(object):\n\t\t\tdef abbreviateProduct(self, left, right):\n\t\t\t\tMAX = 10 ** 13\n\t\t\t\tpre, suf, zeros, org_digits = 1, 1, 0, 0\n\t\t\t\tfor n in range(left, right + 1):\n\t\t\t\t\tpre *= n # update pre\n\t\t\t\t\tsuf *= n # update suf\n\t\t\t\t\twhile suf % 10 == 0: # count and remove the trailing zeros\n\t\t\t\t\t\tzeros += 1\n\t\t\t\t\t\tsuf //= 10\n\t\t\t\t\tif suf > MAX: # retain at most 13 digits\n\t\t\t\t\t\tsuf %= MAX\n\t\t\t\t\twhile pre > MAX: # retain at most 13 digits\n\t\t\t\t\t\tif org_digits == 0:\n\t\t\t\t\t\t\torg_digits = 13\n\t\t\t\t\t\tpre //= 10\n\t\t\t\t\t\torg_digits += 1\n\t\t\t\tif len(str(suf)) <= 10 and org_digits - zeros <= 10:\n\t\t\t\t\treturn str(suf)[:10] + \'e\' + str(zeros)\n\t\t\t\telse:\n\t\t\t\t\treturn str(pre)[:5] + \'...\' + str(suf)[-5:] + \'e\' + str(zeros)\n\t\t```\n\t\n\n* **Solution 3: use logarithmic sum to record prefix**\n\t* I noticed a popular solution from another perspective using logarithmic sum, but since the time complexity is still `O(R - L)`, maybe it is no need to waste code to deal with 2 and 5 specially, removing them can make the code and the idea concise a lot.\n\t* the key idea is the logarithm of the product of a set of numbers is equal to the sum of the logarithms of the set of numbers.\n\t\t* `log(a * b * c) = log(a) + log(b) + log(c)`\n\t* Although the multiplication of `1` to `10 ** 6` is very very large, far exceeding the range of any basic data type in the programming language, its logarithm base `10` is only `5565702.91719`\n\t\t* however there is no free lunch, it must also loss precision when you recover the original large number N via power of `10`, like the 2 solutions above.\n\t* The integer part of the logarithm of a number N records the scale of N, and the decimal part records the precision of N.\n\t\t* for example, `log10(37968) = 4.579417720873264`\n\t\t\t* `10 ** 0.579417720873264 = 3.7968000000000024`\n\t\t\t* `10 ** 4 = 10000`\n\t\t\t* `37968 = 10 ** 4.579417720873264 = (10 ** 4) * (10 ** 0.579417720873264)`\n\t\t* Since `10 ** 0 = 1` and `10 ** 1 == 10`, for any `0 < x < 1`, `1 < 10 ** x < 10`, that also means so `len(str(int((10 ** x) * 100000))) == 5`\n\t\t* **Essentially this is the same as scientific notation.**\n\t\t```\n\t\tclass Solution(object):\n\t\t\tdef abbreviateProduct(self, left, right):\n\t\t\t\tsuf, zeros, log_sum, prod, org_digits = 1, 0, 0, 1.0, 0\n\t\t\t\tfor n in range(left, right + 1):\n\t\t\t\t\tlog_sum += math.log10(n)\n\t\t\t\t\tsuf *= n\n\t\t\t\t\twhile suf % 10 == 0: # count and remove the trailing zeros\n\t\t\t\t\t\tzeros += 1\n\t\t\t\t\t\tsuf //= 10\n\t\t\t\t\tif suf > 10 ** 8:\n\t\t\t\t\t\tsuf %= 10 ** 8\n\t\t\t\t\tprod *= n\n\t\t\t\t\twhile prod > 1.0:\n\t\t\t\t\t\torg_digits += 1\n\t\t\t\t\t\tprod /= 10\n\t\t\t\tif org_digits - zeros <= 10:\n\t\t\t\t\tpre = 10 ** (log_sum - int(log_sum) + 9)\n\t\t\t\t\treturn str(pre)[:max(1, org_digits - zeros)] + \'e\' + str(zeros)\n\t\t\t\telse:\n\t\t\t\t\tpre = 10 ** (log_sum - int(log_sum) + 4)\n\t\t\t\t\treturn str(pre)[:5] + \'...\' + str(suf)[-5:] + \'e\' + str(zeros)\n\t\t```\n\n\n**PLEASE UPVOTE if you like \uD83D\uDE01 If you have any question, feel free to ask.**
36
3
[]
9
abbreviating-the-product-of-a-range
[Python] math soluthion, explained
python-math-soluthion-explained-by-dbabi-7uc9
I will tidy up my code a bit later, for the moment what is important is idea. Basically, problem can be separated into 3 independet smaller problems:\n\n1. Find
dbabichev
NORMAL
2021-12-25T16:00:46.438846+00:00
2021-12-25T16:00:46.438876+00:00
1,884
false
I will tidy up my code a bit later, for the moment what is important is idea. Basically, problem can be separated into 3 independet smaller problems:\n\n1. Find number of trailing zeroes.\n2. Find last 5 digits which are not zeroes.\n3. Find first 5 digits.\n\nLet us deal with this problems one by one.\n1. To find number of trailing zeroes, we can use problem 172. Factorial Trailing Zeroes with modifications. We will find number of times divisors 2 and 5 met in our product and then choose the smallest one. I spend one fail attempt to catch the case like `L = R = 15`, then we have 1 five and 0 two.\n2. Find last 5 digits which are not zeroes: here what we need to do is to multiply all numbers, but also divide it by correct number of 5 and 2. To make our life easier I consider cases `R - L <= 30` separately where I just calculate product which is not very big by hands, python allows us to do it. In all other cases we can be sure that number of `5` in our prime factorization is smaller than number of `2`. I do not have strict proof, but it will help to make code a bit easier. So, each time we have number divisible by `5`, we divide it by `5` as many times as possible. Also we keep count of how many times we need to divide by `2` and when we reached limit, we stop dividing.\n3. Finally, how to find the first `5` digits of product. We will use logarithms and pray that we do not have rounding error. Indeed: `t = log10(L * (L+1) * ... * R) = log10(L) + ... + log10(R)`. What we need to take now is `10^(4 + {t})`, where `{t}` is fraction part. Luckily all tests were passed, probably because rounding error goes up and down and do not grow a lot.\n\n#### Complexity\nIt is `O(R - L)` for time because we need to work with modules and also with logarithms.\n\n#### Code\n```python\nfrom math import log10, floor, ceil\n\nclass Solution:\n def abbreviateProduct(self, L, R):\n def TZ(n, q):\n ans = 0\n while q <= n:\n ans += n//q\n q *= 5\n return ans\n\n c1 = TZ(R, 5) - TZ(L-1, 5)\n c2 = TZ(R, 2) - TZ(L-1, 2)\n tr_zeroes = min(c1, c2)\n\n if R - L <= 30:\n ans = 1\n for i in range(L, R + 1):\n ans *= i\n ans_str = str(ans) if tr_zeroes == 0 else str(ans)[:-tr_zeroes]\n \n if len(ans_str) > 10:\n return ans_str[:5] + "..." + ans_str[-5:] + "e" + str(tr_zeroes)\n else:\n return ans_str + "e" + str(tr_zeroes)\n \n else:\n t = sum(log10(i) for i in range(L, R + 1))\n tmp = t - floor(t)\n first = floor(10**(tmp + 4))\n\n last = 1\n to_do = tr_zeroes\n for i in range(L, R+1):\n Q = i\n while Q % 5 == 0: Q//=5\n if Q % 2 == 0 and to_do > 0:\n Q = Q//2\n to_do -= 1\n last = (last * Q) % 100000\n\n return str(first) + "..." + "0" * (5 - len(str(last))) + str(last) + "e" + str(tr_zeroes)\n```\n\nIf you have any question, feel free to ask. If you like the explanations, please **Upvote!**
28
7
['Math']
6
abbreviating-the-product-of-a-range
[python] Feel so sorry about the man who was asked this problem in the interview.
python-feel-so-sorry-about-the-man-who-w-e3gh
I don\'t like this type of question, although I solved it in the contest. \nIt is pure math, and nearly doesn\'t contain any algorithem. Maybe the hedge fo
hxu10
NORMAL
2021-12-25T16:35:20.877631+00:00
2022-01-01T17:08:49.519512+00:00
1,237
false
I don\'t like this type of question, although I solved it in the contest. \nIt is pure math, and nearly doesn\'t contain any algorithem. Maybe the hedge found like this problem. \n\n##### Also, according to @hqztrue , there are possibly some annoying edge testcases, if the product is like xxxxx99999999.... or xxxxx00000000, many solution will fail due to the precision error. I don\'t think these test cases should be collected, and problem author should make statement: It is guaranteed that the 6-7 digits will not be 99 or 00. \n\nBut since someone asked it, let me share my idea\n**1, count the number of zeros in the range(left,right):**\nThis can be done by calculating total factor of 2s and 5s, and take their minimum, that\'s the number of 0s.\n\n**2, remove the factor of 2 and 5 in the number range**\nThe factor removed both should be the same number of 0s \nIn step 1. For example, in the range (24,27), there are two 5s and four 2s so number of 0s should be two, you needed to divide two 2s in 24, and two 5s in 25, so the array[24,25,26,27] will become [6,1,26,27]\n\n**3, iterate through the array for front 5 digits and rear 5 digits**\nfor the rear 5 digits, just take the mod 100000, and for the front 5 digits, you can multiply these number together, and if the number greater than 100000, you divide these number by 10 until the number smaller than 100000, finally you take the integer part of the number. \n\nBelow is my code, total time: 1396ms, beat 100%.\n\n```\nimport math \nf2,f5 = [0]*1000001, [0]*1000001\nfor i in range(1,1000001):\n temp,d2,d5 = i,0,0\n while temp%2==0:\n temp = temp//2\n d2 += 1\n while temp%5==0:\n temp = temp//5\n d5 += 1\n f2[i] = f2[i-1] + d2\n f5[i] = f5[i-1] + d5\n \nclass Solution(object):\n def abbreviateProduct(self, left, right):\n """\n :type left: int\n :type right: int\n :rtype: str\n """\n twos = f2[right] - f2[left-1]\n fives = f5[right] - f5[left-1]\n tens = min(fives,twos)\n rest2,rest5 = tens, tens\n nums = [i for i in range(left,right+1)]\n for i in range(len(nums)):\n while rest2>0 and nums[i]%2==0:\n nums[i] = nums[i] // 2\n rest2 -= 1\n while rest5>0 and nums[i]%5==0:\n nums[i] = nums[i] // 5\n rest5 -= 1 \n rear,realnum, = 1,1\n front = 1.0\n\t\t\n for num in nums:\n front *= num\n while front > 100000: \n front = front * 0.1 \n rear = rear * num\n rear = rear % 100000\n if realnum>0: realnum = realnum * num\n if realnum>10**10: realnum = -1\n front = int(front)\n \n if realnum>0:\n ans = str(realnum) + "e" + str(tens)\n else:\n rears = str(rear)\n if len(rears)<5:\n rears = "0"*(5-len(rears)) + rears\n ans = str(front)+"..."+rears+"e"+str(tens) \n return ans\n```
14
2
[]
2
abbreviating-the-product-of-a-range
[Python3] quasi brute-force
python3-quasi-brute-force-by-ye15-jt3g
Please check out this commit for solutions of biweekly 68. \n\n\nclass Solution:\n def abbreviateProduct(self, left: int, right: int) -> str:\n ans =
ye15
NORMAL
2021-12-25T16:02:35.209453+00:00
2021-12-25T17:41:51.630253+00:00
964
false
Please check out this [commit](https://github.com/gaosanyong/leetcode/commit/8bba95f803d58a5e571fa13de6635c96f5d1c1ee) for solutions of biweekly 68. \n\n```\nclass Solution:\n def abbreviateProduct(self, left: int, right: int) -> str:\n ans = prefix = suffix = 1\n trailing = 0 \n flag = False \n for x in range(left, right+1): \n if not flag: \n ans *= x\n while ans % 10 == 0: ans //= 10 \n if ans >= 1e10: flag = True \n prefix *= x\n suffix *= x\n while prefix >= 1e12: prefix //= 10 \n while suffix % 10 == 0: \n trailing += 1\n suffix //= 10 \n if suffix >= 1e10: suffix %= 10_000_000_000\n while prefix >= 100000: prefix //= 10 \n suffix %= 100000\n if flag: return f"{prefix}...{suffix:>05}e{trailing}"\n return f"{ans}e{trailing}"\n```
14
3
['Python3']
6
abbreviating-the-product-of-a-range
Python | Use Log Value, explained
python-use-log-value-explained-by-zoo302-0ftp
First we notice that the trailing zeroes should be abbreviated, so we can calculate the number of trailing zeroes by calculating how many 2 and 5 in the result
zoo30215
NORMAL
2021-12-25T16:00:59.523268+00:00
2021-12-25T16:37:40.618744+00:00
436
false
First we notice that the trailing zeroes should be abbreviated, so we can calculate the number of trailing zeroes by calculating how many `2` and `5` in the result of prime factorization of the product, and the number of trailing zeroes is the minimum between the two values.\n\nNext we notice that we only need to calculate the first and the last 5 digits of the result. We can handle it separately.\n* How to calculate the last 5 digits?\nWell, it is easier than calculating first 5 digits. We only need to keep the remainder of the running product divided by `100000`.\n* How to calculate the first 5 digits?\nWe can calculate the cumulative sum of log value of the product, then convert it back to real numbers.\nFor example, `log(123456789) ~= 8.09151497717` and `pow(10, 4.09151497717) ~= 12345.6789`. We can find that if we only need to calculate the first 5 digits, the log value larger than 4 (i.e. `4.xx`) would be enough.\n\nThere is a small trick that we need to add a small epsilon (like `1e-11`) to handle the float precision.\n\n```python \nclass Solution:\n def abbreviateProduct(self, left: int, right: int) -> str:\n \n twos, fives = 0, 0\n tail, cum_log = 1, 0.0\n for i in range(left, right + 1):\n # eliminate all the 2\'s and 5\'s\n while i % 2 == 0:\n i //= 2\n twos += 1\n while i % 5 == 0:\n i //= 5\n fives += 1\n # keep last 5 digits with division by 100000\n tail = (tail * i) % 100000\n # calculate cumulative log\n cum_log += log(i, 10)\n # the number of trailing zeros\n zeros = min(twos, fives)\n \n # add the extra 2\'s or 5\'s into tail and cum_log\n tail = (tail * pow(2, twos - zeros, 100000) * pow(5, fives - zeros, 100000)) % 100000\n cum_log += log(2, 10) * (twos - zeros) + log(5, 10) * (fives - zeros)\n \n if cum_log >= 10:\n # extract first 5 digits from cumulative log\n head = str(pow(10, cum_log - floor(cum_log) + 1e-11 + 4))[:5]\n tail = str(tail).zfill(5)\n return f\'{head}...{tail}e{zeros}\'\n else:\n # use the cumulative log to calcualte the whole digits\n whole = str(floor(pow(10, cum_log + 1e-11)))\n return f\'{whole}e{zeros}\'\n```
10
1
[]
0