Posts

Featured Post

CBSE class 10 English Question Paper (2020)

Image

Jewel Game (Hack The Interview V : Asia-Pacific)

Contest Link import java.util.*; class Solution{     public static void main(String[] args){         Scanner sc = new Scanner(System.in);         int t = sc.nextInt();         while(t-->0){             String jewels = sc.next();             StringBuilder s = new StringBuilder();             s.append(jewels);             int count = 0;             while(true){                 int temp = 0;                 for(int i=0;i<s.length()-1;i++){                     if(s.charAt(i)==s.charAt(i+1)){                         count++;                         s.deleteCharAt(i);                         s.deleteCharAt(i);                         temp++;                     }                 }                 if(temp==0){                     break;                 }             }             System.out.println(count);         }     } }

The XOR Problem (Hack The Interview V : Asia-Pacific)

Contest Link import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.function.*; import java.util.regex.*; import java.util.stream.*; import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.toList; class Result { /* * Complete the 'maxXorValue' function below. * * The function is expected to return a STRING. * The function accepts following parameters: * 1. STRING x * 2. INTEGER k */ public static String maxXorValue(String x, int k) { StringBuilder s = new StringBuilder(); int n = 0; int i = 0; for(i=0;i<x.length();i++){ if(n<k){ if(x.charAt(i)=='0'){ s.append('1'); n += 1; } else{ s.append('0'); }

New KeyBoard (Hack The Interview V : Asia-Pacific)

Contest Link import java.util.*; class Solution{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); String s = sc.next(); StringBuilder res = new StringBuilder(""); boolean isPressed = false; int count = 0; boolean cursorStart = false; ArrayList<Character> c = new ArrayList<>(); for(int i=0;i<10;i++){ c.add((char)i); } for(int i=0;i<s.length();i++){ if(s.charAt(i)=='<'){ res = res.reverse(); cursorStart = true; continue; } else if(s.charAt(i)=='>'){ if(cursorStart == true){ res = res.reverse(); cursorStart = false; } continue; } else if(s.charAt(i)=='*'){ res.delete(res.length()-1, res.length());

Hackerrank Solution :- print-the-elements-of-a-linked-list

Question Link import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.regex.*; public class Solution {     static class SinglyLinkedListNode {         public int data;         public SinglyLinkedListNode next;         public SinglyLinkedListNode(int nodeData) {             this.data = nodeData;             this.next = null;         }     }     static class SinglyLinkedList {         public SinglyLinkedListNode head;         public SinglyLinkedListNode tail;         public SinglyLinkedList() {             this.head = null;             this.tail = null;         }         public void insertNode(int nodeData) {             SinglyLinkedListNode node = new SinglyLinkedListNode(nodeData);             if (this.head == null) {                 this.head = node;             } else {                 this.tail.next = node;             }             this.tail = node;         }     }     // Complete the pr

Seive of Eratosthenis algorithm to generate prime numbers.

Image
Sieve of Eratosthenis Algorithm:- 1. Create a list of consecutive integers from 2 to n: (2, 3, 4, …, n). 2. Initially, let p equal 2, the first prime number. 3. Starting from p2, count up in increments of p and mark each of these numbers greater than or equal to p2 itself in the list.    These numbers will be p(p+1), p(p+2), p(p+3), etc.. 4. Find the first number greater than p in the list that is not marked. If there was no such number, stop.     Otherwise, let p now equal this number (which is the next prime), and repeat from step 3. <-->  When the algorithm terminates, all the numbers in the list that are not marked false are prime.  If you want the code for it then comment down.