Los números impares positivos en orden ascendente como 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, …. y agrupados como (1), (3, 5), (7, 9, 11), (13, 15, 17, 19),…. y así.
Así, el primer grupo es (1), el segundo grupo es (3, 5) y el tercer grupo es (7, 9, 11), etc. en general, el k-ésimo grupo contiene los siguientes k elementos de la secuencia .
Dado k , encuentra la suma del k- ésimo grupo.
Ejemplos:
Input : k = 3 Output : 27 3rd group is (7, 9, 11) and the sum is 27. Input : k = 1 Output : 1
Método 1: O(n)
La idea es encontrar el primer elemento del k-ésimo grupo.
Por ejemplo:
- El primer elemento del primer grupo es 1, que es el primer número impar.
- El primer elemento del segundo grupo es 3, que es el segundo número impar.
- El primer elemento del tercer grupo es 7, que es el cuarto número impar.
- El primer elemento del cuarto grupo es 13, que es el séptimo número impar.
- y así.
En general, el primer elemento del k-ésimo grupo es el enésimo número impar, donde
n = (1 + 2 + 3 +……+ (k – 1)) + 1.
Ahora, como sabemos, el enésimo número impar es 2n – 1 Esto nos da el primer elemento del k-ésimo grupo. También sabemos que hay k elementos en el grupo, por lo que podemos encontrar su suma simplemente contando hacia arriba desde 2n – 1, por dos, k veces y sumándolos todos. Esto nos da una solución de tiempo lineal.
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; }
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
<?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. ?>
Javascript
<script> // javascript 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. let cur = (k * (k - 1)) + 1; let sum = 0; // Finding the sum. while (k--) { sum += cur; cur += 2; } return sum; } // Driver code let k = 3; document.write(kthgroupsum(k)); // This code is contributed by _saurabh_jaiswal. </script>
Producción :
27
Método 2: O(1)
Una solución complicada es encontrar k 3 . Uno puede observar fácilmente que la suma del k-ésimo grupo será k 3 .
A continuación se muestra la implementación de este enfoque:
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; }
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
<?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. ?>
Javascript
<script> // Efficient Javascript 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 let k = 3; document.write(kthgroupsum(k)); // This code is contributed by _saurabh_jaiswal. </script>
Producción :
27