Created
April 30, 2020 21:02
-
-
Save appikonda/7d765de68aa6cf87d56000e0c929c3fa to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
public class InterviewQuestion { | |
public static void main(String[] args) { | |
int arr[] = { 2, 2, 3, 4 }; | |
int num = 4; | |
System.out.println("Number is: " + num + "and its count :" + countNum(arr, num)); | |
} | |
static int countNum(int[] arr, int num) { | |
List<Integer> al = new ArrayList<>(arr.length); | |
for (int i : arr) { | |
al.add(Integer.valueOf(i)); | |
} | |
int i = al.indexOf(num); | |
int count = 0; | |
int nextIndex = 0; | |
while (i < arr.length) { | |
nextIndex = al.indexOf(num + 1); | |
if (nextIndex != -1) { | |
count++; | |
num++; | |
} else { | |
break; | |
} | |
i++; | |
} | |
return count; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment