Stuck in a hackerrank question (Climbing the leaderboard) (mistake not TLE) [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 days ago.
Improve this question
I am trying to solve HackerRank's Climbing the Leaderboard
#include <stdio.h>
int leaderboard[200010];
int leaderboardRanking[200010];
int playerScore[200010];
int main(){
int players, gamesPlayed;
scanf("%d", &players);
for(int i=0;i<players;i++){
scanf("%d", &leaderboard[i]);
if(i==0) leaderboardRanking[i]=1;
else if(i>0 && leaderboard[i]==leaderboard[i-1]) leaderboardRanking[i]=leaderboardRanking[i-1];
else leaderboardRanking[i]=leaderboardRanking[i-1]+1;
}
scanf("%d", &gamesPlayed);
for(int i=0;i<gamesPlayed;i++){
scanf("%d", &playerScore[i]);
}
int previous = players;
for(int i=0;i<gamesPlayed;i++){
for(int j=previous;j>=0;j--){
if(playerScore[i]<leaderboard[j]){
printf("%d\n", leaderboardRanking[j]+1);
previous = j;
break;
}
else if(playerScore[i]==leaderboard[j]){
printf("%d\n", leaderboardRanking[j]);
previous = j;
break;
}
else if(j==0){
printf("%d\n", leaderboardRanking[j]);
previous = j;
break;
}
}
}
}

Related

How can i use the For or while loop to fill an array of strings? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
When i try this code i don't get any errors or warnings or anything, it just doesn't function properly and i can't figure out why, it shows the strings mixed up or missing characters.
#include <stdio.h>
#include <stdlib.h>
int main() {
char arrr[53];
int i,
j;
for (i = 0; i <= 5; i++) {
printf("please enter a string \n");
scanf("%s", &arrr[i]);
}
for (j = 0; j <= 5; j++)
printf("%s \n", &arrr[j]);
return 0;
}
I suspect you are trying to do something like:
#include <stdio.h>
#include <stdlib.h>
int
main(void)
{
char arrr[6][53];
int i;
for( i = 0; i < 6; i++) {
if( scanf("%52s", arrr[i]) != 1 ) {
break;
}
}
for( int j=0 ; j < i; j++ ) {
printf("%d: %s\n", j, arrr[j]);
}
return 0;
}

How do I convert this code into an array or malloc? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
#include <stdio.h>
#include <stdlib.h>
int factorial(int n) {
int f;
for(f = 1; n > 0; n--)
f *= n;
return f;
}
int ncr(int n,int r) {
return factorial(n) / ( factorial(n-r) * factorial(r) );
}
int main(int argc, char* argv[]) {
int n, i, j;
n = atoi(argv[1]);
for(i = 0; i <= n; i++) {
printf("1");
for(j = 1; j <= i; j++)
if(i == j && j > 0) printf(" 1");
else printf(" %d", ncr(i, j));
printf("\n");
}
return 0;
}
If I test it with the argument of a number that is above 12, I get strange numbers from row 12(?).. Why is this happening? should I use malloc or array? Can someone please change this codes into an array or malloc? Thanks.
Why strange? Factorial 13 is 6227020800, which exceeds INTMAX so you wrap into negative land. "long long" would likely work. Get to know the MAX and MIN values in for size limits.
Always be sure of your ranges before you start designing a program.

Function insertionSort does not sort the array [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
This is my code. After the call to the function insertionSort, when i print the array it prints the array without sorting it.
I am unable to understand if the problem is in the sorting algorithm or some other problem. The code compiles just fine and runs too, so that excludes the probability of any syntactic errors.
#include<stdio.h>
void insertionSort(int arr[], int n);
int main(){
int n, i;
printf("Enter n: ");
scanf("%d\n", &n);
int arr[n];
for(i=0; i<n; i++){
scanf("%d", &arr[i]);
}
insertionSort(arr, n);
for(i=0; i<n; i++){
printf("%d\n", arr[i]);
}
}
void insertionSort(int arr[], int n){
int i,j, key;
for(j=1; j<n; j++){
key = arr[j];
i = j-1;
while (i>0 && arr[i] > key) {
arr[i+1] = arr[i];
arr[i] = key;
i--;
}
}
}
You forgot to include arr[0] to the candidate to be inserted.
Try using while (i>=0 && arr[i] > key) instead of while (i>0 && arr[i] > key) for the loop.

What does this program do? (arrays+functions) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I tried to run this code so many times, every time the output is just the same, I'm not getting, what's the use of such a code?
#include<stdio.h>
#include<conio.h>
void increment(int,int, int);
int main()
{
int a[20],i;
clrscr();
printf("Enter array elements");
for(i=0;i<20;i++)
scanf("%d", &a[i]);
increment(a[0],a[5],a[2]);
printf("After passing array:\n");
for(i=0;i<20;i++)
printf("%d\t", a[i]);
getch();
return 0;
}
void increment(int x, int y, int z)
{
//int i;
x++;
y++;
z++;
//for(i=0;i<size;i++)
// x[i]++;
}
To have any usefulness, the address of the values to be modified need to be passed to increment(), not the values themselves:
Change this:
void increment(int x, int y, int z)
{
//int i;
x++;
y++;
z++;
// for(i=0;i<size;i++)
// x[i]++;
}
To this:
void increment(int *x, int *y, int *z)
{
//int i;
(*x)++;
(*y)++;
(*z)++;
// for(i=0;i<size;i++)
// x[i]++;
}
Then change this:
increment(a[0],a[5],a[2]);
to this:
increment(&a[0],&a[5],&a[2]);

I am getting the error "expected declaration specifiers or ‘...’ before ‘(’ token"? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
#include <stdio.h>
int main()
{
int i, F(int);
int F((int - 1) + (int - 2))
{
return (int - 1) + (int - 2)
}
for(i=1; i<=15; i++)
{
F(1) == 0;
F(i) == F(i-1)+F(i-2);
printf("F(%d) = %d", i, F(i));
}
return 0;
}
I am trying to print the fibonacci sequence from the first to the fifteenth number
#include <stdio.h>
int F(int n){
if(n==1)
return 0;
if(n==2)
return 1;
return F(n - 1) + F(n - 2);
}
int main(){
int i;
for(i=1; i<=15; i++){
printf("F(%d) = %d\n", i, F(i));
}
return 0;
}

Resources