Solution using C Language:
Doubly_Linked_List||Insert_front && Delete_front||programming_info
#include<stdio.h>
#include<stdlib.h>
struct dnode
{
int info;
struct dnode*prev;
struct dnode*next;
};
void insert_front(struct dnode **p,int item)
{
struct dnode *temp;
temp=(struct dnode*)malloc(sizeof(struct dnode));
temp->prev=NULL;
temp->info=item;
temp->next=NULL;
struct dnode *q;
q=*p;
if(q==NULL)
{
*p=temp;
}
else
{
temp->next=q;
q->prev=temp;
*p=temp;
}
}
void delete_front(struct dnode **p)
{
struct dnode *q;
q=*p;
if(q==NULL)
{
printf("No nodes to delete\n");
exit(0);
}
else if(q->next==NULL)
{
free(q);
*p=NULL;
}
else
{
struct dnode *r;
r=q;
r=r->next;
r->prev=NULL;
q->next=NULL;
free(q);
*p=r;
}
}
void display(struct dnode *p)
{
while(p!=NULL)
{
printf("%d ",p->info);
p=p->next;
}
}
int main()
{
int ch;
struct dnode *first=NULL;
int item;
do
{
printf("1.Insert_front 2.Delete _front 3.Display 4.Exit\n");
printf("Enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:{
printf("Enter the item to be inserted\n");
scanf("%d",&item);
insert_front(&first,item);
}
break;
case 2:{
delete_front(&first);
}
break;
case 3:{
display(first);
}
break;
case 4:exit(0);
}
}
while(ch);
return 0;
}
Output:
1.Insert_front 2.Delete _front 3.Display 4.Exit
Enter your choice
1
Enter the item to be inserted
5
1.Insert_front 2.Delete _front 3.Display 4.Exit
Enter your choice
3
5 1.Insert_front 2.Delete _front 3.Display 4.Exit
Enter your choice
2
1.Insert_front 2.Delete _front 3.Display 4.Exit
Enter your choice
4
Singly_Linked_List||Insert Rear and Delete Rear||programming_info
Solution using C language:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node*link;
};
void insert_rear(struct node **p,int item)
{
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
temp->link=NULL;
temp->info=item;
if(*p==NULL)
{
*p=temp;
}
else
{
struct node *q;
q=*p;
while(q->link!=NULL)
{
q=q->link;
}
q->link=temp;
}
}
void delete_rear(struct node **p)
{
struct node *q;
q=*p;
if(q==NULL)
{
printf("No nodes to delete\n");
exit(0);
}
else if(q->link==NULL)
{
free(q);
*p=NULL;
}
else
{
struct node *r;
r=NULL;
while(q->link!=NULL)
{
r=q;
q=q->link;
}
free(q);
r->link=NULL;
}
}
void display(struct node *p)
{
while(p!=NULL)
{
printf("%d ",p->info);
p=p->link;
}
}
int main()
{
int ch;
struct node *first=NULL;
int item;
do
{
printf("1.Insert_rear 2.Delete _rear 3.Display 4.Exit\n");
printf("Enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:{
printf("Enter the item to be inserted\n");
scanf("%d",&item);
insert_rear(&first,item);
}
break;
case 2:{
delete_rear(&first);
}
break;
case 3:{
display(first);
}
break;
case 4:exit(0);
}
}
while(ch);
return 0;
}
Output:
1.Insert_rear 2.Delete _rear 3.Display 4.Exit
Enter your choice
1
Enter the item to be inserted
4
1.Insert_rear 2.Delete _rear 3.Display 4.Exit
Enter your choice
1
Enter the item to be inserted
5
1.Insert_rear 2.Delete _rear 3.Display 4.Exit
Enter your choice
2
1.Insert_rear 2.Delete _rear 3.Display 4.Exit
Enter your choice
3
4 1.Insert_rear 2.Delete _rear 3.Display 4.Exit
Enter your choice
4
Singly_Linked_List||Insert front and Delete front||programming_info
Solution using C language:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node*link;
};
void insert_front(struct node **p,int item)
{
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
temp->link=NULL;
temp->info=item;
if(*p==NULL)
{
*p=temp;
}
else
{
temp->link=*p;
*p=temp;
}
}
void delete_front(struct node **p)
{
struct node *q;
q=*p;
if(q==NULL)
{
printf("No nodes to delete\n");
exit(0);
}
else if(q->link==NULL)
{
free(q);
*p=NULL;
}
else
{
struct node *r;
r=q;
r=r->link;
free(q);
*p=r;
}
}
void display(struct node *p)
{
while(p!=NULL)
{
printf("%d ",p->info);
p=p->link;
}
}
int main()
{
int ch;
struct node *first=NULL;
int item;
do
{
printf("1.Insert_front 2.Delete _front 3.Display 4.Exit\n");
printf("Enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:{
printf("Enter the item to be inserted\n");
scanf("%d",&item);
insert_front(&first,item);
}
break;
case 2:{
delete_front(&first);
}
break;
case 3:{
display(first);
}
break;
case 4:exit(0);
}
}
while(ch);
return 0;
}
Output:
1.Insert_front 2.Delete _front 3.Display 4.Exit
Enter your choice
1
Enter the item to be inserted
5
1.Insert_front 2.Delete _front 3.Display 4.Exit
Enter your choice
3
5 1.Insert_front 2.Delete _front 3.Display 4.Exit
Enter your choice
2
1.Insert_front 2.Delete _front 3.Display 4.Exit
Enter your choice
4
Enormous Input Test||codechef||beginner solution ||programming info
Enormous Input Test
The purpose of this problem is to verify whether the method you are using to read input data is sufficiently fast to handle problems branded with the enormous Input/Output warning. You are expected to be able to process at least 2.5MB of input data per second at runtime.
Input
The input begins with two positive integers n k (n, k<=107). The next n lines of input contain one positive integer ti, not greater than 109, each.
Output
Write a single integer to output, denoting how many integers ti are divisible by k.
Example
Input: 7 3 1 51 966369 7 9 999996 11 Output: 4
solution using c++:-
// Note that this problem is for testing fast input-output.
// We can use scanf, printf in C langauge, which are quite fast in general :)
#include <bits/stdc++.h>
using namespace std;
// Usually, you can use scanf/printf in C++.
// However, if you want to use cin/cout, it is usually slow.
// To make it faster. Use cin.tie(NULL) and set ios_base::sync_with_stdio(false)
// See the below code for details.
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// Read the input n, k
int n, k;
cin >> n >> k;
// ans denotes number of integers n divisible by k
int ans = 0;
for (int i = 0; i < n; i++) {
int t;
cin >> t;
if (t % k == 0) {
ans++;
}
}
// Print the ans.
cout << ans << "\n";
return 0;
}
alternate solution:-
#include <iostream>
#include<cstdio>
using namespace std;
int main() {
// Read the input n, k
int n, k;
scanf("%d %d",&n,&k);
// ans denotes number of integers n divisible by k
int ans = 0;
for (int i = 0; i < n; i++) {
int t;
scanf("%d",&t);
if (t % k == 0) {
ans++;
}
}
// Print the ans.
printf("%d\n",ans);
return 0;
}
#include<cstdio>
using namespace std;
int main() {
// Read the input n, k
int n, k;
scanf("%d %d",&n,&k);
// ans denotes number of integers n divisible by k
int ans = 0;
for (int i = 0; i < n; i++) {
int t;
scanf("%d",&t);
if (t % k == 0) {
ans++;
}
}
// Print the ans.
printf("%d\n",ans);
return 0;
}
Turbo Sort||codechef||beginner solution ||programming info
Turbo Sort
Given the list of numbers, you are to sort them in non decreasing order.
Input
t – the number of numbers in list, then t lines follow [t <= 10^6].
Each line contains one integer: N [0 <= N <= 10^6]
Each line contains one integer: N [0 <= N <= 10^6]
Output
Output given numbers in non decreasing order.
Example
Input:
5 5 3 6 7 1
Output:
1 3 5 6 7
Solution using C++ language:
#include <iostream> #include<bits/stdc++.h> using namespace std; int main() { // your code goes here int t,i; cin>>t; int a[t]; for(i=0;i<t;i++) { cin>>a[i]; } sort(a,a+t); for(i=0;i<t;i++) { cout<<a[i]<<endl; } return 0; }
Small factorials||codechef||beginner solution ||programming info
Small factorials
You are asked to calculate factorials of some small positive integers.
Input
An integer t, 1<=t<=100, denoting the number of testcases, followed by t lines, each containing a single integer n, 1<=n<=100.
Output
For each integer n given at input, display a line with the value of n!
Example
Sample input:
4 1 2 5 3
Sample output:
1 2 120 6
Solution using C++ language:
#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>
using namespace boost::multiprecision;
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
cpp_int f=1;
for(int i=1;i<=n;i++)
{
f=f*i;
}
cout<<f<<endl;
}
return 0;
}
alternate solution:-
#include <iostream>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int size=1000,fact[size],extra=0,j=size-1;
fact[size-1]=1;
while(n>1)
{
int x;
for(int k=size-1;k>=j;k--)
{
x=fact[k]*n+extra;
fact[k]=x%10;
extra=x/10;
}
while(extra>0)
{
fact[--j]=extra%10;
extra/=10;
}
n--;
}
for(int k=j;k<size;k++)
{
cout<<fact[k];
}
cout<<endl;
}
return 0;
}
Day 9: factorial 3|| hackerrank || 30 days of code || programming_info
Objective
Today, we're learning and practicing an algorithmic concept called Recursion. Check out the Tutorial tab for learning materials and an instructional video!
Today, we're learning and practicing an algorithmic concept called Recursion. Check out the Tutorial tab for learning materials and an instructional video!
Recursive Method for Calculating Factorial
Task
Write a factorial function that takes a positive integer, as a parameter and prints the result of ( factorial).
Write a factorial function that takes a positive integer, as a parameter and prints the result of ( factorial).
Note: If you fail to use recursion or fail to name your recursive function factorial or Factorial, you will get a score of .
Input Format
A single integer, (the argument to pass to factorial).
Constraints
- Your submission must contain a recursive function named factorial.
Output Format
Print a single integer denoting .
Sample Input
3
Sample Output
6
Explanation
Consider the following steps:
From steps and , we can say ; then when we apply the value from to step , we get . Thus, we print as our answer.
Solution using C language:
#include <assert.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* readline();
// Complete the factorial function below.
int factorial(int n) {
if (n>=1)
return n*factorial(n-1);
else
return 1;
}
int main()
{
FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w");
char* n_endptr;
char* n_str = readline();
int n = strtol(n_str, &n_endptr, 10);
if (n_endptr == n_str || *n_endptr != '\0') { exit(EXIT_FAILURE); }
int result = factorial(n);
fprintf(fptr, "%d\n", result);
fclose(fptr);
return 0;
}
char* readline() {
size_t alloc_length = 1024;
size_t data_length = 0;
char* data = malloc(alloc_length);
while (true) {
char* cursor = data + data_length;
char* line = fgets(cursor, alloc_length - data_length, stdin);
if (!line) { break; }
data_length += strlen(cursor);
if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') { break; }
size_t new_length = alloc_length << 1;
data = realloc(data, new_length);
if (!data) { break; }
alloc_length = new_length;
}
if (data[data_length - 1] == '\n') {
data[data_length - 1] = '\0';
}
data = realloc(data, data_length);
return data;
}