Message on Whatsapp 8879355057 for DSA(OA + Interview) + Fullstack Dev Training + 1-1 Personalized Mentoring to get 10+LPA Job
0 like 0 dislike
1,131 views
in Online Assessments by Expert (46,090 points) | 1,131 views

1 Answer

0 like 0 dislike

Q1) You are given a positive Integer N, your task is to find the next smallest integer greater than N that does not contain two identical consecutive digits

 

ex: N=1765 -> 1767 beause 1766 has two identical consecutive digits
N=54 -> 56 not 55 because two identical digits

 

Solved using brute force, could think of an optimal solution.

 

	function sol(N){
		while(true){
			if(!checkConsecutive(`${N+1}`)){
				return N+1;
			}
			N++
		}
	}
	
	function checkConsecutive(N){
		if(N.length == 1) return false;
		let flag = false;
		for(let i = 0;i<N.length-1;i++){
			if(N[i] === N[i+1]){
				flag = true
			}
		}
		return flag
	}
by Expert (46,090 points)