升序为1、3、5、7、9、11、13、15、17、19,…的正奇数。并分为(1),(3、5),(7、9、11),(13、15、17、19),…。等等。
因此,第一组是(1),第二组是(3,5),第三组是(7,9,11),依此类推,通常,第k个组包含该序列的后k个元素。
给定k ,求出第k个组的总和。
例子 :
Input : k = 3
Output : 27
3rd group is (7, 9, 11) and the sum is 27.
Input : k = 1
Output : 1
方法1:O(n)
这个想法是找到第k个组的第一个元素。
例如:
- 第一组的第一个元素为1,即第一个奇数。
- 第二组的第一个元素是3,这是第二个奇数。
- 第三组的第一个元素是7,这是第四个奇数。
- 第四组的第一个元素是13,这是第七个奇数。
- 等等。
通常,第k个组的第一个元素是第n个奇数,其中
n =(1 + 2 + 3 +……+(k – 1))+ 1。
现在,我们知道,第n个奇数是2n –1。这使我们成为第k个组的第一个元素。我们还知道该组中有k个元素,因此我们可以通过简单地从2n – 1向上计数两次,k次并将它们全部相加来找到它们的总和。这为我们提供了线性时间解决方案。
C++
// CPP program to find sum of k-th group of
// positive odd integers.
#include
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;
}
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
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".
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.
PHP
Javascript
C++
// Efficient CPP program to find sum of k-th
// group of positive odd integers.
#include
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;
}
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
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".
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.
PHP
Javascript
输出 :
27
方法2:O(1)
一个棘手的解决方案是找到k 3 。可以很容易地观察到,第k组的总和为k 3 。
以下是此方法的实现:
C++
// Efficient CPP program to find sum of k-th
// group of positive odd integers.
#include
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;
}
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
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".
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.
的PHP
Java脚本
输出 :
27