From 3ebdf658cb2a5ec659a6881ad980368a9c64b7ac Mon Sep 17 00:00:00 2001 From: Maheshwar <75472016+Maheshwar01@users.noreply.github.com> Date: Sun, 17 Oct 2021 13:15:26 +0530 Subject: [PATCH 1/2] Create Mo's_algorithm.cpp --- Mo's_algorithm.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Mo's_algorithm.cpp diff --git a/Mo's_algorithm.cpp b/Mo's_algorithm.cpp new file mode 100644 index 0000000..cf9dedb --- /dev/null +++ b/Mo's_algorithm.cpp @@ -0,0 +1,18 @@ +# Python program to compute sum of ranges for different range queries. + +# Function that accepts array and list of queries and print sum of each query +def printQuerySum(arr,Q): + + for q in Q: # Traverse through each query + L,R = q # Extract left and right indices + s = 0 + for i in range(L,R+1): # Compute sum of current query range + s += arr[i] + + print("Sum of",q,"is",s) # Print sum of current query range + +# Driver script +arr = [1, 1, 2, 1, 3, 4, 5, 2, 8] +Q = [[0, 4], [1, 3], [2, 4]] +printQuerySum(arr,Q) +#This code is contributed by Shivam Singh From 08a52230a356c3acf90f3a34d29cee12eda2cf6e Mon Sep 17 00:00:00 2001 From: Maheshwar <75472016+Maheshwar01@users.noreply.github.com> Date: Sun, 17 Oct 2021 14:22:14 +0530 Subject: [PATCH 2/2] Update and rename Mo's_algorithm.cpp to Mo's_algorithm.py --- Mo's_algorithm.cpp | 18 ------------------ Mo's_algorithm.py | 14 ++++++++++++++ 2 files changed, 14 insertions(+), 18 deletions(-) delete mode 100644 Mo's_algorithm.cpp create mode 100644 Mo's_algorithm.py diff --git a/Mo's_algorithm.cpp b/Mo's_algorithm.cpp deleted file mode 100644 index cf9dedb..0000000 --- a/Mo's_algorithm.cpp +++ /dev/null @@ -1,18 +0,0 @@ -# Python program to compute sum of ranges for different range queries. - -# Function that accepts array and list of queries and print sum of each query -def printQuerySum(arr,Q): - - for q in Q: # Traverse through each query - L,R = q # Extract left and right indices - s = 0 - for i in range(L,R+1): # Compute sum of current query range - s += arr[i] - - print("Sum of",q,"is",s) # Print sum of current query range - -# Driver script -arr = [1, 1, 2, 1, 3, 4, 5, 2, 8] -Q = [[0, 4], [1, 3], [2, 4]] -printQuerySum(arr,Q) -#This code is contributed by Shivam Singh diff --git a/Mo's_algorithm.py b/Mo's_algorithm.py new file mode 100644 index 0000000..e4a8ca8 --- /dev/null +++ b/Mo's_algorithm.py @@ -0,0 +1,14 @@ + +def printQuerySum(arr,Q): + + for q in Q: + L,R = q + s = 0 + for i in range(L,R+1): + s += arr[i] + + print("Sum of",q,"is",s) + +arr = [1, 1, 2, 1, 3, 4, 5, 2, 8] +Q = [[0, 4], [1, 3], [2, 4]] +printQuerySum(arr,Q)