Find the Alpha Score of the Given Steps (Using BST)
Given an array A[] consisting of N numbers denoting the values written on N steps, the task is to find the alpha score of the total journey of climbing up all the stairs. Since the answer can be very large, print the answer modulo 109 + 7.
Alpha Score: The alpha score at each step is the sum of all the numbers previously seen on the stairs climbed which are smaller than the number on the current stair.
The alpha score of the total journey is the sum of the alpha scores of each step.
Examples:
Input: A[] = {13, 14, 20}
Output: 87
Explanation:
Alpha Score at the first stair = 13
Alpha Score at the second stair = 13 + 14 = 27
Alpha Score of the third stair = 13 + 14 + 20 = 47
Sum of all the Alpha Scores=13 + 27 + 47 = 87
Therefore, the Alpha Score of the total journey is 87.
Input: A[] = {10, 11, 12}
Output: 64
Explanation:
Alpha Score at the first stair = 10
Alpha Score at the second stair = 10 + 11 = 21
Alpha Score of the third stair = 10+11 + 12 = 33
Sum of all the Alpha Scores =10 + 21 + 33
Therefore, the Alpha Score of the total journey is 64.
Naive Approach:
The simplest approach to solve the problem is to traverse each element in the array and calculate the sum of all the elements smaller than the current element present at previous indices to calculate the Alpha Score of the current stair. For each sum calculated, update the total_sum(Alpha Score of the total journey). Finally, print the total_sum as the Alpha Score of the complete journey.
Below is the implementation of the approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findAlphaScore( int A[], int n) {
int mod = 1e9 + 7;
int total_sum = 0;
for ( int i = 0; i < n; i++) {
int sum = A[i];
for ( int j = 0; j < i; j++) {
if (A[j] < A[i]) {
sum = (sum + A[j]) % mod;
}
}
total_sum = (total_sum + sum) % mod;
}
return total_sum;
}
int main() {
int A[] = { 10, 11, 12 };
int n = sizeof (A) / sizeof (A[0]);
int alphaScore = findAlphaScore(A, n);
cout << alphaScore << endl;
return 0;
}
|
Java
import java.util.*;
public class GFG {
public static int findAlphaScore( int A[], int n)
{
int mod = 1000000007 ;
int total_sum
= 0 ;
for ( int i = 0 ; i < n; i++) {
int sum = A[i];
for ( int j = 0 ; j < i; j++) {
if (A[j] < A[i]) {
sum = (sum + A[j]) % mod;
}
}
total_sum = (total_sum + sum) % mod;
}
return total_sum;
}
public static void main(String[] args)
{
int A[] = { 10 , 11 , 12 };
int n = A.length;
int alphaScore = findAlphaScore(A, n);
System.out.println(alphaScore);
}
}
|
Python3
def find_alpha_score(arr):
mod = 10 * * 9 + 7
total_sum = 0
for i in range ( len (arr)):
s = arr[i]
for j in range (i):
if arr[j] < arr[i]:
s = (s + arr[j]) % mod
total_sum = (total_sum + s) % mod
return total_sum
arr = [ 10 , 11 , 12 ]
alpha_score = find_alpha_score(arr)
print (alpha_score)
|
C#
using System;
public class MainClass {
public static long FindAlphaScore( int [] arr) {
int mod = 1000000007;
long totalSum = 0;
for ( int i = 0; i < arr.Length; i++) {
int s = arr[i];
for ( int j = 0; j < i; j++) {
if (arr[j] < arr[i]) {
s = (s + arr[j]) % mod;
}
}
totalSum = (totalSum + s) % mod;
}
return totalSum;
}
public static void Main( string [] args) {
int [] arr = { 10, 11, 12 };
long alphaScore = FindAlphaScore(arr);
Console.WriteLine(alphaScore);
}
}
|
Javascript
function findAlphaScore(arr) {
const mod = 1000000007;
let totalSum = 0;
for (let i = 0; i < arr.length; i++) {
let s = arr[i];
for (let j = 0; j < i; j++) {
if (arr[j] < arr[i]) {
s = (s + arr[j]) % mod;
}
}
totalSum = (totalSum + s) % mod;
}
return totalSum;
}
const arr = [10, 11, 12];
const alphaScore = findAlphaScore(arr);
console.log(alphaScore);
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach:
The above approach can be optimized using Binary Search Trees. Follow the steps below:
- Sort the given array.
- Construct a BST from the sorted array.
- Recursively traverse the BST and follow the steps below:
- Traverse the left subtree.
- Add the value of the current node to sum (Alpha score for current stair) and update the total_sum (Alpha score of the journey till now).
- Traverse the right subtree.
- After complete traversal of the BST, print the total_sum.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
static long sum = 0, total_sum = 0;
static long mod = 1000000007;
struct Node
{
Node *left, *right;
int data;
Node( int x)
{
data = x;
left = NULL;
right = NULL;
}
};
long getAlphaScore(Node* node)
{
if (node->left != NULL)
getAlphaScore(node->left);
sum = (sum + node->data) % mod;
total_sum = (total_sum + sum) % mod;
if (node->right != NULL)
getAlphaScore(node->right);
return total_sum;
}
Node* constructBST( int arr[], int start,
int end, Node* root)
{
if (start > end)
return NULL;
int mid = (start + end) / 2;
if (root == NULL)
root = new Node(arr[mid]);
root->left = constructBST(arr, start,
mid - 1, root->left);
root->right = constructBST(arr, mid + 1, end,
root->right);
return root;
}
int main()
{
int arr[] = { 10, 11, 12 };
int length = 3;
sort(arr, arr + length);
Node *root = NULL;
root = constructBST(arr, 0, length - 1, root);
cout << (getAlphaScore(root));
}
|
Java
import java.lang.*;
import java.util.*;
class Node {
Node left, right;
int data;
public Node( int data)
{
this .data = data;
left = null ;
right = null ;
}
}
class AlphaScore {
Node root;
AlphaScore() { root = null ; }
static long sum = 0 , total_sum = 0 ;
static long mod = 1000000007 ;
public static long getAlphaScore(Node node)
{
if (node.left != null )
getAlphaScore(node.left);
sum = (sum + node.data) % mod;
total_sum = (total_sum + sum) % mod;
if (node.right != null )
getAlphaScore(node.right);
return total_sum;
}
public static Node constructBST( int [] arr, int start,
int end, Node root)
{
if (start > end)
return null ;
int mid = (start + end) / 2 ;
if (root == null )
root = new Node(arr[mid]);
root.left
= constructBST(arr, start, mid - 1 , root.left);
root.right
= constructBST(arr, mid + 1 , end, root.right);
return root;
}
public static void main(String args[])
{
int arr[] = { 10 , 11 , 12 };
int length = arr.length;
Arrays.sort(arr);
Node root = null ;
root = constructBST(arr, 0 , length - 1 , root);
System.out.println(getAlphaScore(root));
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self .left = None
self .right = None
sum = 0
total_sum = 0
mod = 1000000007
def getAlphaScore(node):
global sum
global total_sum
if node.left ! = None :
getAlphaScore(node.left)
sum = ( sum + node.data) % mod
total_sum = (total_sum + sum ) % mod
if node.right ! = None :
getAlphaScore(node.right)
return total_sum
def constructBST(arr, start, end, root):
if start > end:
return None
mid = (start + end) / / 2
if root = = None :
root = Node(arr[mid])
root.left = constructBST(arr, start,
mid - 1 ,
root.left)
root.right = constructBST(arr, mid + 1 ,
end, root.right)
return root
arr = [ 10 , 11 , 12 ]
length = len (arr)
arr.sort()
root = None
root = constructBST(arr, 0 , length - 1 , root)
print (getAlphaScore(root))
|
C#
using System;
class Node
{
public Node left, right;
public int data;
public Node( int data)
{
this .data = data;
left = null ;
right = null ;
}
}
class AlphaScore{
Node root;
AlphaScore(){root = null ;}
static long sum = 0, total_sum = 0;
static long mod = 1000000007;
static long getAlphaScore(Node node)
{
if (node.left != null )
getAlphaScore(node.left);
sum = (sum + node.data) % mod;
total_sum = (total_sum + sum) % mod;
if (node.right != null )
getAlphaScore(node.right);
return total_sum;
}
static Node constructBST( int [] arr, int start,
int end, Node root)
{
if (start > end)
return null ;
int mid = (start + end) / 2;
if (root == null )
root = new Node(arr[mid]);
root.left = constructBST(arr, start,
mid - 1, root.left);
root.right = constructBST(arr, mid + 1,
end, root.right);
return root;
}
public static void Main(String []args)
{
int []arr = { 10, 11, 12 };
int length = arr.Length;
Array.Sort(arr);
Node root = null ;
root = constructBST(arr, 0, length - 1, root);
Console.WriteLine(getAlphaScore(root));
}
}
|
Javascript
<script>
class Node
{
constructor(data)
{
this .data = data;
this .left = null ;
this .right = null ;
}
}
var root = null ;
function AlphaScore(){root = null ;}
var sum = 0, total_sum = 0;
var mod = 1000000007;
function getAlphaScore(node)
{
if (node.left != null )
getAlphaScore(node.left);
sum = (sum + node.data) % mod;
total_sum = (total_sum + sum) % mod;
if (node.right != null )
getAlphaScore(node.right);
return total_sum;
}
function constructBST(arr, start, end, root)
{
if (start > end)
return null ;
var mid = parseInt((start + end) / 2);
if (root == null )
root = new Node(arr[mid]);
root.left = constructBST(arr, start,
mid - 1, root.left);
root.right = constructBST(arr, mid + 1,
end, root.right);
return root;
}
var arr = [10, 11, 12];
var length = arr.length;
arr.sort();
var root = null ;
root = constructBST(arr, 0, length - 1, root);
document.write(getAlphaScore(root));
</script>
|
Time Complexity: O(NlogN)
Auxiliary Space: O(N)
Last Updated :
09 May, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...