Sunday, 16 June 2019

Sudo Placements geeks for geeks Solution in Java

FOR THE SOLUTION YOU MAY GET TLE- error(Exceeding timelimit error) it's just because Java is slower than cpp/c

Ques-

If a=1, b=2, c=3,....z=26. Given a string, find all possible codes that string can generate. Give a count as well as print the strings. 

For example: 
Input: "1123". You need to general all valid alphabet codes from this string.

Output List 
aabc //a = 1, a = 1, b = 2, c = 3 
kbc // since k is 11, b = 2, c= 3 
alc // a = 1, l = 12, c = 3 
aaw // a= 1, a =1, w= 23 
kw // k = 11, w = 23
Solution -
public class JavaApplication3 {

    /**
     * @param args the command line arguments
     */
    
    public static Set<String> decode(String prefix, String code) {
Set<String> set = new HashSet<String>();
if (code.length() == 0) 
                {
set.add(prefix);
return set;
}

if (code.charAt(0) == '0')
return set;

set.addAll(decode(prefix + (char) (code.charAt(0) - '1' + 'a'),
code.substring(1)));
if (code.length() >= 2 && code.charAt(0) == '1') {
set.addAll(decode(
prefix + (char) (10 + code.charAt(1) - '1' + 'a'),
code.substring(2)));
}
if (code.length() >= 2 && code.charAt(0) == '2'
&& code.charAt(1) <= '6') {
set.addAll(decode(
prefix + (char) (20 + code.charAt(1) - '1' + 'a'),
code.substring(2)));
}
return set;
}
    
    public static void main(String[] args) {
        Set s = decode("", "9999");
        System.out.println(s);
    }    
}

Immediate Smaller Element

Given an integer array of size N. For each element in the array, check whether there exist a smaller element on the next immediate position of the array. If such an element exists, print that element. If there is no smaller element on the immediate next to the element then print -1.

Input:
The first line of input contains an integer T denoting the number of test cases. T testcases follow. Each testcase contains 2 lines of input:

The first line contains an integer N, where N is the size of array.
The second line contains N integers(elements of the array) sperated with spaces.

Output:

For each test case, print the next immediate smaller elements for each element in the array.

Constraints:

1 ≤ T ≤ 200
1 ≤ N ≤ 107
1 ≤ arr[i] ≤ 1000

Example:

Input
2
5
4 2 1 5 3
6
5 6 2 3 1 7

Output
2 1 -1 3 -1
-1 2 -1 1 -1 -1



Explanation:
Testcase 1: Array elements are 4, 2, 1, 5, 3. Immediate smaller of 2 is immediate smaller of 4, 1 is immediate smaller of 2,no immediate smaller of 1, 3 is immediate smaller of 5, and no immediate smaller for last element exists. So ouput is : 2 1 -1 3 -1.



CODE- SOLUTION

import java.util.*;

import java.lang.*;

import java.io.*;



class GFG {
 public static void main (String[] args) {
  //code
  Scanner kb=new Scanner(System.in);
  int times=kb.nextInt();
  for(int i=0;i<times;i++)//testcases
  {

      int size_arr=kb.nextInt();

      int arr[]=new int[size_arr];

      for(int j=0;j<size_arr;j++)

      {
          arr[j]=kb.nextInt();
      }

   

      try{

      for(int j=0;j<size_arr;j++)
      {
          if(arr[j]>arr[j+1])
          {
              System.out.println(arr[j+1]);
          }
          else
          {
              System.out.println("-1");
          }

       
      }
      }

      catch(Exception e)

      {
          System.out.println("-1");
      }


  }//testcases

 }


}


Rotating an Array
Given an array of N size. The task is to rotate array by d elements where d is less than or equal to N.

Input:
The first line of input contains a single integer T denoting the number of test cases. Then T test cases follow. Each test case consist of three lines. The first line of each test case consists of an integer N, where N is the size of array.
The second line of each test case contains N space separated integers denoting array elements. The third line of each test case contains "d" .

Output:
Corresponding to each test case, in a new line, print the modified array.

Constraints:
1 ≤ T ≤ 200
1 ≤ N ≤ 200
1 ≤ A[i] ≤ 1000

Example:
Input
1
5
1 2 3 4 5
2

Output
3 4 5 1 2

Solution-

/*package whatever //do not write package name here */

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

class GFG {
public static void main (String[] args) {
//code
Scanner kb=new Scanner(System.in);
int times=kb.nextInt();
for(int i=0;i<times;i++)
{
    int sizearr=kb.nextInt();
    int arr[]=new int[sizearr];
    for(int j=0;j<sizearr;j++)
    {
        arr[j]=kb.nextInt();
      
    }
    int by_ro = kb.nextInt();
    for(int j=by_ro;j<sizearr;j++)
    {
        System.out.print(arr[j]);
    }
    for(int j=0;j<by_ro;j++)
    {
        System.out.print(arr[j]);
    }
    
}//times

}

}

Leaders in an array


Given an array of positive integers. Your task is to find the leaders in the array.
Note: An element of array is leader if it is greater than or equal to all the elements to its right side. Also, the rightmost element is always a leader. 

Input:
The first line of input contains an integer T denoting the number of test cases. The description of T test cases follows.
The first line of each test case contains a single integer N denoting the size of array.
The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of the array.

Output:
Print all the leaders.

Constraints:
1 <= T <= 100
1 <= N <= 107
0 <= Ai <= 107

Example:
Input:
3
6
16 17 4 3 5 2
5
1 2 3 4 0
5
7 4 5 7 3
Output:
17 5 2
4 0
7 7 3

Explanation:
Testcase 3: All elements on the right of 7 (at index 0) are smaller than or equal to 7. Also, all the elements of right side of 7 (at index 3) are smaller than 7. And, the last element 3 is itself a leader since no elements are on its right.

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

class GFG {
    
public static void main (String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine().trim()); //Inputting the testcases
while(t-->0){
    int n = Integer.parseInt(br.readLine().trim());
    int arr[] = new int[n];
    String inputLine[] = br.readLine().trim().split(" ");
    for(int i=0; i<n; i++){
        arr[i] = Integer.parseInt(inputLine[i]);
    }
    int maxEle = arr[n-1];
    StringBuffer str = new StringBuffer();
    ArrayList<Integer> res = new ArrayList<Integer>();
    for(int i=n-1; i>=0; i--) {
        if(arr[i] >= maxEle){
            maxEle = arr[i];
            res.add(maxEle);
        }
    }
    for(int i=res.size()-1; i>=0; i--){
        str.append(res.get(i)+" ");
    }
    System.out.println(str);
}
}

}



Print an array in Pendulum Arrangement

Write a program to input a list of n integers in an array and arrange them in a way similar to the to-and-fro movement of a Pendulum.

The minimum element out of the list of integers, must come in center position of array. If there are even elements, then minimum element should be moved to (n-1)/2 index (considering that indexes start from 0)
The next number (next to minimum) in the ascending order, goes to the right, the next to next number goes to the left of minimum number and it continues like a Pendulum.
Input:
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case contains an integer n denoting the size of the array. Then next line contains N space separated integers forming the array.

Output:
Output the array in Pendulum Arrangement.

Constraints:
1<=T<=500
1<=N<=100
1<=a[i]<=1000

Example:
Input:
2
5
1 3 2 5 4
5
11 12 31 14 5

Output:
5 3 1 2 4
31 12 5 11 14

Solution-

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Scanner;

public class test {
    public static void main(String[] args) throws IOException {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int times=Integer.parseInt(br.readLine());
        while(times-->0)
        {
           int size=Integer.parseInt(br.readLine());
           int arr[]=new int[size];
           String line=br.readLine();
           String [] strs=line.trim().split("\\s+");

           for(int i=0;i<size;i++)
               arr[i]=Integer.parseInt(strs[i]);

            Arrays.sort(arr);
            StringBuffer sb=new StringBuffer();
            int mid_point=(size-1)/2;
            sb.append(arr[0]+" ");
            int j=1;
            for(int i=0;i<size/2;i++)
            {
                sb.append(arr[j++]+" ");
                sb.insert(0,arr[j++]+" ");

            }
            if(size%2==0)
                sb.append(arr[arr.length-1]+" ");

            System.out.println(sb);


        }//times

    }
}




SP BIT MAGIC JAVA SOLUTIONS

Binary representation

Write a program to print Binary representation of a given number N.


Input:
The first line of input contains an integer T, denoting the number of test cases. Each test case contains an integer N.

Output:
For each test case, print the binary representation of the number N in 14 bits.

Constraints:
1 ≤ T ≤ 100
1 ≤ N ≤ 5000

Example:
Input:
2
2
5

Output:
00000000000010
00000000000101

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

class binary {
    public static void main (String[] args) {
        //code
        Scanner kb=new Scanner(System.in);
        int times=kb.nextInt();
        int t=0;
        int arr[]=new int[14];
        for(int i=0;i<times;i++)
        {
            int num=kb.nextInt();

            for(int j=0;j<14;j++)
            {
                //System.out.println("j value "+ j);
                arr[j]=num%2;
                //System.out.println(arr[j]);
                num=num/2;
                //System.out.println(num);
            }



            //System.out.println("result");
            int j=13;
            while(j>=0)
            {
                System.out.print(arr[j]);
                j=j-1;
            }
            System.out.println();



        }//times
    }

}

GPT4ALL - A new LLaMa (Large Language Model)

posted 29th March, 2023 - 11:50, GPT4ALL launched 1 hr ago  What if we use AI generated prompt and response to train another AI - Exactly th...