// Given an array[n] . Check if subarray with Sum== K exists or not
import java.util.*;
import java.lang.*;
import java.io.*;
// The main method must be in a class named "Main".
class Main {
public static boolean subarrayWithSumK(int [] arr , int k ){
int n = arr.length;
for(int s = 0 ; s<n ; s++){
int sum = 0;
for(int e=s ; e<n ; e++){
sum+= arr[e];
if(sum==k){
return true ;
}
}
}
return false ;
}
public static void main(String[] args) {
int arr[] = {1, 4, 20, 3, 10, 5};
int k = 33;
System.out.println("Result :- " + subarrayWithSumK(arr,k));
}
}
To embed this project on your website, copy the following code and paste it into your website's HTML: