File tree Expand file tree Collapse file tree 3 files changed +134
-0
lines changed
Hackerrank/30 Days of Code Expand file tree Collapse file tree 3 files changed +134
-0
lines changed Original file line number Diff line number Diff line change 1+ import java .io .*;
2+ import java .util .*;
3+ class Node {
4+ int data ;
5+ Node next ;
6+ Node (int d ){
7+ data =d ;
8+ next =null ;
9+ }
10+
11+ }
12+ class Solution
13+ {
14+ public static Node removeDuplicates (Node head ) {
15+ Node curr = head ;
16+
17+ while (curr != null ) {
18+ if (curr .next != null && curr .data == curr .next .data ) {
19+ curr .next = curr .next .next ;
20+ }
21+ else {
22+ curr = curr .next ;
23+ }
24+ }
25+
26+ return head ;
27+ }
28+ public static Node insert (Node head ,int data )
29+ {
30+ Node p =new Node (data );
31+ if (head ==null )
32+ head =p ;
33+ else if (head .next ==null )
34+ head .next =p ;
35+ else
36+ {
37+ Node start =head ;
38+ while (start .next !=null )
39+ start =start .next ;
40+ start .next =p ;
41+
42+ }
43+ return head ;
44+ }
45+ public static void display (Node head )
46+ {
47+ Node start =head ;
48+ while (start !=null )
49+ {
50+ System .out .print (start .data +" " );
51+ start =start .next ;
52+ }
53+ }
54+ public static void main (String args [])
55+ {
56+ Scanner sc =new Scanner (System .in );
57+ Node head =null ;
58+ int T =sc .nextInt ();
59+ while (T -->0 ){
60+ int ele =sc .nextInt ();
61+ head =insert (head ,ele );
62+ }
63+ head =removeDuplicates (head );
64+ display (head );
65+
66+ }
67+ }
Original file line number Diff line number Diff line change 1+ import java .io .*;
2+ import java .util .*;
3+
4+ public class Solution {
5+
6+ public static void main (String [] args ) throws Exception {
7+
8+ BufferedReader br =new BufferedReader (new InputStreamReader (System .in ));
9+
10+ int n = Integer .parseInt (br .readLine ());
11+ for (int i =0 ;i <n ;i ++) {
12+ int num = Integer .parseInt (br .readLine ());
13+ System .out .println (isPrime (num ) ? "Prime" : "Not prime" );
14+ }
15+ }
16+
17+ public static boolean isPrime (int n ) {
18+ if (n < 2 ) return false ;
19+ if (n == 2 || n == 3 ) return true ;
20+ if (n % 2 == 0 || n % 3 == 0 ) return false ;
21+
22+ for (int i =5 ; i <= (int )Math .sqrt (n ); i ++) {
23+ if (n %i == 0 ) {
24+ return false ;
25+ }
26+ }
27+
28+ return true ;
29+ }
30+ }
Original file line number Diff line number Diff line change 1+ import java .io .*;
2+ import java .util .*;
3+
4+ public class Solution {
5+
6+ public static void main (String [] args ) throws Exception {
7+ BufferedReader br =new BufferedReader (new InputStreamReader (System .in ));
8+
9+ String [] strs = br .readLine ().trim ().split ("\\ s+" );
10+
11+ int [] dayReturned = new int [3 ];
12+ for (int i =0 ;i <3 ;i ++) {
13+ dayReturned [i ] = Integer .parseInt (strs [i ]);
14+ }
15+
16+ strs = br .readLine ().trim ().split ("\\ s+" );
17+
18+ int [] actualDayReturned = new int [3 ];
19+ for (int i =0 ;i <3 ;i ++) {
20+ actualDayReturned [i ] = Integer .parseInt (strs [i ]);
21+ }
22+
23+ int fine = 0 ;
24+
25+ if (dayReturned [2 ] == actualDayReturned [2 ] && dayReturned [1 ] == actualDayReturned [1 ] && dayReturned [0 ] > actualDayReturned [0 ]) {
26+ fine += 15 *(dayReturned [0 ] - actualDayReturned [0 ]);
27+ }
28+ else if (dayReturned [2 ] == actualDayReturned [2 ] && dayReturned [1 ] > actualDayReturned [1 ]) {
29+ fine += 500 *(dayReturned [1 ] - actualDayReturned [1 ]);
30+ }
31+ else if (dayReturned [2 ] > actualDayReturned [2 ]) {
32+ fine = 10000 ;
33+ }
34+
35+ System .out .println (fine );
36+ }
37+ }
You can’t perform that action at this time.
0 commit comments