Tuesday, April 21, 2020

Title

Title (100 Marks)

You just need to take string input and checks whether all the case-based characters in the string following non-casebased letters. Non-casebased letters are uppercase and all other case-based characters are lowercase.

Input Format
You will be given a function with string as single argument. 

Constraints
1 <= |S| <= 10^4

Output Format
You need to return the String either "True" or "False". 

Sample TestCase 1
Input
This Is String Example
Output
True
Solution:













import java.io.*;
import java.util.*;
public class CandidateCode {
      public static void main(String args[] ) throws Exception {
          Scanner sc = new Scanner(System.in);
          String s1 = sc.nextLine();
          StringBuffer s2 = new StringBuffer();

        String arr[] = s1.split(" ");
        for(int i=0; i<arr.length; i++)
        {
            s2.append(arr[i].substring(0,1).toUpperCase() + arr[i].substring(1)+" ");
        }
       String str = s2.toString();
       if(s1.equals(str.trim()))
             System.out.print("True");
        else
             System.out.print("False");
       }
}

Monday, April 20, 2020

Problem 2(Equality)

Equality (100 Marks)

The new world believes in equality for everyone and that they must have equal rights and individual identity, regardless of what they are and from where they belong. In this problem, everyone should be present equally. The string may contain lowercase alphabets, uppercase alphabets, numbers or other characters but everyone should exist equally. If the string is such then print "Equality For Everyone" otherwise print "No Equality".

Input Format
The first line of input consists of numbers of test cases, T.
Next T lines consist of strings to check.


Constraints
1<= T <=10
1<= |string| <=1000


Output Format
Print the required output.

Sample TestCase 1
Input
3
aaabbBBAAA12345)(*&^
aa11BB!!!!!!
aWsEdR1!2@3#
Output
Equality For Everyone
No Equality
Equality For Everyone
Solutions:



















import java.io.*;
import java.util.*;
public class CandidateCode {
public static void main(String args[] ) throws Exception {
Scanner sc = new Scanner (System.in);
String t = sc.nextLine();
int test = Integer.parseInt(t);
for(int j=0; j<test;j++){
int low=0, upper=0, special=0,num=0;
String str = sc.nextLine();
for(int i =0; i<str.length(); i++){
if(str.charAt(i)>='a' && str.charAt(i)<='z')
low++;
else if(str.charAt(i)>='A' && str.charAt(i)<='Z')
upper++;
else if(str.charAt(i)>='0' && str.charAt(i)<='9')
num++;
else if(str.charAt(i)=='!'||str.charAt(i)<='@'||str.charAt(i)=='('||str.charAt(i)==')'||str.charAt(i)=='*'||str.charAt(i)=='&'||str.charAt(i)=='^'||str.charAt(i)=='#')
special++;
}
if((low==upper) && (num==special) && (low==num) && (special==upper) && (low==special))
System.out.println("Equality For Everyone");
else
System.out.println("No Equality");
}
}
}

Problem 1(English Alphabets)

English Alphabets (100 Marks)

In English, there are two types of characters, vowels and consonants. Romi is a student in school and is new to computers. He knows about vowels and consonants but does not know how to write code to get the correct output. He needs your help to learn to code and print the number of vowels, consonants and the product of them. Help Romi understand the code.

Input Format
The first line of input consist of number of test cases, T.
Next T lines consist of strings.


Constraints
1<= T <=10
1<= |S| <=1000


Output Format
For each string print the number of vowels, number of consonants and the product of them space separately.

Sample TestCase 1
InpuT
2
abcdefgh
zxcvbnm
Output
2 6 12
0 7 

import java.io.*;
import java.util.*;
 public class CandidateCode {
          public static void main(String args[] ) throws Exception {
              Scanner sc = new Scanner(System.in);
              String test = sc.nextLine();
              int t = Integer.parseInt(test);
              for(int i=1;i<=t;i++){
                  String s = sc.nextLine();
               // System.out.println(s);
                 int v=0,c=0;
               for(int j=0;j<s.length();j++){
                   if(s.charAt(j)=='a'||s.charAt(j)=='e'||s.charAt(j)=='i'||s.charAt(j)=='o'||s.charAt(j)=='u')
                        v++;
                  else
                        c++;
                }
             System.out.println(v+" "+c+" "+v*c);
           }
       }
}

Wednesday, April 15, 2020

Dictionary order

Dictionary order (100 Marks)

You will be given an array of strings and you are asked to sort them lexicographically. Note: the length of the array should not be less than 2.

Input Format
You will be given a function with array of strings as single argument. 



Constraints

Size of array <= 10000



Output Format

You need to return the reverse lexicographically sorted array from the function.

Sample TestCase 1
Input

4
dky
amit
sumit
deepak
Output
amit
deepak
dky
sumit
JDK 8



















import java.io.*;
import java.util.*;
import jav.util.stream.*;
//import java.util.stream.*;
public class CandidateCode {

       public static void main(String args[] ) throws Exception {
       Scanner sc = new Scanner(System.in);
       int size=sc.nextInt();
       ArrayList<String> arr = new ArrayList<String>(size);

      for(int i=0 ;i<size; i++)
            arr.add(sc.next());

      List<String> list = arr;
      List<String> slist = list.stream().sorted().collect(Collectors.toList());

      for(int i=0; i<size; i++)
         if(i!=size-1)
            System.out.println(slist.get(i));
        else
            System.out.print(slist.get(i));
     }
}

Tuesday, April 14, 2020

Pokemon (100 Marks)

Sergei B. was very pleased, and now he wants to visit as few flats as possible in order to collect Pokemons of all types that appear in this house. Your task is to help him and determine this minimum number of flats he has to visit Sergei B., the young coach of Pokemons, has found the big house which consists of n flats ordered in a row from left to right. It is possible to enter each flat from the street. It is possible to go out from each flat. Also, each flat is connected with the flat to the left and the flat to the right. Flat number 1 is only connected with the flat number 2 and the flat number n is only connected with the flat number n - 1.

There is exactly one Pokemon of some type in each of these flats. Sergei B. asked residents of the house to let him enter their flats in order to catch Pokemons. After consulting the residents of the house decided to let Sergei B. enter one flat from the street, visit several flats and then go out from some flat. But they won't let him visit the same flat more than once.

Input Format
You will be given a function with string array as a single argument which contains the row s with the length n, it consists of uppercase and lowercase letters of English alphabet, the i-th letter equals the type of Pokemon, which is in the flat number i.

Constraints
1 < |S| < 10^5
Output Format
Return the minimum number of flats which Sergei B. should visit in order to catch Pokemons of all types which there are in the house.
Sample TestCase 1
Input
3
A
a
A
Output
2
Solution:























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

public class CandidateCode {
      public static void Count(String arr[], int n){
      Map<String, Integer> map = new HashMap<>();

       for(int i=0; i<n; i++){
             if(map.containsKey(arr[i])){
                  map.put(arr[i], map.get(arr[i])+1);
             }
            else
               map.put(arr[i],i);
       }
       System.out.print(map.size());
}
public static void main(String args[] ) throws Exception {
      Scanner sc = new Scanner(System.in);
      int len=sc.nextInt();

      String arr[] = new String[len];
      for(int i=0; i<arr.length; i++)
            arr[i]=sc.next();
           Count(arr, len);
  }
}

ODD (100 Marks)

You have to create a function odd(). The function should print only the odd numbers and should be able to handle the variable number of arguments.


odd(int, int)
odd(int, int ,int)
odd(int, int ,int, int)
odd(int, int ,int, int, int)



Input Format
There are 5 lines of input with each having an integer.



Constraints
1<= N <=50



Output Format
For each function call print the odd number in the arguments only space separately. If there is no odd integer print an empty line.

Sample TestCase 1
Input
1
2
3
4
5
Output
1
1
1 3
1 3
1 3 5
Solution:




















import java.io.*;
import java.util.*;
public class CandidateCode {
             public static void odd(int ...a){
             int temp[] =temp=new int[a.length];
             int c=0,k=0;
             for(int i=0; i<a.length; i++){
                  if(!(a[i]%2==0)){
                         c++;
                         temp[k++]=a[i];
                   }
             for(int j=0; j<c; j++){
                  if(j!=c-1)
                        System.out.print(temp[j]+ " ");
                  else
                        System.out.print(temp[j]);
            // if(i!=a.length-1)
                 System.out.println();
             }
    }
     public static void main(String args[] ) throws Exception {
           Scanner sc = new Scanner(System.in);
            int p=sc.nextInt();
            int q=sc.nextInt();
            int r=sc.nextInt();
            int s=sc.nextInt();
            int t=sc.nextInt();
            odd(p,q,r,s,t);
      }
}

Sunday, April 12, 2020

Add Occurrence Of Twice (100 Marks)

You will be given an array and you need to find the elements occurring twice and add them.

Input Format
You will be taking a number as an input stdin which tells about the length of the array. On another line, array elements should be there with single space between them.

Constraints
1 <= L <= 1000
1 <= Ai <= 1000

Output Format
You need to print the sum of the numbers occurring twice.

Sample TestCase 1
Input
12
9 8 7 7 8 6 5 4 6 5 1 2
Output
26
                                              

  Solution:
import java.io.*;
import java.util.*;
public class CandidateCode {
        public static void Count(int arr[], int n){
              Map<Integer, Integer> map = new HashMap<>();
              for(int i=0; i<n; i++){
                    if(map.containsKey(arr[i]))
                          map.put(arr[i], map.get(arr[i])+1);
                    else
                          map.put(arr[i],1);
               }
              int sum=0;
             for(Map.Entry<Integer, Integer> entry : map.entrySet()){
                   if(entry.getValue()==2)
                        sum+=entry.getKey();
                    // System.out.println(entry.getKey() + " : " + entry.getValue());
              }
         System.out.print(sum);
       }
      public static void main(String args[] ) throws Exception {
            Scanner sc = new Scanner(System.in);
            int size = sc.nextInt();
            int arr[] = new int[size];
            for(int i=0; i<arr.length; i++)
            arr[i]=sc.nextInt();
            Count(arr,size);
       }
}