Leetcode with dani
الذهاب إلى القناة على Telegram
Join us and let's tackle leet code questions together: improve your problem-solving skills Preparing for coding interviews learning new algorithms and data structures connect with other coding enthusiasts
إظهار المزيد1 270
المشتركون
+124 ساعات
+17 أيام
-430 أيام
أرشيف المشاركات
","datePublished":"2024-08-02T16:07:55Z","dateModified":"2024-08-02T16:08:01Z","author":{"@type":"Organization","name":"Leetcode with dani","url":"https://telemetr.io/ar/channels/1979744670-leetcodeq","image":"https://img.tlmtr.io/c/29YO1E/5832277572212345105?ty=x"},"publisher":{"@type":"Organization","name":"Leetcode with dani","url":"https://telemetr.io/ar/channels/1979744670-leetcodeq","image":"https://img.tlmtr.io/c/29YO1E/5832277572212345105?ty=x"},"commentCount":0,"interactionStatistic":[{"@type":"InteractionCounter","interactionType":"https://schema.org/ViewAction","userInteractionCount":1}]}},{"@type":"ListItem","position":17,"item":{"@type":"SocialMediaPosting","@id":"https://telemetr.io/ar/channels/1979744670-leetcodeq/posts/590","url":"https://telemetr.io/ar/channels/1979744670-leetcodeq/posts/590","mainEntityOfPage":"https://telemetr.io/ar/channels/1979744670-leetcodeq/posts/590","headline":"please follow me on tiktok 🙏eyejemerku nw ena esti aberetatugn","articleBody":"please follow me on tiktok 🙏eyejemerku nw ena esti aberetatugn","datePublished":"2024-08-02T16:07:51Z","dateModified":"2024-08-05T07:01:48Z","author":{"@type":"Organization","name":"Leetcode with dani","url":"https://telemetr.io/ar/channels/1979744670-leetcodeq","image":"https://img.tlmtr.io/c/29YO1E/5832277572212345105?ty=x"},"publisher":{"@type":"Organization","name":"Leetcode with dani","url":"https://telemetr.io/ar/channels/1979744670-leetcodeq","image":"https://img.tlmtr.io/c/29YO1E/5832277572212345105?ty=x"},"commentCount":0,"interactionStatistic":[{"@type":"InteractionCounter","interactionType":"https://schema.org/ViewAction","userInteractionCount":114},{"@type":"InteractionCounter","interactionType":"https://schema.org/ShareAction","userInteractionCount":1}]}},{"@type":"ListItem","position":18,"item":{"@type":"SocialMediaPosting","@id":"https://telemetr.io/ar/channels/1979744670-leetcodeq/posts/589","url":"https://telemetr.io/ar/channels/1979744670-leetcodeq/posts/589","mainEntityOfPage":"https://telemetr.io/ar/channels/1979744670-leetcodeq/posts/589","headline":"https://t.me/zprogramming_bot","articleBody":"https://t.me/zprogramming_bot","datePublished":"2024-08-02T12:12:35Z","dateModified":"2024-08-02T12:12:35Z","author":{"@type":"Organization","name":"Leetcode with dani","url":"https://telemetr.io/ar/channels/1979744670-leetcodeq","image":"https://img.tlmtr.io/c/29YO1E/5832277572212345105?ty=x"},"publisher":{"@type":"Organization","name":"Leetcode with dani","url":"https://telemetr.io/ar/channels/1979744670-leetcodeq","image":"https://img.tlmtr.io/c/29YO1E/5832277572212345105?ty=x"},"commentCount":0,"interactionStatistic":[{"@type":"InteractionCounter","interactionType":"https://schema.org/ViewAction","userInteractionCount":276}]}},{"@type":"ListItem","position":19,"item":{"@type":"SocialMediaPosting","@id":"https://telemetr.io/ar/channels/1979744670-leetcodeq/posts/588","url":"https://telemetr.io/ar/channels/1979744670-leetcodeq/posts/588","mainEntityOfPage":"https://telemetr.io/ar/channels/1979744670-leetcodeq/posts/588","headline":"write your answer in the group or send it with the bot i will review it.","articleBody":"write your answer in the group or send it with the bot i will review it.","datePublished":"2024-08-02T12:12:06Z","dateModified":"2024-08-02T12:12:06Z","author":{"@type":"Organization","name":"Leetcode with dani","url":"https://telemetr.io/ar/channels/1979744670-leetcodeq","image":"https://img.tlmtr.io/c/29YO1E/5832277572212345105?ty=x"},"publisher":{"@type":"Organization","name":"Leetcode with dani","url":"https://telemetr.io/ar/channels/1979744670-leetcodeq","image":"https://img.tlmtr.io/c/29YO1E/5832277572212345105?ty=x"},"commentCount":0,"interactionStatistic":[{"@type":"InteractionCounter","interactionType":"https://schema.org/ViewAction","userInteractionCount":271}]}},{"@type":"ListItem","position":20,"item":{"@type":"SocialMediaPosting","@id":"https://telemetr.io/ar/channels/1979744670-leetcodeq/posts/587","url":"https://telemetr.io/ar/channels/1979744670-leetcodeq/posts/587","mainEntityOfPage":"https://telemetr.io/ar/channels/1979744670-leetcodeq/posts/587","headline":"Prefix Sum Array – Implementation and Applications in Competitive Programming Given an array arr[] of size N,…","articleBody":"Prefix Sum Array – Implementation and Applications in Competitive Programming\n\nGiven an array arr[] of size N, find the prefix sum of the array. A prefix sum array is another array prefixSum[] of the same size, such that the value of prefixSum[i] is arr[0] + arr[1] + arr[2] . . . arr[i].\n\nExamples: \nInput: arr[] = {10, 20, 10, 5, 15}\nOutput: prefixSum[] = {10, 30, 40, 45, 60}\nExplanation: While traversing the array, update the element by adding it with its previous element.\nprefixSum[0] = 10, \nprefixSum[1] = prefixSum[0] + arr[1] = 30, \nprefixSum[2] = prefixSum[1] + arr[2] = 40 and so on.","datePublished":"2024-08-02T12:10:38Z","dateModified":"2024-08-02T12:10:38Z","author":{"@type":"Organization","name":"Leetcode with dani","url":"https://telemetr.io/ar/channels/1979744670-leetcodeq","image":"https://img.tlmtr.io/c/29YO1E/5832277572212345105?ty=x"},"publisher":{"@type":"Organization","name":"Leetcode with dani","url":"https://telemetr.io/ar/channels/1979744670-leetcodeq","image":"https://img.tlmtr.io/c/29YO1E/5832277572212345105?ty=x"},"commentCount":0,"interactionStatistic":[{"@type":"InteractionCounter","interactionType":"https://schema.org/ViewAction","userInteractionCount":296},{"@type":"InteractionCounter","interactionType":"https://schema.org/LikeAction","userInteractionCount":2}]}}]}









1 269
def fillPrefixSum(arr, n, prefixSum):
prefixSum[0] = arr[0]
# Adding present element
# with previous element
for i in range(1, n):
prefixSum[i] = prefixSum[i - 1] + arr[i]
# Driver code
if __name__ == '__main__':
arr = [10, 4, 16, 20]
n = len(arr)
# Function call
prefixSum = [0 for i in range(n + 1)]
fillPrefixSum(arr, n, prefixSum)
for i in range(n):
print(prefixSum[i], " ", end="")
# This code is contributed
# by Anant Agarwal.1 269
#Q18 #leet_codeQ18 Easy
1480 . Given an array nums. We define a running sum of an array as runningSum[i] = sum(nums[0]…nums[i]).
Return the running sum of nums.
Example 1:
Input: nums = [1,2,3,4]
Output: [1,3,6,10]
Explanation: Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4].
Example 2:
Input: nums = [1,1,1,1,1]
Output: [1,2,3,4,5]
Explanation: Running sum is obtained as follows: [1, 1+1, 1+1+1, 1+1+1+1, 1+1+1+1+1].
Example 3:
Input: nums = [3,1,2,10,1]
Output: [3,4,6,16,17]
Constraints:
1 <= nums.length <= 1000
-10^6 <= nums[i] <= 10^6
1 269
Example 2:
Input : Arr[] = {6, 7, 8, 10}, Q = 2
queries[] = {0, 3, 1, 2}
Output : 7 7
1 269
Example 1:
Input : Arr[] = {1, 2, 3, 4, 5}, Q = 3
queries[] = {0, 2, 1, 3, 0, 4}
Output : 2 3 3
Explanation:
Here we can see that the array of
integers is [1, 2, 3, 4, 5].
Query 1: L = 0 and R = 2
Sum = 6
Integer Count = 3
So, Mean is 2
Query 2: L = 1 and R = 3
Sum = 9
Integer Count = 3
So, Mean is 3
Query 3: L = 0 and R = 4
Sum = 15
Integer Count = 5
So, the Mean is 3.
So, In the end, the function will
return the array [2, 3, 3] as an answer.
1 269
Mean of range in array
#Q17 Geeks for Geeks
Given an array of n integers and q queries. Write a program to find floor value of mean in range l to r for each query in a new line.
Queries are given by an array queries[] of size 2*q. Here queries[2*i] denote l and queries[2*i+1] denote r for i-th query (0<= i <q).
1 269
#leet_codeQ16 #Q_209 #Easy #Prefix_sum
1991. Find the Middle Index in Array
Hint
Given a 0-indexed integer array nums, find the leftmost middleIndex (i.e., the smallest amongst all the possible ones).
A middleIndex is an index where nums[0] + nums[1] + ... + nums[middleIndex-1] == nums[middleIndex+1] + nums[middleIndex+2] + ... + nums[nums.length-1].
If middleIndex == 0, the left side sum is considered to be 0. Similarly, if middleIndex == nums.length - 1, the right side sum is considered to be 0.
Return the leftmost middleIndex that satisfies the condition, or -1 if there is no such index.
Example 1:
Input: nums = [2,3,-1,8,4]
Output: 3
Explanation: The sum of the numbers before index 3 is: 2 + 3 + -1 = 4
The sum of the numbers after index 3 is: 4 = 4
Example 2:
Input: nums = [1,-1,4]
Output: 2
Explanation: The sum of the numbers before index 2 is: 1 + -1 = 0
The sum of the numbers after index 2 is: 0
Example 3:
Input: nums = [2,5]
Output: -1
Explanation: There is no valid middleIndex.
Constraints:
1 <= nums.length <= 100
-1000 <= nums[i] <= 1000
1 269
1991. Find the Middle Index in Array
Hint
Given a 0-indexed integer array nums, find the leftmost middleIndex (i.e., the smallest amongst all the possible ones).
A middleIndex is an index where nums[0] + nums[1] + ... + nums[middleIndex-1] == nums[middleIndex+1] + nums[middleIndex+2] + ... + nums[nums.length-1].
If middleIndex == 0, the left side sum is considered to be 0. Similarly, if middleIndex == nums.length - 1, the right side sum is considered to be 0.
Return the leftmost middleIndex that satisfies the condition, or -1 if there is no such index.
Example 1:
Input: nums = [2,3,-1,8,4]
Output: 3
Explanation: The sum of the numbers before index 3 is: 2 + 3 + -1 = 4
The sum of the numbers after index 3 is: 4 = 4
Example 2:
Input: nums = [1,-1,4]
Output: 2
Explanation: The sum of the numbers before index 2 is: 1 + -1 = 0
The sum of the numbers after index 2 is: 0
Example 3:
Input: nums = [2,5]
Output: -1
Explanation: There is no valid middleIndex.
Constraints:
1 <= nums.length <= 100
-1000 <= nums[i] <= 1000
1 269
Answer:
def prefix_sum(arr):
prefix_sum_arr = [0] * len(arr)
prefix_sum_arr[0] = arr[0]
for i in range(1, len(arr)):
prefix_sum_arr[i] = prefix_sum_arr[i - 1] + arr[i]
return prefix_sum_arr
arr = [10, 20, 10, 5, 15]
result = prefix_sum(arr)
print(result) # Output: [10, 30, 40, 45, 60]1 269
<blockquote class="tiktok-embed" cite="https://www.tiktok.com/@b_edit12" data-unique-id="b_edit12" data-embed-type="creator" style="max-width: 780px; min-width: 288px;" > <section> <a target="_blank" href="https://www.tiktok.com/@b_edit12?refer=creator_embed">@b_edit12</a> </section> </blockquote> <script async src="https://www.tiktok.com/embed.js"></script>
1 269
Prefix Sum Array – Implementation and Applications in Competitive Programming
Given an array arr[] of size N, find the prefix sum of the array. A prefix sum array is another array prefixSum[] of the same size, such that the value of prefixSum[i] is arr[0] + arr[1] + arr[2] . . . arr[i].
Examples:
Input: arr[] = {10, 20, 10, 5, 15}
Output: prefixSum[] = {10, 30, 40, 45, 60}
Explanation: While traversing the array, update the element by adding it with its previous element.
prefixSum[0] = 10,
prefixSum[1] = prefixSum[0] + arr[1] = 30,
prefixSum[2] = prefixSum[1] + arr[2] = 40 and so on.
