# Find the smallest contiguous sum pair in an array

Here given code implementation process.

``````// C Program
// Find the smallest contiguous sum pair in an array
#include <stdio.h>

//Function which is display array elements
void display(int arr[], int size)
{
for (int i = 0; i < size; ++i)
{
printf("%d ", arr[i]);
}
printf("\n");
}
// Find the smallest contiguous pair which is exist in array
void smallestContiguous(int arr[], int size)
{
if (size <= 1)
{
// When have less than 2 elements
return;
}
printf("\n  Given Array \n  ");
display(arr, size);
// Get first contiguous pair
int a = 0;
int b = 1;
// Execute loop through by array size
for (int i = 2; i < size; ++i)
{
if ((arr[a] + arr[b]) > arr[i - 1] + arr[i])
{
// Update resultant position
a = i - 1;
b = i;
}
}
// Display calculated result
printf("  Smallest contiguous pair is (%d,%d) \n", arr[a], arr[b]);
}
int main(int argc, char const *argv[])
{
// Define array of integer elements
int arr[] = {
1 , 4 , 5 , 5 , -5 , 4 , 6 , -5 , 7
};
// Get the size
int size = sizeof(arr) / sizeof(arr[0]);
// Find smallest contiguous pair
smallestContiguous(arr, size);
return 0;
}``````

#### Output

``````  Given Array
1 4 5 5 -5 4 6 -5 7
Smallest contiguous pair is (-5,4)``````
``````/*
Java Program for
Find the smallest contiguous sum pair in an array
*/
class Pairs
{
//Function which is display array elements
public void display(int[] arr, int size)
{
for (int i = 0; i < size; ++i)
{
System.out.print("  " + arr[i]);
}
System.out.print("\n");
}
// Find the smallest contiguous pair which is exist in array
public void smallestContiguous(int[] arr, int size)
{
if (size <= 1)
{
// When have less than 2 elements
return;
}
System.out.print("\n  Given Array \n ");
display(arr, size);
// Get first contiguous pair
int a = 0;
int b = 1;
// Execute loop through by array size
for (int i = 2; i < size; ++i)
{
if ((arr[a] + arr[b]) > arr[i - 1] + arr[i])
{
// Update resultant position
a = i - 1;
b = i;
}
}
// Display calculated result
System.out.print("  Smallest contiguous pair is (" + arr[a] + "," + arr[b] + ") \n");
}
public static void main(String[] args)
{
// Define array of integer elements
int[] arr = {
1 , 4 , 5 , 5 , -5 , 4 , 6 , -5 , 7
};
// Get the size
int size = arr.length;
// Find smallest contiguous pair
}
}``````

#### Output

``````  Given Array
1  4  5  5  -5  4  6  -5  7
Smallest contiguous pair is (-5,4)``````
``````// Include header file
#include <iostream>
using namespace std;

/*
C++ Program for
Find the smallest contiguous sum pair in an array
*/

class Pairs
{
public:
//Function which is display array elements
void display(int arr[], int size)
{
for (int i = 0; i < size; ++i)
{
cout << "  " << arr[i];
}
cout << "\n";
}
// Find the smallest contiguous pair which is exist in array
void smallestContiguous(int arr[], int size)
{
// When have less than 2 elements
if (size <= 1)
{
return;
}
cout << "\n  Given Array \n ";
this->display(arr, size);
// Get first contiguous pair
int a = 0;
int b = 1;
// Execute loop through by array size
for (int i = 2; i < size; ++i)
{
if ((arr[a] + arr[b]) > arr[i - 1] + arr[i])
{
// Update resultant position
a = i - 1;
b = i;
}
}
// Display calculated result
cout << "  Smallest contiguous pair is (" << arr[a] << "," << arr[b] << ") \n";
}
};
int main()
{
// Define array of integer elements
int arr[] = {
1 , 4 , 5 , 5 , -5 , 4 , 6 , -5 , 7
};
// Get the size
int size = sizeof(arr) / sizeof(arr[0]);
// Find smallest contiguous pair
return 0;
}``````

#### Output

``````  Given Array
1  4  5  5  -5  4  6  -5  7
Smallest contiguous pair is (-5,4)``````
``````// Include namespace system
using System;
/*
C# Program for
Find the smallest contiguous sum pair in an array
*/
public class Pairs
{
//Function which is display array elements
public void display(int[] arr, int size)
{
for (int i = 0; i < size; ++i)
{
Console.Write("  " + arr[i]);
}
Console.Write("\n");
}
// Find the smallest contiguous pair which is exist in array
public void smallestContiguous(int[] arr, int size)
{
// When have less than 2 elements
if (size <= 1)
{
return;
}
Console.Write("\n  Given Array \n ");
display(arr, size);
// Get first contiguous pair
int a = 0;
int b = 1;
// Execute loop through by array size
for (int i = 2; i < size; ++i)
{
if ((arr[a] + arr[b]) > arr[i - 1] + arr[i])
{
// Update resultant position
a = i - 1;
b = i;
}
}
// Display calculated result
Console.Write("  Smallest contiguous pair is (" + arr[a] + "," + arr[b] + ") \n");
}
public static void Main(String[] args)
{
// Define array of integer elements
int[] arr = {
1 , 4 , 5 , 5 , -5 , 4 , 6 , -5 , 7
};
// Get the size
int size = arr.Length;
// Find smallest contiguous pair
}
}``````

#### Output

``````  Given Array
1  4  5  5  -5  4  6  -5  7
Smallest contiguous pair is (-5,4)``````
``````<?php
/*
Php Program for
Find the smallest contiguous sum pair in an array
*/
class Pairs
{
//Function which is display array elements
public	function display(\$arr, \$size)
{
for (\$i = 0; \$i < \$size; ++\$i)
{
echo "  ". \$arr[\$i];
}
echo "\n";
}
// Find the smallest contiguous pair which is exist in array
public	function smallestContiguous(\$arr, \$size)
{
// When have less than 2 elements
if (\$size <= 1)
{
return;
}
echo "\n  Given Array \n ";
\$this->display(\$arr, \$size);
// Get first contiguous pair
\$a = 0;
\$b = 1;
// Execute loop through by array size
for (\$i = 2; \$i < \$size; ++\$i)
{
if ((\$arr[\$a] + \$arr[\$b]) > \$arr[\$i - 1] + \$arr[\$i])
{
// Update resultant position
\$a = \$i - 1;
\$b = \$i;
}
}
// Display calculated result
echo "  Smallest contiguous pair is (". \$arr[\$a] .",". \$arr[\$b] .") \n";
}
}

function main()
{
// Define array of integer elements
\$arr = array(1, 4, 5, 5, -5, 4, 6, -5, 7);
// Get the size
\$size = count(\$arr);
// Find smallest contiguous pair
}
main();``````

#### Output

``````  Given Array
1  4  5  5  -5  4  6  -5  7
Smallest contiguous pair is (-5,4)``````
``````/*
Node Js Program for
Find the smallest contiguous sum pair in an array
*/
class Pairs
{
//Function which is display array elements
display(arr, size)
{
for (var i = 0; i < size; ++i)
{
process.stdout.write("  " + arr[i]);
}
process.stdout.write("\n");
}
// Find the smallest contiguous pair which is exist in array
smallestContiguous(arr, size)
{
// When have less than 2 elements
if (size <= 1)
{
return;
}
process.stdout.write("\n  Given Array \n ");
this.display(arr, size);
// Get first contiguous pair
var a = 0;
var b = 1;
// Execute loop through by array size
for (var i = 2; i < size; ++i)
{
if ((arr[a] + arr[b]) > arr[i - 1] + arr[i])
{
// Update resultant position
a = i - 1;
b = i;
}
}
// Display calculated result
process.stdout.write("  Smallest contiguous pair is (" + arr[a] + "," + arr[b] + ") \n");
}
}

function main()
{
// Define array of integer elements
var arr = [1, 4, 5, 5, -5, 4, 6, -5, 7];
// Get the size
var size = arr.length;
// Find smallest contiguous pair
}
main();``````

#### Output

``````  Given Array
1  4  5  5  -5  4  6  -5  7
Smallest contiguous pair is (-5,4)``````
``````#   Python 3 Program for
#   Find the smallest contiguous sum pair in an array

class Pairs :
# Function which is display array elements
def display(self, arr, size) :
i = 0
while (i < size) :
print("  ", arr[i], end = "")
i += 1

print(end = "\n")

#  Find the smallest contiguous pair which is exist in array
def smallestContiguous(self, arr, size) :
#  When have less than 2 elements
if (size <= 1) :
return

print("\n  Given Array \n ", end = "")
self.display(arr, size)
#  Get first contiguous pair
a = 0
b = 1
i = 2
#  Execute loop through by array size
while (i < size) :
if ((arr[a] + arr[b]) > arr[i - 1] + arr[i]) :
#  Update resultant position
a = i - 1
b = i

i += 1

#  Display calculated result
print("  Smallest contiguous pair is (", arr[a] ,",", arr[b] ,") ")

def main() :
#  Define array of integer elements
arr = [1, 4, 5, 5, -5, 4, 6, -5, 7]
#  Get the size
size = len(arr)
#  Find smallest contiguous pair

if __name__ == "__main__": main()``````

#### Output

``````  Given Array
1   4   5   5   -5   4   6   -5   7
Smallest contiguous pair is ( -5 , 4 )``````
``````#   Ruby Program for
#   Find the smallest contiguous sum pair in an array

class Pairs
# Function which is display array elements
def display(arr, size)
i = 0
while (i < size)
print("  ", arr[i])
i += 1
end

print("\n")
end

#  Find the smallest contiguous pair which is exist in array
def smallestContiguous(arr, size)
#  When have less than 2 elements
if (size <= 1)
return
end

print("\n  Given Array \n ")
self.display(arr, size)
#  Get first contiguous pair
a = 0
b = 1
i = 2
#  Execute loop through by array size
while (i < size)
if ((arr[a] + arr[b]) > arr[i - 1] + arr[i])
#  Update resultant position
a = i - 1
b = i
end

i += 1
end

#  Display calculated result
print("  Smallest contiguous pair is (", arr[a] ,",", arr[b] ,") \n")
end

end

def main()
#  Define array of integer elements
arr = [1, 4, 5, 5, -5, 4, 6, -5, 7]
#  Get the size
size = arr.length
#  Find smallest contiguous pair
end

main()``````

#### Output

``````  Given Array
1  4  5  5  -5  4  6  -5  7
Smallest contiguous pair is (-5,4)
``````
``````/*
Scala Program for
Find the smallest contiguous sum pair in an array
*/
class Pairs
{
//Function which is display array elements
def display(arr: Array[Int], size: Int): Unit = {
var i: Int = 0;
while (i < size)
{
print("  " + arr(i));
i += 1;
}
print("\n");
}
// Find the smallest contiguous pair which is exist in array
def smallestContiguous(arr: Array[Int], size: Int): Unit = {
// When have less than 2 elements
if (size <= 1)
{
return;
}
print("\n  Given Array \n ");
this.display(arr, size);
// Get first contiguous pair
var a: Int = 0;
var b: Int = 1;
var i: Int = 2;
// Execute loop through by array size
while (i < size)
{
if ((arr(a) + arr(b)) > arr(i - 1) + arr(i))
{
// Update resultant position
a = i - 1;
b = i;
}
i += 1;
}
// Display calculated result
print("  Smallest contiguous pair is (" + arr(a) + "," + arr(b) + ") \n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Pairs = new Pairs();
// Define array of integer elements
var arr: Array[Int] = Array(1, 4, 5, 5, -5, 4, 6, -5, 7);
// Get the size
var size: Int = arr.length;
// Find smallest contiguous pair
}
}``````

#### Output

``````  Given Array
1  4  5  5  -5  4  6  -5  7
Smallest contiguous pair is (-5,4)``````
``````/*
Swift 4 Program for
Find the smallest contiguous sum pair in an array
*/
class Pairs
{
//Function which is display array elements
func display(_ arr: [Int], _ size: Int)
{
var i: Int = 0;
while (i < size)
{
print("  ", arr[i], terminator: "");
i += 1;
}
print(terminator: "\n");
}
// Find the smallest contiguous pair which is exist in array
func smallestContiguous(_ arr: [Int], _ size: Int)
{
// When have less than 2 elements
if (size <= 1)
{
return;
}
print("\n  Given Array \n ", terminator: "");
self.display(arr, size);
// Get first contiguous pair
var a: Int = 0;
var b: Int = 1;
var i: Int = 2;
// Execute loop through by array size
while (i < size)
{
if ((arr[a] + arr[b]) > arr[i - 1] + arr[i])
{
// Update resultant position
a = i - 1;
b = i;
}
i += 1;
}
// Display calculated result
print("  Smallest contiguous pair is (", arr[a] ,",", arr[b] ,") ");
}
}
func main()
{
// Define array of integer elements
let arr: [Int] = [1, 4, 5, 5, -5, 4, 6, -5, 7];
// Get the size
let size: Int = arr.count;
// Find smallest contiguous pair
}
main();``````

#### Output

``````  Given Array
1   4   5   5   -5   4   6   -5   7
Smallest contiguous pair is ( -5 , 4 )``````
``````/*
Kotlin Program for
Find the smallest contiguous sum pair in an array
*/
class Pairs
{
//Function which is display array elements
fun display(arr: Array <Int> , size: Int): Unit
{
var i: Int = 0;
while (i < size)
{
print("  " + arr[i]);
i += 1;
}
print("\n");
}
// Find the smallest contiguous pair which is exist in array
fun smallestContiguous(arr: Array <Int> , size: Int): Unit
{
// When have less than 2 elements
if (size <= 1)
{
return;
}
print("\n  Given Array \n ");
this.display(arr, size);
// Get first contiguous pair
var a: Int = 0;
var b: Int = 1;
var i: Int = 2;
// Execute loop through by array size
while (i < size)
{
if ((arr[a] + arr[b]) > arr[i - 1] + arr[i])
{
// Update resultant position
a = i - 1;
b = i;
}
i += 1;
}
// Display calculated result
print("  Smallest contiguous pair is (" + arr[a] + "," + arr[b] + ") \n");
}
}
fun main(args: Array <String> ): Unit
{
// Define array of integer elements
var arr: Array < Int > = arrayOf(1, 4, 5, 5, -5, 4, 6, -5, 7);
// Get the size
var size: Int = arr.count();
// Find smallest contiguous pair
}``````

#### Output

``````  Given Array
1  4  5  5  -5  4  6  -5  7
Smallest contiguous pair is (-5,4)``````

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.