Sum of all elements between k1 ' th ▁ and ▁ k2' th smallest elements | Java program to find sum of all element between to K1 ' th ▁ and ▁ k2' th smallest elements in array ; Returns sum between two kth smallest element of array ; Sort the given array ; Below code is equivalent to ; Driver code
import java . util . Arrays ; class GFG { static int sumBetweenTwoKth ( int arr [ ] , int k1 , int k2 ) { Arrays . sort ( arr ) ; int result = 0 ; for ( int i = k1 ; i < k2 - 1 ; i ++ ) result += arr [ i ] ; return result ; } public static void main ( String [ ] args ) { int arr [ ] = { 20 , 8 , 22 , 4 , 12 , 10 , 14 } ; int k1 = 3 , k2 = 6 ; int n = arr . length ; System . out . print ( sumBetweenTwoKth ( arr , k1 , k2 ) ) ; } }