Posted on by Kalkicode
Code Array

# Smallest subarray with sum greater than a given value

Here given code implementation process.

``````/*
C program for
Smallest subarray with sum greater than a given value
*/
#include <stdio.h>

// Display array elements
void printArray(int arr[], int n)
{
for (int i = 0; i < n; ++i)
{
printf("  %d", arr[i]);
}
}
void smallestLengthSubarray(int arr[], int n, int k)
{
// Define some auxiliary variable
int sum = 0;
int length = n;
int front = 0;
int i = 0;
while (front < n)
{
// Execute loop under given k sum
while (i < n && sum <= k)
{
sum = sum + arr[i];
i++;
}
if (sum > k && length > (i - front))
{
// Change window size
length = (i - front);
}
// Change position
front++;
// Rest sum and i
sum = 0;
i = front;
}
// Display given array
printf("\n Given array : ");
printArray(arr, n);
printf("\n Given sum : %d", k);
if (length == n + n)
{
// When no subarray is greater than k
printf("\n None \n");
}
else
{
// Display calculated result
printf("\n Subarray size : %d \n", length);
}
}
int main(int argc, char
const *argv[])
{
int arr1[] = {
-11 , 1 , 3 , 7 , 1 , 6 , 7 , 2 , 1 , 7
};
int arr2[] = {
2 , 1 , 1 , 5 , 1 , 4 , 5 , 3 , 5
};
int arr3[] = {
1 , 2 , 1 , -2 , 1 , 1 , 2 , -2 , 4 , 0
};
// Test A
int n = sizeof(arr1) / sizeof(arr1[0]);
int k = 14;
/*
arr = [  -11, 1, 3, 8, 1, 2, 6, 7, 2, 1, 7 ]
sum = 14
-------------------------------------------
2, 6, 7
-----------------------
Result : 3
*/
smallestLengthSubarray(arr1, n, k);
// Test B
k = 8;
n = sizeof(arr2) / sizeof(arr2[0]);
/*
arr = [2, 1, 1, 5, 1, 4, 5, 3, 5]

k = 8
---------------------------------
4, 5
-----------------------
Result : 2
*/
smallestLengthSubarray(arr2, n, k);
// Test C
k = 4;
n = sizeof(arr3) / sizeof(arr3[0]);
/*
arr = [1, 2, 1, -2,  1, 1, 2, -2, 4, 0]

k = 4
--------------------
[1, 2, -2, 4]
--------------------
Result : 4
*/
smallestLengthSubarray(arr3, n, k);
return 0;
}``````

#### Output

`````` Given array :   -11  1  3  7  1  6  7  2  1  7
Given sum : 14
Subarray size : 3

Given array :   2  1  1  5  1  4  5  3  5
Given sum : 8
Subarray size : 2

Given array :   1  2  1  -2  1  1  2  -2  4  0
Given sum : 4
Subarray size : 4``````
``````// Java program for
// Smallest subarray with sum greater than a given value
public class Pairs
{
// Display array elements
public void printArray(int[] arr, int n)
{
for (int i = 0; i < n; ++i)
{
System.out.print(" " + arr[i]);
}
}
public void smallestLengthSubarray(int[] arr, int n, int k)
{
// Define some auxiliary variable
int sum = 0;
int length = n;
int front = 0;
int i = 0;
while (front < n)
{
// Execute loop under given k sum
while (i < n && sum <= k)
{
sum = sum + arr[i];
i++;
}
if (sum > k && length > (i - front))
{
// Change window size
length = (i - front);
}
// Change position
front++;
// Rest sum and i
sum = 0;
i = front;
}
// Display given array
System.out.print("\n Given array : ");
printArray(arr, n);
System.out.print("\n Given sum : " + k);
if (length == n + n)
{
// When no subarray is greater than k
System.out.print("\n None \n");
}
else
{
// Display calculated result
System.out.print("\n Subarray size : " + length + " \n");
}
}
public static void main(String[] args)
{
int[] arr1 = {
-11 , 1 , 3 , 7 , 1 , 6 , 7 , 2 , 1 , 7
};
int[] arr2 = {
2 , 1 , 1 , 5 , 1 , 4 , 5 , 3 , 5
};
int[] arr3 = {
1 , 2 , 1 , -2 , 1 , 1 , 2 , -2 , 4 , 0
};
// Test A
int n = arr1.length;
int k = 14;
/*
arr = [  -11, 1, 3, 8, 1, 2, 6, 7, 2, 1, 7 ]
sum = 14
-------------------------------------------
2, 6, 7
-----------------------
Result : 3
*/
// Test B
k = 8;
n = arr2.length;
/*
arr = [2, 1, 1, 5, 1, 4, 5, 3, 5]

k = 8
---------------------------------
4, 5
-----------------------
Result : 2
*/
// Test C
k = 4;
n = arr3.length;
/*
arr = [1, 2, 1, -2,  1, 1, 2, -2, 4, 0]

k = 4
--------------------
[1, 2, -2, 4]
--------------------
Result : 4
*/
}
}``````

#### Output

`````` Given array :  -11 1 3 7 1 6 7 2 1 7
Given sum : 14
Subarray size : 3

Given array :  2 1 1 5 1 4 5 3 5
Given sum : 8
Subarray size : 2

Given array :  1 2 1 -2 1 1 2 -2 4 0
Given sum : 4
Subarray size : 4``````
``````// Include header file
#include <iostream>

using namespace std;
// C++ program for
// Smallest subarray with sum greater than a given value
class Pairs
{
public:
// Display array elements
void printArray(int arr[], int n)
{
for (int i = 0; i < n; ++i)
{
cout << " " << arr[i];
}
}
void smallestLengthSubarray(int arr[], int n, int k)
{
// Define some auxiliary variable
int sum = 0;
int length = n;
int front = 0;
int i = 0;
while (front < n)
{
// Execute loop under given k sum
while (i < n && sum <= k)
{
sum = sum + arr[i];
i++;
}
if (sum > k && length > (i - front))
{
// Change window size
length = (i - front);
}
// Change position
front++;
// Rest sum and i
sum = 0;
i = front;
}
// Display given array
cout << "\n Given array : ";
this->printArray(arr, n);
cout << "\n Given sum : " << k;
if (length == n + n)
{
// When no subarray is greater than k
cout << "\n None \n";
}
else
{
// Display calculated result
cout << "\n Subarray size : " << length << " \n";
}
}
};
int main()
{
int arr1[] = {
-11 , 1 , 3 , 7 , 1 , 6 , 7 , 2 , 1 , 7
};
int arr2[] = {
2 , 1 , 1 , 5 , 1 , 4 , 5 , 3 , 5
};
int arr3[] = {
1 , 2 , 1 , -2 , 1 , 1 , 2 , -2 , 4 , 0
};
// Test A
int n = sizeof(arr1) / sizeof(arr1[0]);
int k = 14;
/*
arr = [  -11, 1, 3, 8, 1, 2, 6, 7, 2, 1, 7 ]
sum = 14
-------------------------------------------
2, 6, 7
-----------------------
Result : 3
*/
// Test B
k = 8;
n = sizeof(arr2) / sizeof(arr2[0]);
/*
arr = [2, 1, 1, 5, 1, 4, 5, 3, 5]
k = 8
---------------------------------
4, 5
-----------------------
Result : 2
*/
// Test C
k = 4;
n = sizeof(arr3) / sizeof(arr3[0]);
/*
arr = [1, 2, 1, -2,  1, 1, 2, -2, 4, 0]
k = 4
--------------------
[1, 2, -2, 4]
--------------------
Result : 4
*/
return 0;
}``````

#### Output

`````` Given array :  -11 1 3 7 1 6 7 2 1 7
Given sum : 14
Subarray size : 3

Given array :  2 1 1 5 1 4 5 3 5
Given sum : 8
Subarray size : 2

Given array :  1 2 1 -2 1 1 2 -2 4 0
Given sum : 4
Subarray size : 4``````
``````// Include namespace system
using System;
// Csharp program for
// Smallest subarray with sum greater than a given value
public class Pairs
{
// Display array elements
public void printArray(int[] arr, int n)
{
for (int i = 0; i < n; ++i)
{
Console.Write(" " + arr[i]);
}
}
public void smallestLengthSubarray(int[] arr, int n, int k)
{
// Define some auxiliary variable
int sum = 0;
int length = n;
int front = 0;
int i = 0;
while (front < n)
{
// Execute loop under given k sum
while (i < n && sum <= k)
{
sum = sum + arr[i];
i++;
}
if (sum > k && length > (i - front))
{
// Change window size
length = (i - front);
}
// Change position
front++;
// Rest sum and i
sum = 0;
i = front;
}
// Display given array
Console.Write("\n Given array : ");
this.printArray(arr, n);
Console.Write("\n Given sum : " + k);
if (length == n + n)
{
// When no subarray is greater than k
Console.Write("\n None \n");
}
else
{
// Display calculated result
Console.Write("\n Subarray size : " + length + " \n");
}
}
public static void Main(String[] args)
{
int[] arr1 = {
-11 , 1 , 3 , 7 , 1 , 6 , 7 , 2 , 1 , 7
};
int[] arr2 = {
2 , 1 , 1 , 5 , 1 , 4 , 5 , 3 , 5
};
int[] arr3 = {
1 , 2 , 1 , -2 , 1 , 1 , 2 , -2 , 4 , 0
};
// Test A
int n = arr1.Length;
int k = 14;
/*
arr = [  -11, 1, 3, 8, 1, 2, 6, 7, 2, 1, 7 ]
sum = 14
-------------------------------------------
2, 6, 7
-----------------------
Result : 3
*/
// Test B
k = 8;
n = arr2.Length;
/*
arr = [2, 1, 1, 5, 1, 4, 5, 3, 5]
k = 8
---------------------------------
4, 5
-----------------------
Result : 2
*/
// Test C
k = 4;
n = arr3.Length;
/*
arr = [1, 2, 1, -2,  1, 1, 2, -2, 4, 0]
k = 4
--------------------
[1, 2, -2, 4]
--------------------
Result : 4
*/
}
}``````

#### Output

`````` Given array :  -11 1 3 7 1 6 7 2 1 7
Given sum : 14
Subarray size : 3

Given array :  2 1 1 5 1 4 5 3 5
Given sum : 8
Subarray size : 2

Given array :  1 2 1 -2 1 1 2 -2 4 0
Given sum : 4
Subarray size : 4``````
``````package main
import "fmt"
// Go program for
// Smallest subarray with sum greater than a given value

// Display array elements
func printArray(arr[] int, n int) {
for i := 0 ; i < n ; i++ {
fmt.Print(" ", arr[i])
}
}
func smallestLengthSubarray(arr[] int, n int, k int) {
// Define some auxiliary variable
var sum int = 0
var length int = n
var front int = 0
var i int = 0
for (front < n) {
// Execute loop under given k sum
for (i < n && sum <= k) {
sum = sum + arr[i]
i++
}
if sum > k && length > (i - front) {
// Change window size
length = (i - front)
}
// Change position
front++
// Rest sum and i
sum = 0
i = front
}
// Display given array
fmt.Print("\n Given array : ")
printArray(arr, n)
fmt.Print("\n Given sum : ", k)
if length == n + n {
// When no subarray is greater than k
fmt.Print("\n None \n")
} else {
// Display calculated result
fmt.Print("\n Subarray size : ", length, " \n")
}
}
func main() {

var arr1 = [] int { -11, 1, 3, 7, 1, 6, 7, 2, 1, 7 }
var arr2 = [] int { 2, 1, 1, 5, 1, 4, 5, 3, 5}
var arr3 = [] int { 1, 2, 1, -2,  1, 1, 2, -2, 4, 0}
// Test A
var n int = len(arr1)
var k int = 14
/*
arr = [  -11, 1, 3, 8, 1, 2, 6, 7, 2, 1, 7 ]
sum = 14
-------------------------------------------
2, 6, 7
-----------------------
Result : 3
*/
smallestLengthSubarray(arr1, n, k)
// Test B
k = 8
n = len(arr2)
/*
arr = [ 2, 1, 1, 5, 1, 4, 5, 3, 5]
k = 8
---------------------------------
4, 5
-----------------------
Result : 2
*/
smallestLengthSubarray(arr2, n, k)
// Test C
k = 4
n = len(arr3)
/*
arr = [1, 2, 1, -2,  1, 1, 2, -2, 4, 0]
k = 4
--------------------
[1, 2, -2, 4]
--------------------
Result : 4
*/
smallestLengthSubarray(arr3, n, k)
}``````

#### Output

`````` Given array :  -11 1 3 7 1 6 7 2 1 7
Given sum : 14
Subarray size : 3

Given array :  2 1 1 5 1 4 5 3 5
Given sum : 8
Subarray size : 2

Given array :  1 2 1 -2 1 1 2 -2 4 0
Given sum : 4
Subarray size : 4``````
``````<?php
// Php program for
// Smallest subarray with sum greater than a given value
class Pairs
{
// Display array elements
public  function printArray(\$arr, \$n)
{
for (\$i = 0; \$i < \$n; ++\$i)
{
echo(" ".\$arr[\$i]);
}
}
public  function smallestLengthSubarray(\$arr, \$n, \$k)
{
// Define some auxiliary variable
\$sum = 0;
\$length = \$n;
\$front = 0;
\$i = 0;
while (\$front < \$n)
{
// Execute loop under given k sum
while (\$i < \$n && \$sum <= \$k)
{
\$sum = \$sum + \$arr[\$i];
\$i++;
}
if (\$sum > \$k && \$length > (\$i - \$front))
{
// Change window size
\$length = (\$i - \$front);
}
// Change position
\$front++;
// Rest sum and i
\$sum = 0;
\$i = \$front;
}
// Display given array
echo("\n Given array : ");
\$this->printArray(\$arr, \$n);
echo("\n Given sum : ".\$k);
if (\$length == \$n + \$n)
{
// When no subarray is greater than k
echo("\n None \n");
}
else
{
// Display calculated result
echo("\n Subarray size : ".\$length.
" \n");
}
}
}

function main()
{
\$arr1 = array(-11, 1, 3, 7, 1, 6, 7, 2, 1, 7);
\$arr2 = array(2, 1, 1, 5, 1, 4, 5, 3, 5);
\$arr3 = array(1, 2, 1, -2, 1, 1, 2, -2, 4, 0);
// Test A
\$n = count(\$arr1);
\$k = 14;
/*
arr = [  -11, 1, 3, 8, 1, 2, 6, 7, 2, 1, 7 ]
sum = 14
-------------------------------------------
2, 6, 7
-----------------------
Result : 3
*/
// Test B
\$k = 8;
\$n = count(\$arr2);
/*
arr = [2, 1, 1, 5, 1, 4, 5, 3, 5]
k = 8
---------------------------------
4, 5
-----------------------
Result : 2
*/
// Test C
\$k = 4;
\$n = count(\$arr3);
/*
arr = [1, 2, 1, -2,  1, 1, 2, -2, 4, 0]
k = 4
--------------------
[1, 2, -2, 4]
--------------------
Result : 4
*/
}
main();``````

#### Output

`````` Given array :  -11 1 3 7 1 6 7 2 1 7
Given sum : 14
Subarray size : 3

Given array :  2 1 1 5 1 4 5 3 5
Given sum : 8
Subarray size : 2

Given array :  1 2 1 -2 1 1 2 -2 4 0
Given sum : 4
Subarray size : 4``````
``````// Node JS program for
// Smallest subarray with sum greater than a given value
class Pairs
{
// Display array elements
printArray(arr, n)
{
for (var i = 0; i < n; ++i)
{
process.stdout.write(" " + arr[i]);
}
}
smallestLengthSubarray(arr, n, k)
{
// Define some auxiliary variable
var sum = 0;
var length = n;
var front = 0;
var i = 0;
while (front < n)
{
// Execute loop under given k sum
while (i < n && sum <= k)
{
sum = sum + arr[i];
i++;
}
if (sum > k && length > (i - front))
{
// Change window size
length = (i - front);
}
// Change position
front++;
// Rest sum and i
sum = 0;
i = front;
}
// Display given array
process.stdout.write("\n Given array : ");
this.printArray(arr, n);
process.stdout.write("\n Given sum : " + k);
if (length == n + n)
{
// When no subarray is greater than k
process.stdout.write("\n None \n");
}
else
{
// Display calculated result
process.stdout.write("\n Subarray size : " + length + " \n");
}
}
}

function main()
{
var arr1 = [-11, 1, 3, 7, 1, 6, 7, 2, 1, 7];
var arr2 = [2, 1, 1, 5, 1, 4, 5, 3, 5];
var arr3 = [1, 2, 1, -2, 1, 1, 2, -2, 4, 0];
// Test A
var n = arr1.length;
var k = 14;
/*
arr = [  -11, 1, 3, 8, 1, 2, 6, 7, 2, 1, 7 ]
sum = 14
-------------------------------------------
2, 6, 7
-----------------------
Result : 3
*/
// Test B
k = 8;
n = arr2.length;
/*
arr = [2, 1, 1, 5, 1, 4, 5, 3, 5]
k = 8
---------------------------------
4, 5
-----------------------
Result : 2
*/
// Test C
k = 4;
n = arr3.length;
/*
arr = [1, 2, 1, -2,  1, 1, 2, -2, 4, 0]
k = 4
--------------------
[1, 2, -2, 4]
--------------------
Result : 4
*/
}
main();``````

#### Output

`````` Given array :  -11 1 3 7 1 6 7 2 1 7
Given sum : 14
Subarray size : 3

Given array :  2 1 1 5 1 4 5 3 5
Given sum : 8
Subarray size : 2

Given array :  1 2 1 -2 1 1 2 -2 4 0
Given sum : 4
Subarray size : 4``````
``````#  Python 3 program for
#  Smallest subarray with sum greater than a given value
class Pairs :
#  Display list elements
def printArray(self, arr, n) :
i = 0
while (i < n) :
print(" ", arr[i], end = "")
i += 1

def smallestLengthSubarray(self, arr, n, k) :
#  Define some auxiliary variable
sum = 0
length = n
front = 0
i = 0
while (front < n) :
#  Execute loop under given k sum
while (i < n and sum <= k) :
sum = sum + arr[i]
i += 1

if (sum > k and length > (i - front)) :
#  Change window size
length = (i - front)

#  Change position
front += 1
#  Rest sum and i
sum = 0
i = front

#  Display given list
print("\n Given array : ", end = "")
self.printArray(arr, n)
print("\n Given sum : ", k, end = "")
if (length == n + n) :
#  When no sublist is greater than k
print("\n None ")
else :
#  Display calculated result
print("\n Subarray size : ", length ," ")

def main() :
arr1 = [-11, 1, 3, 7, 1, 6, 7, 2, 1, 7]
arr2 = [2, 1, 1, 5, 1, 4, 5, 3, 5]
arr3 = [1, 2, 1, -2, 1, 1, 2, -2, 4, 0]
#  Test A
n = len(arr1)
k = 14
#    arr = [  -11, 1, 3, 8, 1, 2, 6, 7, 2, 1, 7 ]
#    sum = 14
#    -------------------------------------------
#    2, 6, 7
#    -----------------------
#    Result : 3
#  Test B
k = 8
n = len(arr2)
#    arr = [2, 1, 1, 5, 1, 4, 5, 3, 5]
#    k = 8
#    ---------------------------------
#    4, 5
#    -----------------------
#    Result : 2
#  Test C
k = 4
n = len(arr3)
#    arr = [1, 2, 1, -2,  1, 1, 2, -2, 4, 0]
#    k = 4
#    --------------------
#    [1, 2, -2, 4]
#    --------------------
#    Result : 4

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

#### Output

`````` Given array :   -11  1  3  7  1  6  7  2  1  7
Given sum :  14
Subarray size :  3

Given array :   2  1  1  5  1  4  5  3  5
Given sum :  8
Subarray size :  2

Given array :   1  2  1  -2  1  1  2  -2  4  0
Given sum :  4
Subarray size :  4``````
``````#  Ruby program for
#  Smallest subarray with sum greater than a given value
class Pairs
#  Display array elements
def printArray(arr, n)
i = 0
while (i < n)
print(" ", arr[i])
i += 1
end

end

def smallestLengthSubarray(arr, n, k)
#  Define some auxiliary variable
sum = 0
length = n
front = 0
i = 0
while (front < n)
#  Execute loop under given k sum
while (i < n && sum <= k)
sum = sum + arr[i]
i += 1
end

if (sum > k && length > (i - front))
#  Change window size
length = (i - front)
end

#  Change position
front += 1
#  Rest sum and i
sum = 0
i = front
end

#  Display given array
print("\n Given array : ")
self.printArray(arr, n)
print("\n Given sum : ", k)
if (length == n + n)
#  When no subarray is greater than k
print("\n None \n")
else

#  Display calculated result
print("\n Subarray size : ", length ," \n")
end

end

end

def main()
arr1 = [-11, 1, 3, 7, 1, 6, 7, 2, 1, 7]
arr2 = [2, 1, 1, 5, 1, 4, 5, 3, 5]
arr3 = [1, 2, 1, -2, 1, 1, 2, -2, 4, 0]
#  Test A
n = arr1.length
k = 14
#    arr = [  -11, 1, 3, 8, 1, 2, 6, 7, 2, 1, 7 ]
#    sum = 14
#    -------------------------------------------
#    2, 6, 7
#    -----------------------
#    Result : 3
#  Test B
k = 8
n = arr2.length
#    arr = [2, 1, 1, 5, 1, 4, 5, 3, 5]
#    k = 8
#    ---------------------------------
#    4, 5
#    -----------------------
#    Result : 2
#  Test C
k = 4
n = arr3.length
#    arr = [1, 2, 1, -2,  1, 1, 2, -2, 4, 0]
#    k = 4
#    --------------------
#    [1, 2, -2, 4]
#    --------------------
#    Result : 4
end

main()``````

#### Output

`````` Given array :  -11 1 3 7 1 6 7 2 1 7
Given sum : 14
Subarray size : 3

Given array :  2 1 1 5 1 4 5 3 5
Given sum : 8
Subarray size : 2

Given array :  1 2 1 -2 1 1 2 -2 4 0
Given sum : 4
Subarray size : 4
``````
``````// Scala program for
// Smallest subarray with sum greater than a given value
class Pairs()
{
// Display array elements
def printArray(arr: Array[Int], n: Int): Unit = {
var i: Int = 0;
while (i < n)
{
print(" " + arr(i));
i += 1;
}
}
def smallestLengthSubarray(arr: Array[Int], n: Int, k: Int): Unit = {
// Define some auxiliary variable
var sum: Int = 0;
var length: Int = n;
var front: Int = 0;
var i: Int = 0;
while (front < n)
{
// Execute loop under given k sum
while (i < n && sum <= k)
{
sum = sum + arr(i);
i += 1;
}
if (sum > k && length > (i - front))
{
// Change window size
length = (i - front);
}
// Change position
front += 1;
// Rest sum and i
sum = 0;
i = front;
}
// Display given array
print("\n Given array : ");
printArray(arr, n);
print("\n Given sum : " + k);
if (length == n + n)
{
// When no subarray is greater than k
print("\n None \n");
}
else
{
// Display calculated result
print("\n Subarray size : " + length + " \n");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Pairs = new Pairs();
var arr1: Array[Int] = Array(-11, 1, 3, 7, 1, 6, 7, 2, 1, 7);
var arr2: Array[Int] = Array(2, 1, 1, 5, 1, 4, 5, 3, 5);
var arr3: Array[Int] = Array(1, 2, 1, -2, 1, 1, 2, -2, 4, 0);
// Test A
var n: Int = arr1.length;
var k: Int = 14;
/*
arr = [  -11, 1, 3, 8, 1, 2, 6, 7, 2, 1, 7 ]
sum = 14
-------------------------------------------
2, 6, 7
-----------------------
Result : 3
*/
// Test B
k = 8;
n = arr2.length;
/*
arr = [2, 1, 1, 5, 1, 4, 5, 3, 5]
k = 8
---------------------------------
4, 5
-----------------------
Result : 2
*/
// Test C
k = 4;
n = arr3.length;
/*
arr = [1, 2, 1, -2,  1, 1, 2, -2, 4, 0]
k = 4
--------------------
[1, 2, -2, 4]
--------------------
Result : 4
*/
}
}``````

#### Output

`````` Given array :  -11 1 3 7 1 6 7 2 1 7
Given sum : 14
Subarray size : 3

Given array :  2 1 1 5 1 4 5 3 5
Given sum : 8
Subarray size : 2

Given array :  1 2 1 -2 1 1 2 -2 4 0
Given sum : 4
Subarray size : 4``````
``````// Swift 4 program for
// Smallest subarray with sum greater than a given value
class Pairs
{
// Display array elements
func printArray(_ arr: [Int], _ n: Int)
{
var i: Int = 0;
while (i < n)
{
print(" ", arr[i], terminator: "");
i += 1;
}
}
func smallestLengthSubarray(_ arr: [Int], _ n: Int, _ k: Int)
{
// Define some auxiliary variable
var sum: Int = 0;
var length: Int = n;
var front: Int = 0;
var i: Int = 0;
while (front < n)
{
// Execute loop under given k sum
while (i < n && sum <= k)
{
sum = sum + arr[i];
i += 1;
}
if (sum > k && length > (i - front))
{
// Change window size
length = (i - front);
}
// Change position
front += 1;
// Rest sum and i
sum = 0;
i = front;
}
// Display given array
print("\n Given array : ", terminator: "");
self.printArray(arr, n);
print("\n Given sum : ", k, terminator: "");
if (length == n + n)
{
// When no subarray is greater than k
print("\n None ");
}
else
{
// Display calculated result
print("\n Subarray size : ", length ," ");
}
}
}
func main()
{
let arr1: [Int] = [-11, 1, 3, 7, 1, 6, 7, 2, 1, 7];
let arr2: [Int] = [2, 1, 1, 5, 1, 4, 5, 3, 5];
let arr3: [Int] = [1, 2, 1, -2, 1, 1, 2, -2, 4, 0];
// Test A
var n: Int = arr1.count;
var k: Int = 14;
/*
arr = [  -11, 1, 3, 8, 1, 2, 6, 7, 2, 1, 7 ]
sum = 14
-------------------------------------------
2, 6, 7
-----------------------
Result : 3
*/
// Test B
k = 8;
n = arr2.count;
/*
arr = [2, 1, 1, 5, 1, 4, 5, 3, 5]
k = 8
---------------------------------
4, 5
-----------------------
Result : 2
*/
// Test C
k = 4;
n = arr3.count;
/*
arr = [1, 2, 1, -2,  1, 1, 2, -2, 4, 0]
k = 4
--------------------
[1, 2, -2, 4]
--------------------
Result : 4
*/
}
main();``````

#### Output

`````` Given array :   -11  1  3  7  1  6  7  2  1  7
Given sum :  14
Subarray size :  3

Given array :   2  1  1  5  1  4  5  3  5
Given sum :  8
Subarray size :  2

Given array :   1  2  1  -2  1  1  2  -2  4  0
Given sum :  4
Subarray size :  4``````
``````// Kotlin program for
// Smallest subarray with sum greater than a given value
class Pairs
{
// Display array elements
fun printArray(arr: Array < Int > , n: Int): Unit
{
var i: Int = 0;
while (i < n)
{
print(" " + arr[i]);
i += 1;
}
}
fun smallestLengthSubarray(arr: Array < Int > , n: Int, k: Int): Unit
{
// Define some auxiliary variable
var sum: Int = 0;
var length: Int = n;
var front: Int = 0;
var i: Int = 0;
while (front < n)
{
// Execute loop under given k sum
while (i < n && sum <= k)
{
sum = sum + arr[i];
i += 1;
}
if (sum > k && length > (i - front))
{
// Change window size
length = (i - front);
}
// Change position
front += 1;
// Rest sum and i
sum = 0;
i = front;
}
// Display given array
print("\n Given array : ");
this.printArray(arr, n);
print("\n Given sum : " + k);
if (length == n + n)
{
// When no subarray is greater than k
print("\n None \n");
}
else
{
// Display calculated result
print("\n Subarray size : " + length + " \n");
}
}
}
fun main(args: Array < String > ): Unit
{
val arr1: Array < Int > = arrayOf(-11, 1, 3, 7, 1, 6, 7, 2, 1, 7);
val arr2: Array < Int > = arrayOf(2, 1, 1, 5, 1, 4, 5, 3, 5);
val arr3: Array < Int > = arrayOf(1, 2, 1, -2, 1, 1, 2, -2, 4, 0);
// Test A
var n: Int = arr1.count();
var k: Int = 14;
/*
arr = [  -11, 1, 3, 8, 1, 2, 6, 7, 2, 1, 7 ]
sum = 14
-------------------------------------------
2, 6, 7
-----------------------
Result : 3
*/
// Test B
k = 8;
n = arr2.count();
/*
arr = [2, 1, 1, 5, 1, 4, 5, 3, 5]
k = 8
---------------------------------
4, 5
-----------------------
Result : 2
*/
// Test C
k = 4;
n = arr3.count();
/*
arr = [1, 2, 1, -2,  1, 1, 2, -2, 4, 0]
k = 4
--------------------
[1, 2, -2, 4]
--------------------
Result : 4
*/
}``````

#### Output

`````` Given array :  -11 1 3 7 1 6 7 2 1 7
Given sum : 14
Subarray size : 3

Given array :  2 1 1 5 1 4 5 3 5
Given sum : 8
Subarray size : 2

Given array :  1 2 1 -2 1 1 2 -2 4 0
Given sum : 4
Subarray size : 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.

Categories
Relative Post