Given two numbers, a and b. Compute the average of the two numbers.
The well know formula (a + b) / 2 may fail at the following case :
If, a = b = (2^31) – 1; i.e. INT_MAX.
Now, (a+b) will cause overflow and hence formula (a + b) / 2 wont work
Below is the implementation :
C++
#include <bits/stdc++.h>
using namespace std;
int compute_average( int a, int b)
{
return (a + b) / 2;
}
int main()
{
int a = INT_MAX, b = INT_MAX;
cout << "Actual average : " << INT_MAX << endl;
cout << "Computed average : " << compute_average(a, b);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int compute_average( int a, int b)
{
return (a + b) / 2 ;
}
public static void main (String[] args) {
int a = Integer.MAX_VALUE;
int b = Integer.MAX_VALUE;
System.out.println( "Actual average : " + Integer.MAX_VALUE);
System.out.println( "Computed average : " + compute_average(a, b));
}
}
|
Python3
import sys
from math import floor
INT_MAX = 2147483647
def compute_average(a, b):
return floor((a + b) / 2 )
if __name__ = = '__main__' :
a = INT_MAX
b = - INT_MAX - 1
print ( "Actual average : " , INT_MAX)
print ( "Computed average : " ,
compute_average(a, b))
|
C#
using System;
public class GFG{
static int compute_average( int a, int b)
{
return (a + b) / 2;
}
static public void Main (){
int a = int .MaxValue;
int b = int .MaxValue;
Console.WriteLine( "Actual average : " + int .MaxValue);
Console.WriteLine( "Computed average : " + compute_average(a, b));
}
}
|
Javascript
<script>
const INT_MAX = 2147483647;
function compute_average(a, b)
{
return Math.floor((a + b) / 2);
}
let a = INT_MAX;
let b = -INT_MAX-1;
document.write( "Actual average : " + INT_MAX + "</br>" );
document.write( "Computed average : " + compute_average(a, b) + "</br>" );
</script>
|
Output:
Actual average : 2147483647
Computed average : -1
Time Complexity: O(1), the code will run in a constant time.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Improved Formula that does not cause overflow :
Average = (a / 2) + (b / 2) + (((a % 2) + (b % 2)) / 2)
Below is the implementation :
C++
#include <bits/stdc++.h>
using namespace std;
int compute_average( int a, int b)
{
return (a / 2) + (b / 2) + ((a % 2 + b % 2) / 2);
}
int main()
{
int a = INT_MAX, b = INT_MAX;
cout << "Actual average : " << INT_MAX << endl;
cout << "Computed average : " << compute_average(a, b);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int compute_average( int a,
int b)
{
return (a / 2 ) + (b / 2 ) +
((a % 2 + b % 2 ) / 2 );
}
public static void main (String[] args)
{
int a = Integer.MAX_VALUE;
int b = Integer.MAX_VALUE;
System.out.println( "Actual average : " +
Integer.MAX_VALUE);
System.out.print( "Computed average : " );
System.out.println(compute_average(a, b));
}
}
|
Python3
INT_MAX = 2147483647
def compute_average(a,b):
return (a / / 2 ) + (b / / 2 ) + ((a % 2 + b % 2 ) / / 2 )
if __name__ = = "__main__" :
a = INT_MAX
b = INT_MAX
print ( "Actual average : " ,INT_MAX)
print ( "Computed average : " ,
compute_average(a, b))
|
C#
using System;
class GFG
{
static int compute_average( int a,
int b)
{
return (a / 2) + (b / 2) +
((a % 2 + b % 2) / 2);
}
public static void Main ()
{
int a = int .MaxValue;
int b = int .MaxValue;
Console.Write( "Actual average : " +
int .MaxValue+ "\n" );
Console.Write( "Computed average : " );
Console.Write(compute_average(a, b));
}
}
|
PHP
<?php
function compute_average( $a , $b )
{
return ( $a / 2) + ( $b / 2) +
(( $a % 2 + $b % 2) / 2);
}
$a = 2147483648;
$b = 2147483648;
$x = 2147483648;
print ( "Actual average : " . $x );
print ( "\nComputed average : " );
print (compute_average( $a , $b ));
?>
|
Javascript
<script>
const INT_MAX = 2147483647;
function compute_average( a, b)
{
return parseInt(a / 2) + parseInt(b / 2) + ((a % 2 + b % 2) / 2);
}
let a = INT_MAX, b = INT_MAX;
document.write( "Actual average : " + INT_MAX + "<br/>" );
document.write( "Computed average : " + compute_average(a, b));
</script>
|
Output:
Actual average : 2147483647
Computed average : 2147483647
Time Complexity: O(1), the code will run in a constant time.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
This article is contributed by Rohit Thapliyal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Please Login to comment...