Code For Alls..!

get the code!!

Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Friday 15 September 2017

                                                                          Average Speed
Average Speed


A single line L with a set of space separated values indicating distance travelled and time taken is passed as the input. The program must calculate the average speed S (with precision upto 2 decimal places) and print S as the output.
Note: The distance and time taken will follow the format DISTANCE@TIMETAKEN. DISTANCE will be in kilometers and TIMETAKEN will be in hours.
Input Format:
The first line contains L.
Output Format:
The first line contains the average speed S.
Boundary Conditions:
Length of L will be from 3 to 100.
Example Input/Output 1:
Input:
60@2 120@3
Output:
36.00 kmph
Explanation:
Total distance = 60+120 = 180 km.
Total time taken = 2+3 = 5 hours.
Hence average speed = 180/5 = 36.00 kmph
Java Program:
import java.util.*;
public class Hello {

    public static void main(String[] args) {
//Your Code Here
Scanner sc=new Scanner(System.in);
String st;
int d=0,s=0;
st=sc.nextLine();
String[] str=st.split(" ");
for(String a:str){
int j=a.indexOf('@');
   d=d+Integer.parseInt(a.substring(0,j)); 
   s=s+Integer.parseInt(a.substring(j+1));
}
System.out.printf("%.2f kmph",((float)d/(float)s));
}
}

Sunday 10 September 2017

                                          The Least Used(LRU) cache algorithm


The least recently used(lru) algorithm exists the elements from the cache(when it's full) that was least recently used.After an element is requested from the cache, it should beadded to the cache (if not already there) and considered the most recently used element in the cache.

Test case 1:
3
16
7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0
output:
11

Test case 2:
2
9
2 3 1 3 2 1 4 3 2

Java Program:
package wipro;
import java.util.*;
public class lru {
public static void main(String[] args)
{
Scanner sc =new Scanner(System.in);
int ps=sc.nextInt();     //read the cache size
int as=sc.nextInt();      //size of the page array
int[] a=new int[as];
for(int i=0;i<as;i++)     //get the page elemnts
a[i]=sc.nextInt();
System.out.println(lrucountmiss(a,ps));

}
public static int getMinValue(int[] array) {
   
int j=0, minValue = array[0];
   for (int i = 1; i < array.length; i++) {
       if (array[i] < minValue) {
           j=i;
       }
   }
   return j;
}
public static int Search(int[] array,int e) {
   for (int i = 1; i < array.length; i++) 
       if (array[i] == e) 
           return i;
       return -1;
}
static int lrucountmiss(int a[],int ps)
{
int[] page=new int[ps];
int[] cach=new int[ps];
int c=0,j=0,k=0;
Arrays.fill(page, -1);
for(int i=0;i<a.length;i++)
{
int t=Search(page,a[i]);
System.out.println(t);
if(t==-1)
{
if(j<ps)
{
page[j]=a[i];
   cach[j]=k++;
   j++;
   }
else
{
int min=getMinValue(cach);
page[min]=a[i];
cach[min]=k++;
}
c++;
}
else
cach[t]=k++;
}
return c;
}

}



Thursday 31 August 2017



An array of N integers is passed as the input to the  program and the program must print the array after reversing it.

Complete the method reverse so that the program can execute successfully.

Input Format:
The first line contains N.
The second line contains N integer values each separated by a space.

Output Format:
The first line contains reversed values of N integers separated by a space.

Boundary Conditions:
1 <= N <= 1000

Example Input/Output 1:
Input:
5
10 22 33 45 600

Output:
600 45 33 22 10

Java :


import java.util.Scanner;

public class Hello {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();import java.util.Scanner;

public class Hello {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();import java.util.Scanner;

public class Hello {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int arr[] = new int[N];
        for(int index=0; index < N; index++){
            arr[index] = sc.nextInt();
        }
       
        arr = reverse(arr);
        int arr[] = new int[N];
        for(int index=0; index < N; index++){
            arr[index] = sc.nextInt();
        }
       
        arr = reverse(arr);

        int arr[] = new int[N];
        for(int index=0; index < N; index++){
            arr[index] = sc.nextInt();
        }
       
        arr = reverse(arr);

        //Printing the output
         for(int index=0; index < N; index++){
             System.out.print(arr[index]+" ");
        }
    }
 static int[] reverse(int[] arr) {
int i=0,j=arr.length-1,t;
while(i<j)
{
   t=arr[i];
   arr[i]=arr[j];
   arr[j]=t;
   i++;
   j--;
}
return arr;
}
} //End of class Hello

Saturday 22 July 2017

Given integers, count the number of pairs of integers whose difference is .
Input Format
The first line contains and .
The second line contains numbers of the set. All the numbers are unique.
Constraints
  • Each integer will be greater than and at least smaller than .
Output Format
An integer that tells the number of pairs of integers whose difference is .
Sample Input
5 2  
1 5 3 4 2  
Sample Output
3
Explanation
There are 3 pairs of integers in the set with a difference of 2.

Java code:
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {
    static int pairs(int[] a,int k) {
      /* Complete this function */
        Arrays.sort(a);
        int i=a.length-1;
        int j=i-1,c=0;
        while(j>=0)
        {
            if(a[i]-a[j]<k)
                j--;
            else if(a[i]-a[j]>k)
                i--;
            else if(a[i]-a[j]==k)
            {
                c++;
                j--;
            }
               
        }
        return c;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int res;
       
        String n = in.nextLine();
        String[] n_split = n.split(" ");
       
        int _a_size = Integer.parseInt(n_split[0]);
        int _k = Integer.parseInt(n_split[1]);
       
        int[] _a = new int[_a_size];
        int _a_item;
        String next = in.nextLine();
        String[] next_split = next.split(" ");
       
        for(int _a_i = 0; _a_i < _a_size; _a_i++) {
            _a_item = Integer.parseInt(next_split[_a_i]);
            _a[_a_i] = _a_item;
        }
       
        res = pairs(_a,_k);
        System.out.println(res);
    }
}
 
Watson gives Sherlock an array of length . Then he asks him to determine if there exists an element in the array such that the sum of the elements on its left is equal to the sum of the elements on its right. If there are no elements to the left/right, then the sum is considered to be zero.
Formally, find an , such that, .
Input Format
The first line contains , the number of test cases. For each test case, the first line contains , the number of elements in the array . The second line for each test case contains space-separated integers, denoting the array .
Constraints



Output Format
For each test case print YES if there exists an element in the array, such that the sum of the elements on its left is equal to the sum of the elements on its right; otherwise print NO.
Sample Input 0
2
3
1 2 3
4
1 2 3 3
Sample Output 0
NO
YES
Explanation 0
For the first test case, no such index exists.
For the second test case, , therefore index satisfies the given conditions.

Java Code:
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    static String solve(int[] a){
       if(a.length==1)
           return "YES" ;
        if(a.length==2)
            return "NO" ;
        int r=0,l=0;
        for(int i=0;i<a.length;i++)
           r+=a[i];
       r=r-a[0];
        for(int i=1;i<a.length;i++)
        {
            l=l+a[i-1];
            r=r-a[i];
            if(l==r)
            return "YES" ;
        }
    return "NO" ;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int T = in.nextInt();
        for(int a0 = 0; a0 < T; a0++){
            int n = in.nextInt();
            int[] a = new int[n];
            for(int a_i=0; a_i < n; a_i++){
                a[a_i] = in.nextInt();
            }
            String result = solve(a);
            System.out.println(result);
        }
    }
}