Average of first n odd naturals numbers
Last Updated :
23 Jan, 2023
Given a Number n then find the Average of first n odd numbers
1 + 3 + 5 + 7 + 9 +………….+ (2n – 1)
Examples :
Input : 5
Output : 5
(1 + 3 + 5 + 7 + 9)/5 = 5
Input : 10
Output : 10
(1 + 3 + 5 + 7 + 9 + 11 + 13 + 15 + 17 + 19)/10 =10
Method 1 ( Naive Approach:)
A simple solution is to iterate loop from 1 to n time. Through sum of all odd numbers and divided by n.This solution take O(N) time.
C++
#include <iostream>
using namespace std;
int avg_of_odd_num( int n)
{
int sum = 0;
for ( int i = 0; i < n; i++)
sum += (2 * i + 1);
return sum / n;
}
int main()
{
int n = 20;
cout << avg_of_odd_num(n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int avg_of_odd_num( int n)
{
int sum = 0 ;
for ( int i = 0 ; i < n; i++)
sum += ( 2 * i + 1 );
return sum / n;
}
public static void main(String[] args)
{
int n = 20 ;
avg_of_odd_num(n);
System.out.println(avg_of_odd_num(n));
}
}
|
Python3
def avg_of_odd_num(n) :
sum = 0
for i in range ( 0 , n) :
sum = sum + ( 2 * i + 1 )
return sum / / n
n = 20
print (avg_of_odd_num(n))
|
C#
using System;
class GFG {
static int avg_of_odd_num( int n)
{
int sum = 0;
for ( int i = 0; i < n; i++)
sum += (2 * i + 1);
return sum / n;
}
public static void Main()
{
int n = 20;
avg_of_odd_num(n);
Console.Write(avg_of_odd_num(n));
}
}
|
PHP
<?php
function avg_of_odd_num( $n )
{
$sum = 0;
for ( $i = 0; $i < $n ; $i ++)
$sum += (2 * $i + 1);
return $sum / $n ;
}
$n = 20;
echo (avg_of_odd_num( $n ));
?>
|
Javascript
<script>
function avg_of_odd_num( n)
{
let sum = 0;
for (let i = 0; i < n; i++)
sum += (2 * i + 1);
return sum / n;
}
let n = 20;
document.write(avg_of_odd_num(n));
</script>
|
Output :
20
Time Complexity: O(n)
Auxiliary Space: O(1)
Method 2 (Efficient Approach:)
The idea is the sum of first n odd number is n2, for find the Average of first n odd numbers so it is divide by n, hence formula is n2/n = n. it take O(1) time.
Avg of sum of first N odd Numbers = N
C++
#include <bits/stdc++.h>
using namespace std;
int avg_of_odd_num( int n)
{
return n;
}
int main()
{
int n = 8;
cout << avg_of_odd_num(n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int avg_of_odd_num( int n)
{
return n;
}
public static void main(String[] args)
{
int n = 8 ;
System.out.println(avg_of_odd_num(n));
}
}
|
Python3
def avg_of_odd_num(n) :
return n
n = 8
print (avg_of_odd_num(n))
|
C#
using System;
class GFG {
static int avg_of_odd_num( int n)
{
return n;
}
public static void Main()
{
int n = 8;
Console.Write(avg_of_odd_num(n));
}
}
|
PHP
<?php
function avg_of_odd_num( $n )
{
return $n ;
}
$n = 8;
echo (avg_of_odd_num( $n ));
?>
|
Javascript
<script>
function avg_of_odd_num(n)
{
return n;
}
var n = 8;
document.write(avg_of_odd_num(n));
</script>
|
Output :
8
Time Complexity : O(1)
Space Complexity: O(1) since using constant variables
Proof
Sum of first n terms of an A.P.(Arithmetic Progression)
= (n/2) * [2*a + (n-1)*d].....(i)
where, a is the first term of the series
and d is the difference between the adjacent
terms of the series.
Here, a = 1, d = 2, applying these values to e. q.,
(i), we get
Sum = (n/2) * [2*1 + (n-1)*2]
= (n/2) * [2 + 2*n - 2]
= (n/2) * (2*n)
= n*n
= n2
Avg of first n odd numbers = n2/n
= n
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...