Wednesday, October 15, 2025
HomeData Modelling & AIFind the sum of elements of the Matrix generated by the given...

Find the sum of elements of the Matrix generated by the given rules

Given three integers A, B, and R, the task is to find the sum of all the elements of the matrix generated by the given rules: 

  1. The first row will contain a single element which is A and the rest of the elements will be 0.
  2. The next row will contain two elements all of which are (A + B) and the rest are 0s.
  3. Third row will contain (A + B + B) three times and the rest are 0s and so on.
  4. The matrix will contain only R rows.

For example, if A = 5, B = 3 and R = 3 then the matrix will be: 
5 0 0 
8 8 0 
11 11 11
Examples:  

Input: A = 5, B = 3, R = 3 
Output: 54 
5 + 8 + 8 + 11 + 11 + 11 = 54
Input: A = 7, B = 56, R = 1 
Output:

Approach: Initialize sum = 0 and for every 1 ? i ? R update sum = sum + (i * A). After every iteration update A = A + B. Print the final sum in the end.
Below is the implementation of the above approach:  

C++




// C++ implementation of the approach
#include <iostream>
using namespace std;
 
// Function to return the required sum
int sum(int A, int B, int R)
{
 
    // To store the sum
    int sum = 0;
 
    // For every row
    for (int i = 1; i <= R; i++) {
 
        // Update the sum as A appears i number
        // of times in the current row
        sum = sum + (i * A);
 
        // Update A for the next row
        A = A + B;
    }
 
    // Return the sum
    return sum;
}
 
// Driver code
int main()
{
 
    int A = 5, B = 3, R = 3;
    cout << sum(A, B, R);
 
    return 0;
}


Java




// JAVA implementation of the approach
import java.util.*;
import java.lang.*;
import java.io.*;
 
class GFG
{
 
// Function to return the required sum
static int sum(int A, int B, int R)
{
 
    // To store the sum
    int sum = 0;
 
    // For every row
    for (int i = 1; i <= R; i++)
    {
 
        // Update the sum as A appears i number
        // of times in the current row
        sum = sum + (i * A);
 
        // Update A for the next row
        A = A + B;
    }
 
    // Return the sum
    return sum;
}
 
// Driver code
public static void main (String[] args)
              throws java.lang.Exception
{
    int A = 5, B = 3, R = 3;
     
    System.out.print(sum(A, B, R));
}
}
 
// This code is contributed by nidhiva


Python3




# Python3 implementation of the approach
 
# Function to return the required sum
def Sum(A, B, R):
 
    # To store the sum
    ssum = 0
 
    # For every row
    for i in range(1, R + 1):
 
        # Update the sum as A appears i number
        # of times in the current row
        sum = sum + (i * A)
 
        # Update A for the next row
        A = A + B
 
    # Return the sum
    return sum
 
# Driver code
A, B, R = 5, 3, 3
print(Sum(A, B, R))
 
# This code is contributed by Mohit Kumar


C#




// C# implementation of the approach
using System;
 
class GFG
{
 
// Function to return the required sum
static int sum(int A, int B, int R)
{
 
    // To store the sum
    int sum = 0;
 
    // For every row
    for (int i = 1; i <= R; i++)
    {
 
        // Update the sum as A appears i number
        // of times in the current row
        sum = sum + (i * A);
 
        // Update A for the next row
        A = A + B;
    }
 
    // Return the sum
    return sum;
}
 
// Driver code
public static void Main ()
{
    int A = 5, B = 3, R = 3;
     
    Console.Write(sum(A, B, R));
}
}
 
// This code is contributed by anuj_67..


Javascript




<script>
// JAVA SCRIPT  implementation of the approach
// Function to return the required sum
function sum( A,  B,  R)
{
 
    // To store the sum
    let sum = 0;
 
    // For every row
    for (let i = 1; i <= R; i++)
    {
 
        // Update the sum as A appears i number
        // of times in the current row
        sum = sum + (i * A);
 
        // Update A for the next row
        A = A + B;
    }
 
    // Return the sum
    return sum;
}
 
// Driver code
 
    let A = 5, B = 3, R = 3;
     
    document.write(sum(A, B, R));
 
//contributed by bobby
 
</script>


Output: 

54

 

Time Complexity: O(R), since there runs a loop for once from 1 to R.

Auxiliary Space: O(1), since no extra space has been taken.

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11891 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11952 POSTS0 COMMENTS
Shaida Kate Naidoo
6851 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS