What is the problem in my this code 2 test case failed

import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int k = sc.nextInt();
String s = sc.next();
char[] arr = s.toCharArray();

	  int low = 0;
	  int high = arr.length-1;

	  while(low < high && k!=0){
		  if(arr[low]!=arr[low+1]){
			  arr[low] = arr[low+1];
			  low++;
			  k--;
		  }
		  if(arr[high]!=arr[high-1]){
			  arr[high] = arr[high-1];
			  high--;
			  k--;
		  }
		  low++;high--;
	  }
	  String res = String.valueOf(arr);
	  System.out.println(res.length());
}

}

pls help me what is the problem in my code

Hey @nigamshubham1998
try for this input
2
ababaaabb
correct output: 7
your code gives : 9