Skip to content

Commit f3f7a77

Browse files
authored
Merge pull request #671 from simranquirky/master
Codechef Dsa series solutions added
2 parents e1761d5 + 54051cd commit f3f7a77

12 files changed

+575
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/* You're given a number N. Print the first N lines of the below-given pattern.
2+
3+
1 2 3 4 5
4+
10 9 8 7 6
5+
11 12 13 14 15
6+
20 19 18 17 16
7+
21 22 23 24 25
8+
30 29 28 27 26
9+
Input:
10+
First-line will contain the number N.
11+
Output:
12+
Print the first N lines of the given pattern.
13+
14+
Constraints
15+
1≤N≤200
16+
Sample Input 1:
17+
4
18+
Sample Output 1:
19+
1 2 3 4 5
20+
10 9 8 7 6
21+
11 12 13 14 15
22+
20 19 18 17 16
23+
Sample Input 2:
24+
2
25+
Sample Output 2:
26+
1 2 3 4 5
27+
10 9 8 7 6
28+
EXPLANATION:
29+
In the first example, we'll print the first 4 lines of the given pattern.
30+
In the second example, we'll print the first 2 lines of the given pattern. */
31+
32+
import java.util.*;
33+
import java.lang.*;
34+
import java.io.*;
35+
36+
/* Name of the class has to be "Main" only if the class is public. */
37+
class Codechef
38+
{
39+
public static void main (String[] args) throws java.lang.Exception
40+
{
41+
Scanner sc= new Scanner(System.in);
42+
int n= sc.nextInt();
43+
for (int i=1; i<=n; i++)
44+
{
45+
if(i%2!=0)
46+
{
47+
for(int j=(5*(i-1)+1);j<=(5*i);j++)
48+
{
49+
System.out.print(j+" ");
50+
}
51+
System.out.println();
52+
}
53+
else{
54+
for(int j=(5*i);j>=(5*(i-1)+1);j--)
55+
{
56+
System.out.print(j+" ");
57+
}
58+
System.out.println();
59+
}
60+
61+
62+
}
63+
}
64+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/* Chef went to a shop and buys a pens and b pencils. Each pen costs x units and each pencil costs y units. Now find what is the total amount Chef will spend to buy a pens and b pencils.
2+
3+
Input:
4+
First-line will contain 4 space separated integers a, b, x and y respectively.
5+
Output:
6+
Print the answer in a new line.
7+
8+
Constraints
9+
1≤a,b,x,y≤103
10+
Sample Input 1:
11+
2 4 4 5
12+
Sample Output 1:
13+
28
14+
Sample Input 2:
15+
1 1 4 8
16+
Sample Output 2:
17+
12
18+
EXPLANATION:
19+
In the first example, total cost is (2 * 4 + 4 * 5) = 28.
20+
In the second example, total cost is (1 * 4 + 1 * 8) = 12. */
21+
22+
23+
#include <stdio.h>
24+
25+
int main(void) {
26+
27+
int a,b,x,y;
28+
scanf("%d %d %d %d",&a,&b,&x,&y);
29+
int pen_cost= a*x;
30+
int pencil_cost=b*y;
31+
int total=pen_cost+pencil_cost;
32+
printf("%d",total);
33+
34+
return 0;
35+
36+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/* You are given a number N and find all the distinct factors of N.
2+
3+
Input:
4+
First-line will contain the number N.
5+
Output:
6+
In the first line print number of distinct factors of N.
7+
In the second line print all distinct factors in ascending order separated by space.
8+
Constraints
9+
1≤N≤106
10+
Sample Input 1:
11+
4
12+
Sample Output 1:
13+
3
14+
1 2 4
15+
Sample Input 2:
16+
6
17+
Sample Output 2:
18+
4
19+
1 2 3 6
20+
EXPLANATION:
21+
In the first example, all factors of 4 are 1, 2, 4.
22+
In the second example, all factors of 6 are 1, 2, 3, 6. */
23+
24+
#include <stdio.h>
25+
26+
int main(void) {
27+
28+
int n;
29+
scanf("%d",&n);
30+
int c=0;
31+
int a[n];
32+
33+
for(int i=1;i<=n;i++)
34+
{
35+
if(n%i==0)
36+
{
37+
a[c]=i;
38+
c++;
39+
}
40+
}
41+
printf("%d \n",c);
42+
for(int i=0;i<c;i++)
43+
{
44+
printf("%d ",a[i]);
45+
}
46+
47+
return 0;
48+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/* Given three distinct integers A, B and C, print the second largest number among them.
2+
3+
Input:
4+
The input consists of three lines.
5+
The first line contains a single integer A.
6+
The second line contains a single integer B.
7+
The third line contains a single integer C.
8+
Output:
9+
Print the second largest number among A, B and C, in a separate line.
10+
11+
Constraints
12+
1≤A,B,C≤109
13+
Sample Input 1:
14+
2
15+
7
16+
21
17+
Sample Output 1:
18+
7
19+
Sample Input 2:
20+
14
21+
28
22+
16
23+
Sample Output 2:
24+
16
25+
EXPLANATION:
26+
In the first example, 7 is the second largest number among the given three numbers.
27+
In the second example, 16 is the second largest number among the given three numbers. */
28+
29+
#include <stdio.h>
30+
31+
int main(void) {
32+
33+
int n1,n2,n3;
34+
scanf("%d\n%d\n%d",&n1,&n2,&n3);
35+
if(((n1>n2)&&(n1<n3))||((n1<n2)&&(n1>n3)))
36+
{
37+
printf("%d",n1);
38+
}
39+
else if(((n2>n1)&&(n2<n3))||((n2<n1)&&(n2>n3)))
40+
{
41+
printf("%d",n2);
42+
}
43+
else
44+
{
45+
printf("%d",n3);
46+
}
47+
48+
return 0;
49+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/* You are given a list of N integers and a value K. Print 1 if K exists in the given list of N integers, otherwise print −1.
2+
3+
Input:
4+
First-line will contain two numbers N and K.
5+
Next line contains N space-separated numbers.
6+
Output:
7+
Print the answer in a new line.
8+
9+
Constraints
10+
1≤N,K,Ai≤105
11+
Sample Input 1:
12+
4 2
13+
1 2 3 4
14+
Sample Output 1:
15+
1
16+
Sample Input 2:
17+
4 4
18+
1 2 6 9
19+
Sample Output 2:
20+
-1
21+
EXPLANATION:
22+
In the first example, as 2 is present in the list.
23+
In the second example, 4 is not present in the list. */
24+
25+
#include <stdio.h>
26+
27+
int main(void) {
28+
29+
int n,k;
30+
scanf("%d %d",&n,&k);
31+
int a[n];
32+
int c=0;
33+
for(int i=1; i<=n; i++)
34+
{
35+
scanf("%d ",&a[i]);
36+
if(a[i]==k)
37+
{
38+
c=1;
39+
}
40+
}
41+
42+
if(c==1)
43+
{
44+
printf("1");
45+
}
46+
else
47+
{
48+
printf("-1");
49+
}
50+
return 0;
51+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/* You're given a number N. If N is divisible by 5 or 11 but not both then print "ONE"(without quotes). If N is divisible by both 5 and 11 then print "BOTH"(without quotes). If N is not divisible by 5 or 11 then print "NONE"(without quotes).
2+
3+
Input:
4+
First-line will contain the number N.
5+
Output:
6+
Print the answer in a newline.
7+
8+
Constraints
9+
1≤N≤103
10+
Sample Input 1:
11+
50
12+
Sample Output 1:
13+
ONE
14+
Sample Input 2:
15+
110
16+
Sample Output 2:
17+
BOTH
18+
Sample Input 2:
19+
16
20+
Sample Output 2:
21+
NONE
22+
EXPLANATION:
23+
In the first example, 50 is divisible by 5, but not 11.
24+
In the second example, 110 is divisible by both 5 and 11.
25+
In the third example, 16 is not divisible by 5 or 11. */
26+
27+
#include <stdio.h>
28+
29+
int main(void) {
30+
31+
int n;
32+
scanf("%d",&n);
33+
if((n%5==0)&&(n%11==0))
34+
{
35+
printf("BOTH");
36+
}
37+
else if((n%5==0)||(n%11==0))
38+
{
39+
printf("ONE");
40+
}
41+
else
42+
{
43+
printf("NONE");
44+
}
45+
return 0;
46+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/* Raju is planning to visit his favourite restaurant. He shall travel to it by bus. Only the buses whose numbers are divisible by 5 or by 6 shall take him to his destination. You are given a bus number N. Find if Raju can take the bus or not. Print YES if he can take the bus, otherwise print NO.
2+
3+
Input:
4+
The first and only line of the input shall contain an integer N, denoting the bus number.
5+
Output:
6+
Print YES if Raju can take that bus, else print NO.
7+
8+
Constraints
9+
1≤N≤106
10+
Sample Input 1:
11+
60
12+
Sample Output 1:
13+
YES
14+
Sample Input 2:
15+
16
16+
Sample Output 2:
17+
NO
18+
Sample Input 3:
19+
20
20+
Sample Output 3:
21+
YES
22+
EXPLANATION:
23+
In the first example, 60 is divisible by 5 and 6 both, so he can take the bus.
24+
In the second example, 16 is divisible by neither 5 nor 6, so he can't take the bus.
25+
In the third example, 20 is divisible by 5, so he can take the bus. */
26+
27+
#include <stdio.h>
28+
29+
int main(void) {
30+
31+
int t;
32+
scanf("%d",&t);
33+
//t is the bus number
34+
if((t%5==0)||(t%6==0))
35+
{
36+
printf("YES");
37+
}
38+
else
39+
{
40+
printf("NO");
41+
}
42+
return 0;
43+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/* You're given two numbers L and R. Print all odd numbers between L and R (both inclusive) in a single line separated by space, in ascending (increasing) order.
2+
3+
Input:
4+
First-line will contain two numbers L and R.
5+
Output:
6+
Print all odd numbers in a single line separated by space, in ascending (increasing) order.
7+
8+
Constraints
9+
1≤L<R≤106
10+
Sample Input 1:
11+
2 9
12+
Sample Output 1:
13+
3 5 7 9
14+
Sample Input 2:
15+
3 4
16+
Sample Output 2:
17+
3
18+
EXPLANATION:
19+
In the first example, odd numbers between 2 and 9 are 3,5,7,9.
20+
In the second example, the only odd number in the range is 3. */
21+
22+
#include <stdio.h>
23+
24+
int main(void) {
25+
26+
int start , end;
27+
scanf("%d %d",&start,&end);
28+
int a[end];
29+
int x=0;
30+
for(int i=start;i<=end;i++)
31+
{
32+
33+
if(i%2!=0)
34+
{
35+
a[x]=i;
36+
x++;
37+
}
38+
}
39+
40+
for (int y=0;y<x;y++)
41+
{
42+
printf("%d ",a[y]);
43+
}
44+
45+
return 0;
46+
}

0 commit comments

Comments
 (0)