Posts

Search

Volante coding question

 Write a program to print the following pattern:-

Input:-

9 (always odd number)

Output:-

bbbb*bbbb

bbb***bbb

bb*****bb

b*******b

*********

b*******b

bb*****bb

bbb***bbb

bbbb*bbbb

Code:-

import java.util.*;

public class Main

{

public static void main(String[] args) {

    Scanner sc= new Scanner(System.in);

    int n=sc.nextInt();

    int len=n/2;

    String str="";

    for(int i=0;i<len;i++){

        str+="b";

    }

    int temp=len-1;

    String str1=str;

    int temp1=0;

    int len1=n/2;

    for(int i=0;i<n;i++){

        if(i<len){

        System.out.println(str+"*"+str1);

        str=str.substring(0,temp)+'*'+str.substring(temp+1);

        temp--;

        str1=str1.substring(0,i)+'*'+str1.substring(i+1);

        }

        if(i==len)

        {

            System.out.println(str+"*"+str1);

            str=str.substring(0,temp1)+'b'+str.substring(temp1+1);

        temp1++;

        str1=str1.substring(0,len1-1)+'b'+str1.substring(len1);

        len1--;

        }

        if(i-1>len){

        System.out.println(str+"*"+str1);

        str=str.substring(0,temp1)+'b'+str.substring(temp1+1);

        temp1++;

        str1=str1.substring(0,len1-1)+'b'+str1.substring(len1);

        len1--;

        }

        if(i==n-1)

        System.out.println(str+"*"+str1);

    }

}

}




Cerner Coding Question

 Problem :-

You are given an array of integers. Count the numbers of ways in which the sum of 4 elements in this array results in zero.

Input:-

Your program should read lines from standard input. Each line consist of comma separated positive and negative integers.

Output:-

Print out the count of the different number of ways that 4 elements sum to zero.

Test 1:-

2,3,1,0,-4,-1

Expected Output:-

2

Test 2:-

0,-1,3,-2

Expected Output:-

1

Solution:-

import java.util.*;
public class Main
{
    int len;
    public  int sum(int[] nums, int target) {
            len = nums.length;
            Arrays.sort(nums);
            List<List<Integer>> list1=helper(nums, target, 40);
            return list1.size();
        }
       private ArrayList<List<Integer>> helper(int[] nums, int target, int k, int index) {
            ArrayList<List<Integer>> res = new ArrayList<List<Integer>>();
            if(index >= len) {
                return res;
            }
            if(k == 2) {
                int i = index, j = len - 1;
                while(i < j) {
                    
                    if(target - nums[i] == nums[j]) {
                        List<Integer> temp = new ArrayList<>();
                        temp.add(nums[i]);
                        temp.add(target-nums[i]);
                        res.add(temp);
                        
                        while(i<j && nums[i]==nums[i+1]) i++;
                        while(i<j && nums[j-1]==nums[j]) j--;
                        i++;
                        j--;
                   
                    } else if (target - nums[i] > nums[j]) {
                        i++;
                    
                    } else {
                        j--;
                    }
                }
            } else{
                for (int i = index; i < len - k + 1; i++) {
                    
                    ArrayList<List<Integer>> temp = helper(nums, target - nums[i], k-1, i+1);
                    if(temp != null){
                        
                        for (List<Integer> t : temp) {
                            t.add(0, nums[i]);
                        }
                        res.addAll(temp);
                    }
                    while (i < len-1 && nums[i] == nums[i+1]) {
                        
                        i++;
                    }
                }
            }
            return res;
        }
    public static void main(String[] args) {
        Scanner sc= new Scanner(System.in);
        String s= sc.nextLine();
        String ar[]=s.split(",");
        int ar1[]=new int[ar.length];
        int i1=0;
        for(String a:ar)
        ar1[i1++]=Integer.parseInt(a);
          int n=ar1.length;
        int count=0;
        Main m=new Main();
        System.out.print(m.sum(ar1,0));
    }
}

Amdocs Coding Question 2

 Given a range of integers [X,Y] where X and Y both are included. Write a program to find the composite numbers and the count C of numbers in that sub-range. A composite number is a number (except 1) that i snot a prime number.

Constraints:-

1. X, Y>1

2. X<Y

Input Format:-

 A single line of input contains X and Y separated by a single white space .

Output Format:-

Multiple lines of output contain three integers each M,N and C where [M,N] are the numbers and C is the count of numbers in the sub-range. Integers in each output line are separated by a single white space.

Solution:-

import java.util.*;

public class Main

{

    static boolean isPrime(int n) { 

     if (n%2==0) return false; 

    for(int i=3;i<=Math.sqrt(n);i+=2) { 

        if(n%i==0) 

            return false; 

    } 

    return true; 

static void sieveAlg1(int x,int y)

{

    int ar[]=new int[50];

    int co=0,z=0;

    for(int i=x;i<=y;i++)

    {

        boolean c=isPrime(i);

        if(c==false)

        {

            co++;

            ar[z++]=i;

            continue;

        }

        if(co>=7)

        {

            System.out.println(ar[0]+" "+ar[co-1]+" "+co);

            z=0;

            co=0;

        }

        else if(c==true)

        {

            co=0;

            z=0;

        }

   }

    

}

public static void main(String[] args) {

    Scanner sc =new Scanner(System.in);

    int testStart=sc.nextInt();

    int testStop=sc.nextInt();

    sieveAlg1(testStart,testStop);     

}

}


Amdocs Coding Question

 Write a program to perform the following operations on a given integer N.

1. Convert all the digits of  N to their character representation (in upper case only) as 1=A,2=B,3=C and so on. If there are any '0' in N, then remove it.

2. Add all digits of N to obtain sum S.

3. If S is odd, print only those character derived by converting odd digits of N. If S is even, print only those characters derived by converting even digits of N. Alphabetical order must be followed in either case.

Constraints:-

N>0

Input Format:-

The input contains an integer N.

Output Format:-

The first line of output contains the characters in alphabetically order.

The second line of output contains the sum S.

The third line of output contains the characters obtained by converting either even or odd numbers, in alphabetical order. In case of absence of any even/odd number in the input integer, the third line of output should not be printed.

Solution:-

import java.util.*;

public class Main

{

public static void main(String[] args) {

    Scanner sc= new Scanner(System.in);

    String a= sc.next();

    char ar[]={'A','B','C','D','E','F','G','H','I','J'};

    int num[]=new int[a.length()];

    int c=0;

    for(int i=0;i<a.length();i++)

    {

        if(a.charAt(i)!='0')

        {

            num[c]=a.charAt(i);

            num[c]=num[c]-48;

            c++;

           // System.out.print(num[c-1]+" ");

        }

    }

    int newn[]=new int[c];

    int newe[]=new int[c];

    int newo[]=new int[c];

    

    int sum=0,e=0,o=0,z=0,y=0;

    for(int i=0;i<c;i++)

    {

        newn[i]=num[i];

        sum+=newn[i];

        if(newn[i]%2==0)

        {

            newe[z++]=newn[i];

            e++;

        }

        else if(newn[i]%2==1)

        {

            newo[y++]=newn[i];

            o++;

        }

    }

    //System.out.print(e+" "+o);

    int newe1[]=new int[e];

    int newo1[]=new int[o];

    for(int i=0;i<e;i++)

    {

        newe1[i]=newe[i];

       // System.out.print(newe1[i]+" ");

        

    }

    for(int i=0;i<o;i++)

    {

        newo1[i]=newo[i];

        // System.out.print(newo1[i]+" ");

       

    }

    

    Arrays.sort(newn);

    for(int i=0;i<c;i++)

    {

        System.out.print(ar[newn[i]-1]);

    }

System.out.println();

System.out.println(sum);

Arrays.sort(newe1);

Arrays.sort(newo1);

if(sum%2==0)

{

    if(e>0)

    {

        for(int i=0;i<e;i++)

        {

                 System.out.print(ar[newe1[i]-1]);

        }

        }

        

}

else{

    if(o>0)

    {

        for(int i=0;i<o;i++)

        {

                 System.out.print(ar[newo1[i]-1]);

        }

        }     

}

}

}

  

Electronics Shop

Monica wants to buy a keyboard and a USB drive from her favorite electronics store. The store has several models of each. Monica wants to spend as much as possible for the  items, given her budget.

Given the price lists for the store's keyboards and USB drives, and Monica's budget, find and print the amount of money Monica will spend. If she doesn't have enough money to both a keyboard and a USB drive, print -1 instead. She will buy only the two required items.

For example, suppose she has  to spend. Three types of keyboards cost . Two USB drives cost . She could purchase a , or a . She chooses the latter. She can't buy more than  items so she can't spend exactly .

Function Description

Complete the getMoneySpent function in the editor below. It should return the maximum total price for the two items within Monica's budget, or  if she cannot afford both items.

getMoneySpent has the following parameter(s):

  • keyboards: an array of integers representing keyboard prices
  • drives: an array of integers representing drive prices
  • b: the units of currency in Monica's budget

Input Format

The first line contains three space-separated integers , and , her budget, the number of keyboard models and the number of USB drive models.
The second line contains  space-separated integers , the prices of each keyboard model.
The third line contains  space-separated integers , the prices of the USB drives.

Constraints

  • The price of each item is in the inclusive range .

Output Format

Print a single integer denoting the amount of money Monica will spend. If she doesn't have enough money to buy one keyboard and one USB drive, print -1 instead.

Sample Input 0

10 2 3
3 1
5 2 8

Sample Output 0

9

Explanation 0

She can buy the  keyboard and the  USB drive for a total cost of .

Sample Input 1

5 1 1
4
5

Sample Output 1

-1

Explanation 1

There is no way to buy one keyboard and one USB drive because , so we print .

Solution:-

import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
import java.util.regex.*;

public class Solution {

    /*
     * Complete the getMoneySpent function below.
     */
    static int getMoneySpent(int[] keyboards, int[] drives, int b) {
        /*
         * Write your code here.
         */
         int max=Integer.MIN_VALUE;
         for(int i=0;i<keyboards.length;i++)
         for(int j=0;j<drives.length;j++)
         if(keyboards[i]+drives[j]<=b)
         {
             if(keyboards[i]+drives[j]>max)
             max=keyboards[i]+drives[j];
         }
         if(max==Integer.MIN_VALUE)
         return -1;
         return max;

    }

    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) throws IOException {
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        String[] bnm = scanner.nextLine().split(" ");
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])*");

        int b = Integer.parseInt(bnm[0]);

        int n = Integer.parseInt(bnm[1]);

        int m = Integer.parseInt(bnm[2]);

        int[] keyboards = new int[n];

        String[] keyboardsItems = scanner.nextLine().split(" ");
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])*");

        for (int keyboardsItr = 0; keyboardsItr < n; keyboardsItr++) {
            int keyboardsItem = Integer.parseInt(keyboardsItems[keyboardsItr]);
            keyboards[keyboardsItr] = keyboardsItem;
        }

        int[] drives = new int[m];

        String[] drivesItems = scanner.nextLine().split(" ");
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])*");

        for (int drivesItr = 0; drivesItr < m; drivesItr++) {
            int drivesItem = Integer.parseInt(drivesItems[drivesItr]);
            drives[drivesItr] = drivesItem;
        }

        /*
         * The maximum amount of money she can spend on a keyboard and USB drive, or -1 if she can't purchase both items
         */

        int moneySpent = getMoneySpent(keyboards, drives, b);

        bufferedWriter.write(String.valueOf(moneySpent));
        bufferedWriter.newLine();

        bufferedWriter.close();

        scanner.close();
    }
}