 1597791600

# Sum of previous numbers that are greater than current number for given array

Given an array A[], for each element in the array, the task is to find the sum of all the previous elements which are strictly greater than the current element.

Examples:

_Input: _A[] = {2, 6, 4, 1, 7}

_Output: _0 0 6 12 0

Explanation:

For 2 and 6 there is no element greater to it on the left.

For 4 there is 6.

For 1 the sum would be 12.

For 7 there is again no element greater to it.

_Input: _A[] = {7, 3, 6, 2, 1}

Output:_ 0 7 7 16 18_

Explanation:

_For 7 there is no element greater to it on the left. _

For 3 there is 7.

For 6 the sum would be 7.

For 2 it has to be 7 + 3 + 6 = 16.

For 1 the sum would be 7 + 3 + 6 + 2 = 18

Naive Approach: For each element, the idea is to find the elements which are strictly greater than the current element on the left side of it and then find the sum of all those elements.

Below is the implementation of the above approach:

• C++

`// C++ program for the above approach`

`#include <bits/stdc++.h>`

`**using**` `**namespace**` `std;`

`// Max Element of the Array`

`**const**` `**int**` `maxn = 1000000;`

`// Function to find the sum of previous`

`// numbers that are greater than the`

`// current number for the given array`

`**void**` `sumGreater(``**int**` `ar[],` `**int**` `N)`

`{`

`// Loop to iterate over all`

`// the elements of the array`

`**for**` `(``**int**` `i = 0; i < N; i++) {`

`// Store the answer for`

`// the current element`

`**int**` `cur_sum = 0;`

`// Iterate from (current index - 1)`

`// to 0 and check if ar[j] is greater`

`// than the current element and add`

`// it to the cur_sum if so`

`**for**` `(``**int**` `j = i - 1; j >= 0; j--) {`

`**if**` `(ar[j] > ar[i])`

`cur_sum += ar[j];`

`}`

`// Print the answer for`

`// current element`

`cout << cur_sum <<` `" "``;`

`}`

`}`

`// Driver Code`

`**int**` `main()`

`{`

`// Given array arr[]`

`**int**` `ar[] = { 7, 3, 6, 2, 1 };`

`// Size of the array`

`**int**` `N =` `**sizeof**` `ar /` `**sizeof**` `ar;`

`// Function call`

`sumGreater(ar, N);`

`**return**` `0;`

`}`

Output:

``````0 7 7 16 18
``````

_Time Complexity: _O(N2)

_Auxiliary Space: _O(1)

Efficient Approach: To optimize the above approach the idea is to use Fenwick Tree. Below are the steps:

1. Traverse the given array and find the sum(say total_sum) of all the elements stored in the Fenwick Tree.
2. Now Consider each element(say arr[i]) as the index of the Fenwick Tree.
3. Now find the sum of all the elements(say curr_sum) which is smaller than the current element using values stored in Tree.
4. The value of total_sum – curr_sum will give the sum of all elements which are strictly greater than the elements on the left side of the current element.
5. Update the current element in the Fenwick Tree.
6. Repeat the above steps for all the elements in the array.

#arrays #competitive programming #tree #binary indexed tree #bit #segment-tree

## Buddha Community  1597791600

## Sum of previous numbers that are greater than current number for given array

Given an array A[], for each element in the array, the task is to find the sum of all the previous elements which are strictly greater than the current element.

Examples:

_Input: _A[] = {2, 6, 4, 1, 7}

_Output: _0 0 6 12 0

Explanation:

For 2 and 6 there is no element greater to it on the left.

For 4 there is 6.

For 1 the sum would be 12.

For 7 there is again no element greater to it.

_Input: _A[] = {7, 3, 6, 2, 1}

Output:_ 0 7 7 16 18_

Explanation:

_For 7 there is no element greater to it on the left. _

For 3 there is 7.

For 6 the sum would be 7.

For 2 it has to be 7 + 3 + 6 = 16.

For 1 the sum would be 7 + 3 + 6 + 2 = 18

Naive Approach: For each element, the idea is to find the elements which are strictly greater than the current element on the left side of it and then find the sum of all those elements.

Below is the implementation of the above approach:

• C++

`// C++ program for the above approach`

`#include <bits/stdc++.h>`

`**using**` `**namespace**` `std;`

`// Max Element of the Array`

`**const**` `**int**` `maxn = 1000000;`

`// Function to find the sum of previous`

`// numbers that are greater than the`

`// current number for the given array`

`**void**` `sumGreater(``**int**` `ar[],` `**int**` `N)`

`{`

`// Loop to iterate over all`

`// the elements of the array`

`**for**` `(``**int**` `i = 0; i < N; i++) {`

`// Store the answer for`

`// the current element`

`**int**` `cur_sum = 0;`

`// Iterate from (current index - 1)`

`// to 0 and check if ar[j] is greater`

`// than the current element and add`

`// it to the cur_sum if so`

`**for**` `(``**int**` `j = i - 1; j >= 0; j--) {`

`**if**` `(ar[j] > ar[i])`

`cur_sum += ar[j];`

`}`

`// Print the answer for`

`// current element`

`cout << cur_sum <<` `" "``;`

`}`

`}`

`// Driver Code`

`**int**` `main()`

`{`

`// Given array arr[]`

`**int**` `ar[] = { 7, 3, 6, 2, 1 };`

`// Size of the array`

`**int**` `N =` `**sizeof**` `ar /` `**sizeof**` `ar;`

`// Function call`

`sumGreater(ar, N);`

`**return**` `0;`

`}`

Output:

``````0 7 7 16 18
``````

_Time Complexity: _O(N2)

_Auxiliary Space: _O(1)

Efficient Approach: To optimize the above approach the idea is to use Fenwick Tree. Below are the steps:

1. Traverse the given array and find the sum(say total_sum) of all the elements stored in the Fenwick Tree.
2. Now Consider each element(say arr[i]) as the index of the Fenwick Tree.
3. Now find the sum of all the elements(say curr_sum) which is smaller than the current element using values stored in Tree.
4. The value of total_sum – curr_sum will give the sum of all elements which are strictly greater than the elements on the left side of the current element.
5. Update the current element in the Fenwick Tree.
6. Repeat the above steps for all the elements in the array.

#arrays #competitive programming #tree #binary indexed tree #bit #segment-tree 1596631020

## Sum of prime numbers in range [L, R] from given Array for Q queries

Given an array arr[] of the size of N followed by an array of Q queries, of the following two types:

• Query Type 1: Given two integers L and R, find the sum of prime elements from index L to R where 0 <= L <= R <= N-1.
• Query Type 2: Given two integers i and X, change arr[i] = X where 0 <= i <= n-1.

Note:_ Every first index of the subquery determines the type of query to be answered._

**Example: **

_Input: _arr[] = {1, 3, 5, 7, 9, 11}, Q = { { 1, 1, 3}, {2, 1, 10}, {1, 1, 3 } }

_Output: _

15

12

_Explanation: _

First query is of type 1, so answer is (3 + 5 + 7), = 15

Second query is of type 2, so arr = 10

Third query is of type 1, where arr = 10, which is not prime hence answer is (5 + 7) = 12

Input:_ arr[] = {1, 2, 35, 7, 14, 11}, Q = { {2, 4, 3}, {1, 4, 5 } }_

Output:_ 14_

Explanation:

First query is of type 2, So update arr = 3

Second query is of type 1, since arr = 3, which is prime. So answer is (3 + 11) = 14

**Naive Approach: **The idea is to iterate for each query between L to R and perform the required operation on the given array.

_Time Complexity: _O(Q * N * (O(sqrt(max(arr[i]))

**Approach: ** To optimize the problem use Segment tree and Sieve Of Eratosthenes.

• First, create a boolean array that will mark the prime numbers.
• Now while making the segment tree only add those array elements as leaf nodes which are prime.
• C++
• Python3

`// C++ program for the above approach`

`#include <bits/stdc++.h>`

`**using**` `**namespace**` `std;`

`**int**` `**const**` `MAX = 1000001;`

`**bool**` `prime[MAX];`

`// Function to find the prime numbers`

`**void**` `SieveOfEratosthenes()`

`{`

`// Create a boolean array prime[]`

`// and initialize all entries it as true`

`// A value in prime[i] will`

`// finally be false if i is Not a prime`

`**memset**``(prime,` `**true**``,` `**sizeof**``(prime));`

`**for**` `(``**int**` `p = 2; p * p <= MAX; p++) {`

`// Check if prime[p] is not`

`// changed, then it is a prime`

`**if**` `(prime[p] ==` `**true**``) {`

`// Update all multiples of p`

`// greater than or equal to`

`// the square of it numbers`

`// which are multiple of p`

`// and are less than p^2 are`

`// already been marked`

`**for**` `(``**int**` `i = p * p; i <= MAX; i += p)`

`prime[i] =` `**false**``;`

`}`

`}`

`}`

`// Function to get the middle`

`// index from corner indexes`

`**int**` `getMid(``**int**` `s,` `**int**` `e)`

`{`

`**return**` `s + (e - s) / 2;`

`}`

`// Function to get the sum of`

`// values in the given range`

`// of the array`

`**int**` `getSumUtil(``**int**``* st,` `**int**` `ss,`

`**int**` `se,` `**int**` `qs,`

`**int**` `qe,` `**int**` `si)`

`{`

`// If segment of this node is a`

`// part of given range, then`

`// return the sum of the segment`

`**if**` `(qs <= ss && qe >= se)`

`**return**` `st[si];`

`// If segment of this node is`

`// outside the given range`

`**if**` `(se < qs || ss > qe)`

`**return**` `0;`

`// If a part of this segment`

`// overlaps with the given range`

`**int**` `mid = getMid(ss, se);`

`**return**` `getSumUtil(st, ss, mid,`

`qs, qe,`

`2 * si + 1)`

`+ getSumUtil(st, mid + 1,`

`se, qs, qe,`

`2 * si + 2);`

`}`

`// Function to update the nodes which`

`// have the given index in their range`

`**void**` `updateValueUtil(``**int**``* st,` `**int**` `ss,`

`**int**` `se,` `**int**` `i,`

`**int**` `diff,` `**int**` `si)`

`{`

`// If the input index lies`

`// outside the range of`

`// this segment`

`**if**` `(i < ss || i > se)`

`**return**``;`

`// If the input index is in`

`// range of this node, then update`

`// the value of the node and its children`

`st[si] = st[si] + diff;`

`**if**` `(se != ss) {`

`**int**` `mid = getMid(ss, se);`

`updateValueUtil(st, ss, mid, i,`

`diff, 2 * si + 1);`

`updateValueUtil(st, mid + 1,`

`se, i, diff,`

`2 * si + 2);`

`}`

`}`

`// Function to update a value in`

`// input array and segment tree`

`**void**` `updateValue(``**int**` `arr[],` `**int**``* st,`

`**int**` `n,` `**int**` `i,`

`**int**` `new_val)`

`{`

`// Check for erroneous input index`

`**if**` `(i < 0 || i > n - 1) {`

`cout <<` `"-1"``;`

`**return**``;`

`}`

`// Get the difference between`

`// new value and old value`

`**int**` `diff = new_val - arr[i];`

`**int**` `prev_val = arr[i];`

`// Update the value in array`

`arr[i] = new_val;`

`// Update the values of`

`// nodes in segment tree`

`// only if either previous`

`// value or new value`

`// or both are prime`

`**if**` `(prime[new_val]`

`|| prime[prev_val]) {`

`// If only new value is prime`

`**if**` `(!prime[prev_val])`

`updateValueUtil(st, 0, n - 1,`

`i, new_val, 0);`

`// If only new value is prime`

`**else**` `**if**` `(!prime[new_val])`

`updateValueUtil(st, 0, n - 1,`

`i, -prev_val, 0);`

`// If both are prime`

`**else**`

`updateValueUtil(st, 0, n - 1,`

`i, diff, 0);`

`}`

`}`

`// Return sum of elements in range`

`// from index qs (quey start) to qe`

`// (query end). It mainly uses getSumUtil()`

`**int**` `getSum(``**int**``* st,` `**int**` `n,` `**int**` `qs,` `**int**` `qe)`

`{`

`// Check for erroneous input values`

`**if**` `(qs < 0 || qe > n - 1 || qs > qe) {`

`cout <<` `"-1"``;`

`**return**` `-1;`

`}`

`**return**` `getSumUtil(st, 0, n - 1,`

`qs, qe, 0);`

`}`

`// Function that constructs Segment Tree`

`**int**` `constructSTUtil(``**int**` `arr[],` `**int**` `ss,`

`**int**` `se,` `**int**``* st,`

`**int**` `si)`

`{`

`// If there is one element in`

`// array, store it in current node of`

`// segment tree and return`

`**if**` `(ss == se) {`

`// Only add those elements in segment`

`// tree which are prime`

`**if**` `(prime[arr[ss]])`

`st[si] = arr[ss];`

`**else**`

`st[si] = 0;`

`**return**` `st[si];`

`}`

`// If there are more than one`

`// elements, then recur for left and`

`// right subtrees and store the`

`// sum of values in this node`

`**int**` `mid = getMid(ss, se);`

`st[si]`

`= constructSTUtil(arr, ss, mid,`

`st, si * 2 + 1)`

`+ constructSTUtil(arr, mid + 1,`

`se, st,`

`si * 2 + 2);`

`**return**` `st[si];`

`}`

`// Function to construct segment`

`// tree from given array`

`**int**``* constructST(``**int**` `arr[],` `**int**` `n)`

`{`

`// Allocate memory for the segment tree`

`// Height of segment tree`

`**int**` `x = (``**int**``)(``**ceil**``(log2(n)));`

`// Maximum size of segment tree`

`**int**` `max_size = 2 * (``**int**``)``**pow**``(2, x) - 1;`

`// Allocate memory`

`**int**``* st =` `**new**` `**int**``[max_size];`

`// Fill the allocated memory st`

`constructSTUtil(arr, 0, n - 1, st, 0);`

`// Return the constructed segment tree`

`**return**` `st;`

`}`

`// Driver code`

`**int**` `main()`

`{`

`**int**` `arr[] = { 1, 3, 5, 7, 9, 11 };`

`**int**` `n =` `**sizeof**``(arr) /` `**sizeof**``(arr);`

`**int**` `Q`

`= { { 1, 1, 3 },`

`{ 2, 1, 10 },`

`{ 1, 1, 3 } };`

`// Function call`

`SieveOfEratosthenes();`

`// Build segment tree from given array`

`**int**``* st = constructST(arr, n);`

`// Print sum of values in`

`// array from index 1 to 3`

`cout << getSum(st, n, 1, 3) << endl;`

`// Update: set arr = 10`

`// and update corresponding`

`// segment tree nodes`

`updateValue(arr, st, n, 1, 10);`

`// Find sum after the value is updated`

`cout << getSum(st, n, 1, 3) << endl;`

`**return**` `0;`

`}`

Output:

``````15
12
``````

Time Complexity:_ O(Q * log N) _

Auxiliary Space:_ O(N)_

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

#advanced data structure #arrays #dynamic programming #hash #mathematical #tree #array-range-queries #prime number #segment-tree #sieve 1624649662

## Cell Phone Number List | Mobile Phone Numbers | Buy Mobile Database

The bottom of the funnel is where your most qualified prospects are. This is ultimately where you want leads to be, as Phone Number List they have shown the most interest. Prospects are ready to purchase at the given stage, but they might be considering other options on the market. This Buy Belgium Business Phone Numbers List is your opportunity to pull out all the stops and demonstrate the value of your offers. Now let’s look at how you can engage leads at each stage (and the tools you need). Awareness stage The awareness stage is when a prospect first learns about your brand. They may land on your website through an ad, a Google search, a post on social media, etc. The main goal here is to simply raise awareness and collect leads. The best way to engage leads in this stage is to educate your audience. Here are different types of content you can publish to build awareness for your brand. #cell #phone #number #list #cold #calling #list #mobile #number #database #mobile #number #list #smscampaignlist #telemarketinglist 1626095071

## Buy Mobile Database & Build Phone Number List

We provide Buy Mobile Database cell phone number list mobile number databsae cold calling list, mobile number list, phone number list, SMS campaign list, SMS marketing list, telemarketing list, fax lists, country email list, b2b email list, c-level executive list, job function email list. Our all database is 95% accurate cleaned and updated. #cell #phone #number #list #cold #calling #list #mobile #number #database #mobile #number #list #smscampaignlist #telemarketinglist 1624649955

## Consumer Phone Lists | Buy Mobile Database | B2C Mobile Number Users

We all need a reason to smile right now. Even though it’s neither Christmas nor Halloween, it’s still the time to have fun. April Fool’s Day is coming! So, you’d better start thinking out practical jokes already. The Buy Mobile Database upcoming Laughter day offers businesses the Austria Business Phone List opportunity to generate their online Phone Number List awareness with hoaxes and original mischief. Want to be in the loop this year and pull it off? Various examples of April Fool’s day sale campaigns from globally-known brands are on the table today.

Love it or hate, the 500-year tradition of pulling someone’s leg will carry on. In a business niche as well. But you should Build Phone Number List REALLY walk on eggshells here. Any joke will fall flat if it doesn’t hit a chord with your audience. That’s why I provide you with these comic April Fool’s day marketing ideas many brands have brought. Enjoy! 1. Play the fool wisely Phone Number List April Fool’s Day marketing campaign by HelpCrunch Not every business risks its reputation and mocks itself. If a good Buy Mobile Database sense of humor, self-deprecation, and an excellent imagination are synonymous to you, take the following April Fool’s marketing endeavor into account. We at HelpCrunch know how to cheer you up this April 1st and show ourselves in a more fun light. Our team crafted this rib-tickler, cool, and absolutely one-of-a-kind alternative April Fool’s day homepage. Yes, we can laugh at ourselves in a professional and quick-witted manner

Let me walk you through our main name of the game. Every business strives to be serious-minded and rather formal. Marketing tries to highlight the product or service is unique and just…perfect. But here’s a twist: an ideal product does not exist. That’s Buy Austria Business Phone Numbers List why we made up Phone Number List our mind to tell you about our downsides upfront and honest, exaggerate them, and laugh. We may not be ideal, though we’re sincere and funny. Hopefully, customers will appreciate our sense of humor. We’ve spent 5 years building this platform…It’s still not even close to what we’ve imagined” – this is where the pun is totally intended. The team modified the content throughout the page in keeping with the best April Fools’ day marketing Phone Number List traditions. For instance, the CTA “Start 14-day free trial” is now changed to “Sign up and live dangerously” and the hero section has our team’s witty quotes. If you Phone Number List like the idea and want to implement it with your brand, make sure you know exactly where the sweet spot is. Do as we do. For those who don’t feel like giggling and prefer rather serious air, there is Cell Phone Numebr list the “MAKE ME LAUGH” switch on our off-the-wall home page. Spread the word and celebrate Laughter day with HelpCrunch! 2. Announce a fake product April Fool’s marketing ideas are impossible to imagine Contact Us without the one from McDonald’s. These Phone Number List guys are real tricksters when it comes to this holiday. McDonald’s clearly understands its audience. The fast-food chain presented special “Sauce pots” several years ago. These were mini milkshakes for those who like dipping their fries in something sweet.

#cell #phone #number #list #cold #calling #list #mobile #number #database #mobile #number #list #smscampaignlist #telemarketinglist