File tree Expand file tree Collapse file tree
CrackingTheCodingInterview Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ /*
2+ Detect a cycle in a linked list. Note that the head pointer may be 'null' if the list is empty.
3+
4+ A Node is defined as:
5+ class Node {
6+ int data;
7+ Node next;
8+ }
9+ */
10+
11+ boolean hasCycle (Node head ) {
12+ Node slow_p = head , fast_p = head ;
13+ while (slow_p != null && fast_p != null && fast_p .next !=null ) {
14+ slow_p = slow_p .next ;
15+ fast_p = fast_p .next .next ;
16+ if (slow_p == fast_p ){
17+ return true ;
18+ }
19+ }
20+ return false ;
21+ }
Original file line number Diff line number Diff line change 1+ import java .io .*;
2+ import java .util .*;
3+
4+ public class LeftRotation {
5+
6+ public static void main (String args []) {
7+ Scanner sc = new Scanner (System .in );
8+ int n = sc .nextInt ();
9+ int d = sc .nextInt ();
10+ int a [] = new int [n ];
11+ for (int i = 0 ; i < n ; i ++)
12+ a [i ] = sc .nextInt ();
13+ for (int i = d ; i < n ; i ++)
14+ System .out .print (a [i ] + " " );
15+ for (int i = 0 ; i < d ; i ++)
16+ System .out .print (a [i ] + " " );
17+ }
18+
19+ }
Original file line number Diff line number Diff line change 1+ import java .io .*;
2+ import java .util .*;
3+
4+ public class MakingAnagrams {
5+
6+ public static int numberNeeded (String first , String second ) {
7+ int a [] = new int [26 ];
8+ int b [] = new int [26 ];
9+ for (int i = 0 ; i < first .length (); i ++) {
10+ a [first .charAt (i ) - 97 ]++;
11+ }
12+ for (int i = 0 ; i < second .length (); i ++) {
13+ b [second .charAt (i ) - 97 ]++;
14+ }
15+ int count = 0 ;
16+ for (int i = 0 ; i < 26 ; i ++) {
17+ count += Math .abs (a [i ] - b [i ]);
18+ }
19+ return count ;
20+ }
21+
22+ public static void main (String [] args ) {
23+ Scanner in = new Scanner (System .in );
24+ String a = in .next ();
25+ String b = in .next ();
26+ System .out .println (numberNeeded (a , b ));
27+ }
28+
29+ }
Original file line number Diff line number Diff line change 1+ import java .io .*;
2+ import java .util .*;
3+ import java .text .*;
4+ import java .math .*;
5+ import java .util .regex .*;
6+
7+ public class Primality {
8+
9+ public static void main (String [] args ) {
10+ Scanner in = new Scanner (System .in );
11+ int p = in .nextInt ();
12+ for (int a0 = 0 ; a0 < p ; a0 ++) {
13+ int n = in .nextInt ();
14+ int count = 0 ;
15+ for (int i = 2 ; i <= Math .sqrt (n ); i ++) {
16+ if (n % i == 0 ) {
17+ count ++;
18+ break ;
19+ }
20+ }
21+ System .out .println ((count != 0 || n == 1 ) ? "Not prime" : "Prime" );
22+ }
23+ }
24+
25+ }
Original file line number Diff line number Diff line change 1+ import java .io .*;
2+ import java .util .*;
3+
4+ public class RansomNote {
5+
6+ public static void main (String [] args ) {
7+ Scanner in = new Scanner (System .in );
8+ int m = in .nextInt ();
9+ int n = in .nextInt ();
10+ HashMap <String , Integer > map = new HashMap <String , Integer >();
11+ String magazine [] = new String [m ];
12+ for (int magazine_i = 0 ; magazine_i < m ; magazine_i ++) {
13+ magazine [magazine_i ] = in .next ();
14+ int count = (map .get (magazine [magazine_i ]) != null ) ? map .get (magazine [magazine_i ]) : 0 ;
15+ map .put (magazine [magazine_i ], count + 1 );
16+ }
17+ boolean possible = true ;
18+ String ransom [] = new String [n ];
19+ for (int ransom_i = 0 ; ransom_i < n ; ransom_i ++) {
20+ ransom [ransom_i ] = in .next ();
21+ if (map .get (ransom [ransom_i ]) == null || map .get (ransom [ransom_i ]) == 0 ) {
22+ possible = false ;
23+ break ;
24+ } else {
25+ map .put (ransom [ransom_i ], map .get (ransom [ransom_i ]) - 1 );
26+ }
27+ }
28+ System .out .println ((possible ) ? "Yes" : "No" );
29+ }
30+
31+ }
You can’t perform that action at this time.
0 commit comments