MIn-Max [Hackerearth]

Given an array of integers . Check if all the numbers between minimum and maximum number in array exist’s within the array .

Print ‘YES’ if numbers exist otherwise print ‘NO’(without quotes).

Input:

Integer N denoting size of array

Next line contains N space separated integers denoting elements in array

Output:

Output your answer

Constraints:

1<= N <= 1000

1<= a[i] <= 100

SAMPLE INPUT
6
4 2 1 3 5 6
SAMPLE OUTPUT
YES

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.io.*;
import java.util.*;


class TestClass {
    public static void main(String args[] ) throws Exception {

       Scanner sc = new Scanner(System.in);
       int n = sc.nextInt();
       int arr[] = new int[n];
       for(int i = 0; i<n;i++){
           arr[i] = sc.nextInt();
       }
       Arrays.sort(arr);
       for(int i = 0; i<n-1;i++){
           if(Math.abs(arr[i+1]-arr[i]) > 1){
               System.out.println("NO");
               return;
           }
       }
       
       System.out.println("YES");
    }
}

COUNT NUMBERS [Hackerearth]

Link: here

Your task is pretty simple, given a string S , find the total count of numbers present in the digit.

Input

The first line contains T , the number of test cases. The first line of each and every test case will contain an integer N , the length of the string. The second line of each and every test case will contain a string S of length N.

Output

For each and every test case, output the total count of numbers present in the string.

Constraints

  1. 0<T<200
  2. 0<N<10000
SAMPLE INPUT
1
26
sadw96aeafae4awdw2wd100awd
SAMPLE OUTPUT
4

Explanation

For the first test case, the string given is “sadw96aeafae4awdw2wd100awd”. There are a total of 4 numbers in the string – [96,4,2,100]. So, we output 4.

NOTE: I tried implementing with Java but it gave me NZEC so have to use CPP.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*
Ketan Ramteke
*/
#include <iostream>
#include <cstdio>
using namespace std;
int main(){
int t,n,count;
cin>>t;
while(t--){
 count = 0;
 cin>>n;
 char arr[n];
 cin>>arr;
 for(int i = 0; i<n;i++){        
    if(arr[i] >='0' && arr[i]<='9'){               
        count++;                 
        while(arr[i] >='0' && arr[i]<='9')
                 i++;
      }
    }
   cout<<count<<endl;
  }
}

Magical Word[Hackerearth]

 Link: here

Dhananjay has recently learned about ASCII values.He is very fond of experimenting. With his knowledge of ASCII values and character he has developed a special word and named it Dhananjay’s Magical word.

A word which consists of alphabets whose ASCII values is a prime number is a Dhananjay’s Magical word. An alphabet is Dhananjay’s Magical alphabet if its ASCII value is prime.

Dhananjay’s nature is to boast about the things he knows or has learned about. So just to defame his friends he gives few string to his friends and asks them to convert it to Dhananjay’s Magical word. None of his friends would like to get insulted. Help them to convert the given strings to Dhananjay’s Magical Word.

Rules for converting:

1.Each character should be replaced by the nearest Dhananjay’s Magical alphabet.

2.If the character is equidistant with 2 Magical alphabets. The one with lower ASCII value will be considered as its replacement.

Input format:

The first line of input contains an integer T number of test cases. Each test case contains an integer N (denoting the length of the string) and a stringS.

Output Format:

For each test case, print Dhananjay’s Magical Word in a new line.

Constraints:

1 <= T <= 100

1 <= |S| <= 500

SAMPLE INPUT
1
6
AFREEN
SAMPLE OUTPUT
CGSCCO

Explanation

ASCII values of alphabets in AFREEN are 65, 70, 82, 69, 69 and 78 respectively which are converted to CGSCCO with ASCII values 67, 71, 83, 67, 67, 79 respectively. All such ASCII values are prime numbers.

 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/* Ketan D Ramteke */

import java.util.*;

class Solution
{
	public static void main (String[] args) throws java.lang.Exception
	{
		int word[] = { 67,71,73,79,83,89,97,101,103,107,109,113};

		@SuppressWarnings("resource")
		Scanner sc = new Scanner(System.in);
		int t = sc.nextInt();
		
		for(int j = 0; j<t;j++){
			int sol = 67;
			sc.nextInt();
			char[] arr = sc.next().toCharArray();
			for(char ch:arr){

				int diff = 400;
				
				for(int i = 0; i<word.length;i++){
					int tDiff = Math.abs(ch-word[i]);
					if(tDiff<diff){
						sol = word[i];
						diff = tDiff;
					}
					
				}
				System.out.print((char)sol);
				
			}
			
			System.out.println(); 
		}
	}
}

Seating Arrangement[Hackerearth]

Akash and Vishal are quite fond of traveling. They mostly travel by railways. They were traveling on a train one day and they got interested in the seating arrangement of their compartment. The compartment looked something like

So they got interested to know the seat number facing them and the seat type facing them. The seats are denoted as follows:

  • Window Seat: WS
  • Middle Seat: MS
  • Aisle Seat: AS

You will be given a seat number, find out the seat number facing you and the seat type, i.e. WS, MS or AS.

INPUT
The first line of input will consist of a single integer T denoting number of test-cases. Each test-case consists of a single integer N denoting the seat-number.

OUTPUT
For each test case, print the facing seat-number and the seat-type, separated by a single space in a new line.

CONSTRAINTS

  • 1<=T<=105
  • 1<=N<=108
SAMPLE INPUT
2
18
40
SAMPLE OUTPUT
19 WS
45 AS

PLEASE DO TRY SOLVING BY YOURSELF BEFORE READING THE WHOLE SOLUTION.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <iostream>

using namespace std;

int main()

{

	int t,n,rem=0;

	cin>>t;

	while(t--){

		cin>>n;

		rem = n%12;

		switch(rem){

			case 0:

			cout<<(n-11)<<" WS"<<endl;

			break;

			case 1:

			cout<<(n+11)<<" WS"<<endl;

			break;

			case 2:

			cout<<(n+9)<<" MS"<<endl;

			break;

			case 3:

			cout<<(n+7)<<" AS"<<endl;

			break;

			case 4:

			cout<<(n+5)<<" AS"<<endl;

			break;

			case 5:

			cout<<(n+3)<<" MS"<<endl;

			break;

			case 6:

			cout<<(n+1)<<" WS"<<endl;

			break;

			case 7:

			cout<<(n-1)<<" WS"<<endl;

			break;

			case 8:

			cout<<(n-3)<<" MS"<<endl;

			break;

			case 9:

			cout<<(n-5)<<" AS"<<endl;

			break;

			case 10:

			cout<<(n-7)<<" AS"<<endl;

			break;

			case 11:

			cout<<(n-9)<<" MS"<<endl;

			break;

		}

	}

	return 0;

}

Longest subsequence in number

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/* 

Ketan Ramteke

*/

import java.util.*;
import java.lang.*;
import java.io.*;


class Solution
{
	public static void main (String[] args) throws java.lang.Exception
	{

		Scanner in = new Scanner(System.in);
		int N = in.nextInt();
		String str = ""+N;
		int sol = 0,t=0;
		char check = 'a';
		char[] arr = str.toCharArray();
		for(char ch:arr){
			
			if(check == ch){
				t++;
			}
			else{
				check = ch;
				t = 1;
			}
			
			sol = sol>t?sol:t;
		}
		System.out.println(sol);
		
		
	}
}

 

INPUT:
 111100000
OUTPUT:
 5

Reverse every two adjacent characters from string.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
/*
Ketan Ramteke
*/
import java.util.*;
import java.io.*;
import java.math.*;

class Solution {

	public static void main(String args[]) {
		Scanner in = new Scanner(System.in);
		String S = in.nextLine();
		char[] arr = S.toCharArray();
		for(int i = 1;i<arr.length;i+=2){
			System.out.print(arr[i]+""+arr[i-1]);
		}
		if(arr.length%2!=0)System.out.print(arr[arr.length-1]);
	}
}
INPUT:
ABCDEF
OUTPUT:
BADCFE

Counting the occurrences of characters in the string[in C].

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/*
Ketan Ramteke
Program to find out occurence of characters in the string

*/
#include 
#include 

int main(void) {
 // your code goes here
 int alpha[256],i,j=0;
 memset(alpha,0,sizeof(alpha));//set the value of all elements to 0
 char str[100];
 scanf("%s",str);
 //printf("%s %d\n",str,strlen(str));a
 
 for(i = 0; i<strlen(str);i++){
     j = str[i];
     alpha[j]++;
 }
 for(i = 0; i<=256;i++){  if(alpha[i]>0)
     printf("%c : %d\n",i,alpha[i]);
 }
 return 0;
}

Program to print a number of primes in a given range.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*
 * Ketan Ramteke, github/theketan2
 */

import java.io.*;
import java.util.*;
public class CandidateCode {
	static int prime(int n1, int n2) {
		boolean[] flag = new boolean[n2 + 1];
		int sol = 0;
		flag[1] = true;

		for (int i = 2; i <= n2; i++) {
			if (flag[i] == false) {
				int temp = i + i;
				while (temp <= n2) {
					flag[temp] = true;
					temp += i;
				}
			}
		}
		for (int i = n1; i <= n2; i++) {
			if (!flag[i]) {

 //uncomment following line to print primes
 //System.out.print(i+" ");
				sol++;
			}

		}
		return sol;
	}
	public static void main(String args[]) throws Exception {

 //Write code here
		Scanner sc = new Scanner(System.in);
		System.out.println(prime(sc.nextInt(), sc.nextInt()));

	}
}

Techgig Solution: Flipping Bit

Problem Link

 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Ketan Ramteke

public static int nochange_bits(String input1,int input2,int input3)
{
  //It could be optimized further. Very rudimentary solution

	
	if(input2 <=0 || input3<=0)
		return -1;
	int sol =0;
	char[] ch = input1.toCharArray();
	char[] chtemp = input1.toCharArray();

	for(int i = input2;i<=ch.length;i++){
		if(i%input2 == 0)
			if(ch[i-1]=='0')
				ch[i-1] = '1';
			else
				ch[i-1] = '0';
		}

		for(int i = input3;i<=ch.length;i++){
			if(i%input3 == 0)
				if(ch[i-1]=='0')
					ch[i-1] = '1';
				else
					ch[i-1] = '0';
			}

			for(int i = 0; i<ch.length;i++){
				if(ch[i]==chtemp[i])
					sol++;

			}
			return sol;

		}