Find the sum of elements of the Matrix generated by the given rules Last Updated : 28 May, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report 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: The first row will contain a single element which is A and the rest of the elements will be 0.The next row will contain two elements all of which are (A + B) and the rest are 0s.Third row will contain (A + B + B) three times and the rest are 0s and so on.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 11Examples: Input: A = 5, B = 3, R = 3 Output: 54 5 + 8 + 8 + 11 + 11 + 11 = 54Input: A = 7, B = 56, R = 1 Output: 7 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. Comment More infoAdvertise with us Next Article Find the sum of elements of the Matrix generated by the given rules G gp6 Follow Improve Article Tags : Misc Mathematical Matrix DSA Practice Tags : MathematicalMatrixMisc Similar Reads DSA Tutorial - Learn Data Structures and Algorithms DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on 7 min read Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s 12 min read Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge 14 min read SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e 7 min read Data Structures Tutorial Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st 2 min read Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir 8 min read Breadth First Search or BFS for a Graph Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta 15+ min read Binary Search Algorithm - Iterative and Recursive Implementation Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc 15 min read Insertion Sort Algorithm Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T 9 min read Array Data Structure Guide In this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous 4 min read Like