0
Please solve this problem with any programming language .
Function calling program find the kth occurrence of an integer n Write a program to find the kth occurrence of an integer n in a sequence of non-negative integers, and then call your function from main. for details about what to do go to the link given below and run the code :- https://code.sololearn.com/W2l9NR3oEsIy/?ref=app
2 Answers
+ 4
(function main() {
var res, inp = prompt('give the first line input (n and k values):');
inp = inp.split(' ');
if (inp.length!=2) {
alert('Input Error\n\nExpected two values space separated, getting '+inp.length+'...');
return;
}
var n = parseInt(inp[0]);
var k = parseInt(inp[1]);
if (Number.isNaN(n)||Number.isNaN(k)||n<=0||k<=0) {
alert('Input Error\n\nExpected numerical positive values space separated, getting '+inp[0]+' and '+inp[1]+'...');
return;
}
var inp = prompt('give the second line input (sequence values):');
inp = inp.split(' ');
if (inp[inp.length-1]!='-1') {
alert('Sequence Input Error\n\nShould end with -1...');
return;
}
for (var i in inp) {
if (inp[i]==n) k--;
if (!k) break;
}
res = (k) ? -1 : i;
alert('Result:\n\n'+res);
})();
/*
But your question is quite unreadable, and the description in code linked a bad way of doing, all the more if you just copy plain text in a web document ;P
*/
0
incomplete specification: what if there is no kth occurrence of n?
Edit: sorry my mistake, I haven't read it thoroughly...