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 k^{th} group contains the next k elements of the sequence.

Given **k**, find the sum of the k^{th} 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 2st odd number.
- The 3rd group’s first element is 7, which is the 4st odd number.
- The 4st 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++

`// CPP program to find sum of k-th group of ` `// positive odd integers. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Return the sum of k-th group of positive ` `// odd integers. ` `int` `kthgroupsum(` `int` `k) ` `{ ` ` ` `// Finding first element of kth group. ` ` ` `int` `cur = (k * (k - 1)) + 1; ` ` ` `int` `sum = 0; ` ` ` ` ` `// Finding the sum. ` ` ` `while` `(k--) { ` ` ` `sum += cur; ` ` ` `cur += 2; ` ` ` `} ` ` ` ` ` `return` `sum; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `k = 3; ` ` ` `cout << kthgroupsum(k) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java code to find sum of k-th group ` `// of positive odd integers. ` `import` `java.util.Arrays; ` `import` `java.util.Collections; ` ` ` `class` `GFG ` `{ ` ` ` `// Return the sum of k-th group ` ` ` `// of positive odd integers. ` ` ` `public` `static` `int` `kthgroupsum(` `int` `k) ` ` ` `{ ` ` ` `// Finding first element of kth group. ` ` ` `int` `cur = (k * (k - ` `1` `)) + ` `1` `; ` ` ` `int` `sum = ` `0` `; ` ` ` ` ` `// Finding the sum. ` ` ` `while` `(k-- > ` `0` `) ` ` ` `{ ` ` ` `sum += cur; ` ` ` `cur += ` `2` `; ` ` ` `} ` ` ` ` ` `return` `sum; ` ` ` `} ` ` ` ` ` `// Driver program to test above methods ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `k = ` `3` `; ` ` ` `System.out.print(kthgroupsum(k)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Chhavi ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 code to find sum of k-th group ` `# of positive odd integers. ` ` ` `# Return the sum of k-th group of ` `# positive odd integers. ` `def` `kthgroupsum( k ): ` ` ` ` ` `# Finding first element of kth group. ` ` ` `cur ` `=` `int` `((k ` `*` `(k ` `-` `1` `)) ` `+` `1` `) ` ` ` `sum` `=` `0` ` ` ` ` `# Finding the sum. ` ` ` `while` `k: ` ` ` `sum` `+` `=` `cur ` ` ` `cur ` `+` `=` `2` ` ` `k` `=` `k` `-` `1` ` ` ` ` `return` `sum` ` ` `# Driver code ` `k ` `=` `3` `print` `(kthgroupsum(k)) ` ` ` `# This code is contributed by "Sharad_Bhardwaj". ` |

*chevron_right*

*filter_none*

## C#

`// C# code to find sum of k-th group ` `// of positive odd integers. ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Return the sum of k-th group ` ` ` `// of positive odd integers. ` ` ` `public` `static` `int` `kthgroupsum(` `int` `k) ` ` ` `{ ` ` ` ` ` `// Finding first element of kth group. ` ` ` `int` `cur = (k * (k - 1)) + 1; ` ` ` `int` `sum = 0; ` ` ` ` ` `// Finding the sum. ` ` ` `while` `(k-- > 0) { ` ` ` `sum += cur; ` ` ` `cur += 2; ` ` ` `} ` ` ` ` ` `return` `sum; ` ` ` `} ` ` ` ` ` `// Driver program to test above methods ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `k = 3; ` ` ` ` ` `Console.WriteLine(kthgroupsum(k)); ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find ` `// sum of k-th group of ` `// positive odd integers. ` ` ` `// Return the sum of ` `// k-th group of positive ` `// odd integers. ` `function` `kthgroupsum(` `$k` `) ` `{ ` ` ` ` ` `// Finding first element ` ` ` `// of kth group. ` ` ` `$cur` `= (` `$k` `* (` `$k` `- 1)) + 1; ` ` ` `$sum` `= 0; ` ` ` ` ` `// Finding the sum. ` ` ` `while` `(` `$k` `--) ` ` ` `{ ` ` ` `$sum` `+= ` `$cur` `; ` ` ` `$cur` `+= 2; ` ` ` `} ` ` ` `return` `$sum` `; ` `} ` ` ` `// Driver code ` `$k` `= 3; ` `echo` `kthgroupsum(` `$k` `) ; ` ` ` `// This code is contributed by anuj_67. ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

27

**Method 2 : O(1)**

An tricky solution is to find k^{3}. One can easily observe, the sum of kth group will be k^{3}.

Below is the implementation of this approach:

## C++

`// Efficient CPP program to find sum of k-th ` `// group of positive odd integers. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Return the sum of kth group of positive ` `// odd integer. ` `int` `kthgroupsum(` `int` `k) ` `{ ` ` ` `return` `k * k * k; ` `} ` ` ` `// Driven Program ` `int` `main() ` `{ ` ` ` `int` `k = 3; ` ` ` `cout << kthgroupsum(k) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Efficient Java code to find sum of k-th ` `// group of positive odd integers. ` `import` `java.util.Arrays; ` `import` `java.util.Collections; ` ` ` `class` `GFG ` `{ ` ` ` `// Return the sum of kth group of ` ` ` `// positive odd integer. ` ` ` `public` `static` `int` `kthgroupsum(` `int` `k) ` ` ` `{ ` ` ` `return` `k * k * k; ` ` ` `} ` ` ` ` ` `// Driver program to test above methods ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `k = ` `3` `; ` ` ` `System.out.print( kthgroupsum(k)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Chhavi ` |

*chevron_right*

*filter_none*

## Python3

`# Efficient Python code to find sum of ` `# k-th group of positive odd integers. ` ` ` `# Return the sum of kth group of positive ` `# odd integer. ` `def` `kthgroupsum( k ): ` ` ` `return` `k ` `*` `k ` `*` `k ` ` ` `# Driver code ` `k ` `=` `3` `print` `(kthgroupsum(k)) ` ` ` `# This code is contributed by "Sharad_Bhardwaj". ` |

*chevron_right*

*filter_none*

## C#

`// Efficient C# code to find sum of k-th ` `// group of positive odd integers. ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Return the sum of kth group of ` ` ` `// positive odd integer. ` ` ` `public` `static` `int` `kthgroupsum(` `int` `k) ` ` ` `{ ` ` ` `return` `k * k * k; ` ` ` `} ` ` ` ` ` `// Driver program to test above methods ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `k = 3; ` ` ` ` ` `Console.WriteLine(kthgroupsum(k)); ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// Efficient PHP program to ` `// find sum of k-th group of ` `// positive odd integers. ` ` ` ` ` `// Return the sum of kth group ` `// of positive odd integer. ` `function` `kthgroupsum( ` `$k` `) ` `{ ` ` ` `return` `$k` `* ` `$k` `*` `$k` `; ` `} ` ` ` `// Driven Code ` `$k` `= 3; ` `echo` `kthgroupsum(` `$k` `); ` ` ` `// This code is contributed by anuj_67. ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

27

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count pairs in Array whose product is a Kth power of any positive integer
- Multiplication table till N rows where every Kth row is table of K upto Kth term
- Nth term where K+1th term is product of Kth term with difference of max and min digit of Kth term
- Find kth smallest number in range [1, n] when all the odd numbers are deleted
- Count set bits in the Kth number after segregating even and odd from N natural numbers
- Find K distinct positive odd integers with sum N
- Find sum of N-th group of Natural Numbers
- Minimum positive integer to divide a number such that the result is an odd
- Find the number of positive integers less than or equal to N that have an odd number of digits
- Minimize the sum of the squares of the sum of elements of each group the array is divided into
- Kth odd number in an array
- Average of odd numbers till a given odd number
- Count ordered pairs of positive numbers such that their sum is S and XOR is K
- Represent a number as the sum of positive numbers ending with 9
- Sum of kth powers of first n natural numbers
- Find the sum of prime numbers in the Kth array
- Sum of numbers in the Kth level of a Fibonacci triangle
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
- Sum of maximum and minimum of Kth subset ordered by increasing subset sum
- Count numbers in given range such that sum of even digits is greater than sum of odd digits

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.