Sum of Array 3 numbers equals to given value java code
public class TripletSum {
public static void checkForSum(int arr[], int len, int sum) {
for (int i = 0; i < len ; i++) {
int left = i + 1;
int right = len - 1;
while (right > left) {
if (arr[right] + arr[left] + arr[i] - sum == 0) {
System.out.println("Three indexes are:" + i + " "+ left + " "+ right);
break;
}
if (arr[right] + arr[left] + arr[i] - sum > 0)
right--;
else
left++;
}
}
}
public static void main(String[] args) {
int[] a = {5,2,1,1};
checkForSum(a,4,6);
}
}
Comments
Post a Comment