File tree Expand file tree Collapse file tree 13 files changed +383
-0
lines changed
GeeksForGeeks/Interview Prep Course/Mathematical and Algorithmic Puzzle Expand file tree Collapse file tree 13 files changed +383
-0
lines changed Original file line number Diff line number Diff line change
1
+ import java .util .*;
2
+ import java .lang .*;
3
+ import java .io .*;
4
+
5
+ import static java .lang .System .out ;
6
+
7
+ class GFG {
8
+ public static void main (String [] args ) throws Exception {
9
+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
10
+ int t = Integer .parseInt (br .readLine ());
11
+
12
+ while (t -- > 0 ) {
13
+ int s = Integer .parseInt (br .readLine ());
14
+ System .out .println (getDigitCubeSum (s ) == s ? "Yes" : "No" );
15
+ }
16
+ }
17
+
18
+ private static int getDigitCubeSum (int n ) {
19
+ int sum = 0 ;
20
+
21
+ while (n > 0 ) {
22
+ int rem = n % 10 ;
23
+ sum += rem * rem * rem ;
24
+ n /= 10 ;
25
+ }
26
+
27
+ return sum ;
28
+ }
29
+ }
Original file line number Diff line number Diff line change
1
+ import java .util .*;
2
+ import java .lang .*;
3
+ import java .io .*;
4
+
5
+ import static java .lang .System .out ;
6
+
7
+ class GFG {
8
+ public static void main (String [] args ) throws Exception {
9
+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
10
+ int t = Integer .parseInt (br .readLine ());
11
+
12
+ while (t -- > 0 ) {
13
+ String s = br .readLine ();
14
+ System .out .println (getDecimalValue (s ));
15
+ }
16
+ }
17
+
18
+ private static int getDecimalValue (String s ) {
19
+ int num = 0 ;
20
+ int pow = 0 ;
21
+
22
+ char [] ch = s .toCharArray ();
23
+ for (int i =ch .length -1 ; i >=0 ; i --) {
24
+ num += Math .pow (2 , pow ) * Character .getNumericValue (ch [i ]);
25
+ pow ++;
26
+ }
27
+
28
+ return num ;
29
+ }
30
+ }
Original file line number Diff line number Diff line change
1
+ import java .util .*;
2
+ import java .lang .*;
3
+ import java .io .*;
4
+
5
+ import static java .lang .System .out ;
6
+
7
+ class GFG {
8
+ public static void main (String [] args ) throws Exception {
9
+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
10
+ int t = Integer .parseInt (br .readLine ());
11
+
12
+ while (t -- > 0 ) {
13
+ String [] s = br .readLine ().split ("\\ s+" );
14
+ int a = Integer .parseInt (s [0 ]);
15
+ int b = Integer .parseInt (s [1 ]);
16
+
17
+ int numLeft = a ;
18
+ int numRight = a ;
19
+
20
+ while (true ) {
21
+ int leftRem = numLeft % b ;
22
+ int rightRem = numRight % b ;
23
+
24
+ if (leftRem == 0 && rightRem == 0 ) {
25
+ if (Math .abs (numLeft ) > Math .abs (numRight )) {
26
+ out .println (numLeft );
27
+ }
28
+ else {
29
+ out .println (numRight );
30
+ }
31
+ break ;
32
+ }
33
+ else if (leftRem == 0 ) {
34
+ out .println (numLeft );
35
+ break ;
36
+ }
37
+ else if (rightRem == 0 ) {
38
+ out .println (numRight );
39
+ break ;
40
+ }
41
+
42
+ numLeft --;
43
+ numRight ++;
44
+ }
45
+ }
46
+ }
47
+ }
Original file line number Diff line number Diff line change
1
+ import java .util .*;
2
+ import java .lang .*;
3
+ import java .io .*;
4
+
5
+ import static java .lang .System .out ;
6
+
7
+ class GFG {
8
+ public static void main (String [] args ) throws Exception {
9
+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
10
+ int t = Integer .parseInt (br .readLine ());
11
+
12
+ while (t -- > 0 ) {
13
+ System .out .println (factorial (Integer .parseInt (br .readLine ())));
14
+ }
15
+ }
16
+
17
+ private static long factorial (int n ) {
18
+ long fact = 1 ;
19
+ while (n > 0 ) {
20
+ fact *= n ;
21
+ n --;
22
+ }
23
+
24
+ return fact ;
25
+ }
26
+ }
Original file line number Diff line number Diff line change
1
+ import java .util .*;
2
+ import java .lang .*;
3
+ import java .io .*;
4
+
5
+ import static java .lang .System .out ;
6
+
7
+ class GFG {
8
+ public static void main (String [] args ) throws Exception {
9
+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
10
+ int t = Integer .parseInt (br .readLine ());
11
+
12
+ while (t -- > 0 ) {
13
+ String [] s = br .readLine ().split ("\\ s+" );
14
+ int a = Integer .parseInt (s [0 ]);
15
+ int b = Integer .parseInt (s [1 ]);
16
+
17
+ System .out .println (getGcd (a , b ));
18
+ }
19
+ }
20
+
21
+ private static int getGcd (int a , int b ) {
22
+ if (b ==0 ) {
23
+ return a ;
24
+ }
25
+
26
+ return getGcd (b , a %b );
27
+ }
28
+ }
Original file line number Diff line number Diff line change
1
+ import java .util .*;
2
+ import java .lang .*;
3
+ import java .io .*;
4
+
5
+ import static java .lang .System .out ;
6
+
7
+ class GFG {
8
+ public static void main (String [] args ) throws Exception {
9
+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
10
+ int t = Integer .parseInt (br .readLine ());
11
+
12
+ while (t -- > 0 ) {
13
+ String [] s = br .readLine ().split ("\\ s+" );
14
+ int a = Integer .parseInt (s [0 ]);
15
+ int b = Integer .parseInt (s [1 ]);
16
+
17
+ int gcd = getGcd (a , b );
18
+ int lcm = (a * b ) / gcd ;
19
+
20
+ out .println (lcm + " " + gcd );
21
+ }
22
+ }
23
+
24
+ private static int getGcd (int a , int b ) {
25
+ if (b ==0 ) {
26
+ return a ;
27
+ }
28
+
29
+ return getGcd (b , a %b );
30
+ }
31
+ }
Original file line number Diff line number Diff line change
1
+ import java .util .*;
2
+ import java .lang .*;
3
+ import java .io .*;
4
+
5
+ import static java .lang .System .out ;
6
+
7
+ class GFG {
8
+ public static void main (String [] args ) throws Exception {
9
+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
10
+ int t = Integer .parseInt (br .readLine ());
11
+
12
+ while (t -- > 0 ) {
13
+ out .println (isPrime (Integer .parseInt (br .readLine ())) ? "Yes" : "No" );
14
+ }
15
+ }
16
+
17
+ private static boolean isPrime (int n ) {
18
+ if (n == 1 ) {
19
+ return false ;
20
+ }
21
+
22
+ int limit = (int ) Math .sqrt (n );
23
+ for (int i =2 ; i <=limit ; i ++) {
24
+ if (n % i == 0 ) {
25
+ return false ;
26
+ }
27
+ }
28
+
29
+ return true ;
30
+ }
31
+ }
Original file line number Diff line number Diff line change
1
+ import java .util .*;
2
+ import java .lang .*;
3
+ import java .io .*;
4
+
5
+ import static java .lang .System .out ;
6
+
7
+ class GFG {
8
+ public static void main (String [] args ) throws Exception {
9
+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
10
+ int t = Integer .parseInt (br .readLine ());
11
+
12
+ while (t -- > 0 ) {
13
+ String [] s = br .readLine ().split ("\\ s+" );
14
+ int a = Integer .parseInt (s [0 ]);
15
+ int b = Integer .parseInt (s [1 ]);
16
+ int k = Integer .parseInt (s [2 ]);
17
+
18
+ long n = (long ) Math .pow (a , b );
19
+
20
+ String num = String .valueOf (n );
21
+ System .out .println (num .charAt (num .length () - k ));
22
+ }
23
+ }
24
+ }
Original file line number Diff line number Diff line change
1
+ import java .util .*;
2
+ import java .lang .*;
3
+ import java .io .*;
4
+
5
+ import static java .lang .System .out ;
6
+
7
+ class GFG {
8
+ public static void main (String [] args ) throws Exception {
9
+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
10
+ int t = Integer .parseInt (br .readLine ());
11
+
12
+ while (t -- > 0 ) {
13
+ int s = Integer .parseInt (br .readLine ());
14
+ System .out .println (getReverse (s ));
15
+ }
16
+ }
17
+
18
+ private static int getReverse (int n ) {
19
+ int rev = 0 ;
20
+ while (n > 0 ) {
21
+ rev = rev * 10 + n % 10 ;
22
+ n /= 10 ;
23
+ }
24
+
25
+ return rev ;
26
+ }
27
+ }
Original file line number Diff line number Diff line change
1
+ import java .util .*;
2
+ import java .lang .*;
3
+ import java .io .*;
4
+
5
+ import static java .lang .System .out ;
6
+
7
+ class GFG {
8
+ public static void main (String [] args ) throws Exception {
9
+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
10
+ int t = Integer .parseInt (br .readLine ());
11
+
12
+ while (t -- > 0 ) {
13
+ String [] s = br .readLine ().split ("\\ s+" );
14
+ int a1 = Integer .parseInt (s [0 ]);
15
+ int a2 = Integer .parseInt (s [1 ]);
16
+ int n = Integer .parseInt (br .readLine ());
17
+
18
+ int d = a2 - a1 ;
19
+
20
+ System .out .println (a1 + (n -1 ) * d );
21
+ }
22
+ }
23
+ }
Original file line number Diff line number Diff line change
1
+ import java .util .*;
2
+ import java .lang .*;
3
+ import java .io .*;
4
+
5
+ import static java .lang .System .out ;
6
+
7
+ class GFG {
8
+ public static void main (String [] args ) throws Exception {
9
+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
10
+ int t = Integer .parseInt (br .readLine ());
11
+
12
+ while (t -- > 0 ) {
13
+ String [] s = br .readLine ().split ("\\ s+" );
14
+ int a = Integer .parseInt (s [0 ]);
15
+ int b = Integer .parseInt (s [1 ]);
16
+
17
+ int n = Integer .parseInt (br .readLine ());
18
+
19
+ double r = b / (double ) a ;
20
+
21
+ System .out .println ((int ) (Math .floor (a * Math .pow (r , n -1 ))));
22
+ }
23
+ }
24
+ }
Original file line number Diff line number Diff line change
1
+ import java .util .*;
2
+ import java .lang .*;
3
+ import java .io .*;
4
+
5
+ import static java .lang .System .out ;
6
+
7
+ class GFG {
8
+ public static void main (String [] args ) throws Exception {
9
+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
10
+ int t = Integer .parseInt (br .readLine ());
11
+
12
+ while (t -- > 0 ) {
13
+ int s = Integer .parseInt (br .readLine ());
14
+ int digitSum = getDigitSum (s );
15
+ out .println (isPalindrome (String .valueOf (digitSum )) ? "YES" : "NO" );
16
+ }
17
+ }
18
+
19
+ private static boolean isPalindrome (String s ) {
20
+ return new StringBuilder (s ).reverse ().toString ().equals (s );
21
+ }
22
+
23
+ private static int getDigitSum (int n ) {
24
+ int sum = 0 ;
25
+
26
+ while (n > 0 ) {
27
+ sum += n % 10 ;
28
+ n /= 10 ;
29
+ }
30
+
31
+ return sum ;
32
+ }
33
+ }
Original file line number Diff line number Diff line change
1
+ import java .util .*;
2
+ import java .lang .*;
3
+ import java .io .*;
4
+
5
+ import static java .lang .System .out ;
6
+
7
+ class GFG {
8
+ public static void main (String [] args ) throws Exception {
9
+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
10
+ int t = Integer .parseInt (br .readLine ());
11
+
12
+ while (t -- > 0 ) {
13
+ String [] s = br .readLine ().split ("\\ s+" );
14
+ int n = Integer .parseInt (s [0 ]);
15
+ int r = Integer .parseInt (s [1 ]);
16
+
17
+ System .out .println (factorial (n ) / factorial (n - r ));
18
+ }
19
+ }
20
+
21
+ private static long factorial (int n ) {
22
+ long fact = 1 ;
23
+ while (n > 0 ) {
24
+ fact *= n ;
25
+ n --;
26
+ }
27
+
28
+ return fact ;
29
+ }
30
+ }
You can’t perform that action at this time.
0 commit comments