The positive odd numbers are sorted in ascending order as (1,3,5,7,9...), and grouped as (1),(3,5),(7,9,11), and so on.
Thus, the first group is (1), the second group is (3,5), the third group is (7,9,11), etc. In general, the kth group contains the next k elements of the sequence.
Given k, find the sum of the elements of the kth group. For example, for k = 3, the answer is : 7+9+11 = 27
Find the sum of the elements of the th group.
Explanation:
For a number k, the result will be k*k*k, which is the cube of the number k.
So, simply return the cube of the given number.
Example:
Input: k = 3
Output: 27
Approach
Java
public class NumberOfGroups {public static void main(String[] args) {int k = 3;System.out.println(sumOfGroup(k));}static long sumOfGroup(long k) {long res = k * k * k;return res;}}
C++
#include <bits/stdc++.h>using namespace std;long long sumOfGroup(long long k){long long res = k * k * k;return res;}int main(){int k = 3;cout << sumOfGroup(k) << "\n";return 0;}
No comments:
Post a Comment