The positive odd numbers in ascending order as 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, …. and grouped as (1), (3, 5), (7, 9, 11), (13, 15, 17, 19), …. and so on.
Thus, the first group is (1), the second group is (3, 5) and 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 kth group.
Examples :
Input : k = 3
Output : 27
3rd group is (7, 9, 11) and the sum is 27.
Input : k = 1
Output : 1
Method 1: O(n)
The idea is to find the first element of the kth group.
For example:
- The 1st group’s first element is 1, which is the 1st odd number.
- The 2nd group’s first element is 3, which is the 2nd odd number.
- The 3rd group’s first element is 7, which is the 4th odd number.
- The 4th group’s first element is 13, which is the 7st odd number.
- and so on.
In general, the kth group’s first element is the nth odd number, where
n = (1 + 2 + 3 +……+ (k – 1)) + 1.
Now, as we know, the nth odd number is 2n – 1. This gives us the first element of the kth group. We also know that there are k elements in the group, so we can find their sum by simply counting upwards from 2n – 1, by two, k times and adding them all. This gives us a linear time solution.
C++
#include <bits/stdc++.h>
using namespace std;
int kthgroupsum( int k)
{
int cur = (k * (k - 1)) + 1;
int sum = 0;
while (k--) {
sum += cur;
cur += 2;
}
return sum;
}
int main()
{
int k = 3;
cout << kthgroupsum(k) << endl;
return 0;
}
|
Java
import java.util.Arrays;
import java.util.Collections;
class GFG
{
public static int kthgroupsum( int k)
{
int cur = (k * (k - 1 )) + 1 ;
int sum = 0 ;
while (k-- > 0 )
{
sum += cur;
cur += 2 ;
}
return sum;
}
public static void main(String[] args)
{
int k = 3 ;
System.out.print(kthgroupsum(k));
}
}
|
Python3
def kthgroupsum( k ):
cur = int ((k * (k - 1 )) + 1 )
sum = 0
while k:
sum + = cur
cur + = 2
k = k - 1
return sum
k = 3
print (kthgroupsum(k))
|
C#
using System;
class GFG {
public static int kthgroupsum( int k)
{
int cur = (k * (k - 1)) + 1;
int sum = 0;
while (k-- > 0) {
sum += cur;
cur += 2;
}
return sum;
}
public static void Main()
{
int k = 3;
Console.WriteLine(kthgroupsum(k));
}
}
|
PHP
<?php
function kthgroupsum( $k )
{
$cur = ( $k * ( $k - 1)) + 1;
$sum = 0;
while ( $k --)
{
$sum += $cur ;
$cur += 2;
}
return $sum ;
}
$k = 3;
echo kthgroupsum( $k ) ;
?>
|
Javascript
<script>
function kthgroupsum(k)
{
let cur = (k * (k - 1)) + 1;
let sum = 0;
while (k--)
{
sum += cur;
cur += 2;
}
return sum;
}
let k = 3;
document.write(kthgroupsum(k));
</script>
|
Output :
27
Time complexity: O(k)
Auxiliary space: O(1)
Method 2 : O(1)
An tricky solution is to find k3. One can easily observe, the sum of kth group will be k3.
Below is the implementation of this approach:
C++
#include <bits/stdc++.h>
using namespace std;
int kthgroupsum( int k)
{
return k * k * k;
}
int main()
{
int k = 3;
cout << kthgroupsum(k) << endl;
return 0;
}
|
Java
import java.util.Arrays;
import java.util.Collections;
class GFG
{
public static int kthgroupsum( int k)
{
return k * k * k;
}
public static void main(String[] args)
{
int k = 3 ;
System.out.print( kthgroupsum(k));
}
}
|
Python3
def kthgroupsum( k ):
return k * k * k
k = 3
print (kthgroupsum(k))
|
C#
using System;
class GFG {
public static int kthgroupsum( int k)
{
return k * k * k;
}
public static void Main()
{
int k = 3;
Console.WriteLine(kthgroupsum(k));
}
}
|
PHP
<?php
function kthgroupsum( $k )
{
return $k * $k * $k ;
}
$k = 3;
echo kthgroupsum( $k );
?>
|
Javascript
<script>
function kthgroupsum(k)
{
return k * k * k;
}
let k = 3;
document.write(kthgroupsum(k));
</script>
|
Output :
27
Time Complexity: O(1)
Auxiliary Space: O(1)