0% found this document useful (0 votes)
8 views5 pages

P1

Uploaded by

varshini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views5 pages

P1

Uploaded by

varshini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

import java.io.

*;
import java.util.*;

/*public class Prepinsta{


public static
public static void main(String[] args){
System.out.println()
}
}*/

//rat sum eg=> r=7 unit=2 n=8 arr=2 8 3 5 7 4 1 2 o/p=>2+8+3+5=18 so output is 4


/*public class Prepinsta{
public static int findHouse(int r,int unit,int n,int arr[]){
if(arr==null){
return -1;
}

int sum=0;
int target=r*unit;
int count=0;

for(int i=0;i<n;i++){
sum=sum+arr[i];
count++;
if(sum>=target)
break;
}

if(sum<target)
return 0;

return count;
}
public static void main(String[] args){
int n=8;
int unit=2;
int r=7;
int arr[]={2,8,3,5,7,4,1,2};
System.out.println(findHouse(r,unit,n,arr));
}
}*/

//password checker
//int checkPassword(char str[],int n) // atleast 4 chars // 1 numeric digit // 1
capital // no space or slash(/) //starting no num
//ed=> i/p=>aZ1_67 o/p=1 => if valid return 1 and not valid return 0
/*public class Prepinsta{ //aZ1_67 6
public static String checkPassword(String str,int n){
if(n<4)
return "not valid";
if(str.charAt(0)>='0'&& str.charAt(0)<='9')
return "not valid";
int num=0;
int cap=0;
for(int i=0;i<n;i++){
if(str.charAt(i) == ' ' || str.charAt(i) == '/')
return "not valid";
if(str.charAt(i) >='A' && str.charAt(i) <='Z')
cap++;
if(str.charAt(i)>='0'&& str.charAt(i)<='9')
num++;
}
if(cap>0 && num >0)
return "valid password";
else
return "not valid";
}
public static void main(String[] args){
String str="aZ1_67";
int n=str.length();
System.out.println(checkPassword(str,n));
}
}
*/

//int findcount(int arr[],int length,int num,int diff)


//eg=> 12,3,14,56,77,13 num=13 diff=2 o/p=>3
/*public class Prepinsta{
public static int findcount(int arr[],int length,int num,int diff){
int count=0;
for(int i=0;i<length;i++){
if(Math.abs(num-arr[i])<=diff){
count++;
}
}
return count>0?count:-1;
}
public static void main(String[] args){
int arr[] ={12,3,14,56,77,13};
int length=arr.length;
int num=13;
int diff=2;
System.out.println(findcount(arr,length,num,diff));
}
}*/

//int differenceofSum(int n,int m)


//eg i/p=> n=4 m=20 o/p=>90 =>(%by 4 - not% by 4)
/*public class Prepinsta {
public static int differenceofSum(int n,int m){
int divisible=0,notdivisible=0;
for(int i=0;i<=m;i++){
if(i%4==0)
divisible+=i;
if(i%4!=0)
notdivisible+=i;
}
return Math.abs(notdivisible-divisible);
}
public static void main(String[] args){
int n=4;
int m=20;
System.out.println(differenceofSum(n,m));
}
}*/
//largeSmallSum
//sum of second largest element from the even positions and second smallest from
the odd position of given arr
//arr=3,2,1,7,5,4 o/p=7
// 0,1,2,3,4,5 even=3,1,5 odd=2,7,4 =>3+4=7
/*
public class Prepinsta{
public static int largeSmallSum(int arr[],int n){
if(n<=3)
return 0;
ArrayList<Integer> even=new ArrayList<Integer>();
ArrayList<Integer> odd=new ArrayList<Integer>();
// even.add(arr[0]);
for(int i=0;i<n;i++){
if(i%2==0)
even.add(arr[i]);
else
odd.add(arr[i]);
}
Collections.sort(even);//1,3,5
Collections.sort(odd);//2,4,7
System.out.println(even);
System.out.println(odd);
int ans1=even.get(even.size()-2);
int ans2=odd.get(odd.size()-2);
System.out.println(ans1);
System.out.println(ans2);
return ans1+ans2;
}
public static void main(String[] args){
int arr[] ={3,2,1,7,5,4};
int n=arr.length;
System.out.println(largeSmallSum(arr,n));
}
}
*/

//to print table of a number to 10


//eg=> 5 o/p=>5,10,15,20,25,30,35,40,45,50
/*
public class Prepinsta{
public static void main(String[] args){
Scanner scan= new Scanner(System.in);
System.out.println("enter the number : ");
int num = scan.nextInt();
int value=0,sum=0;
for(int i=1;i<=10;i++){
value=num*i;
System.out.print(value+" ");
sum=value+sum;
}
System.out.println();
System.out.println(sum);
}
}
*/

//to print palindrom btw 2 numbers


//i/p=> 10,80 o/p=> 11,22,33,44,55,66,77
/*
public class Prepinsta{

public static int checkPalindrom(int no){


int remainder=0;
int divisor=no;//11
while(divisor!=0){
int r=divisor%10;
remainder=(remainder*10)+r;
divisor=divisor/10;
}
return rem;
}

public static void main(String[] args){


Scanner scan = new Scanner(System.in);
System.out.println("enter the lower limit : ");
int lowlimit=scan.nextInt();
System.out.println("enter the upper limit : ");
int upplimit=scan.nextInt();
for(int i=lowlimit;i<upplimit;i++){
if(i==checkPalindrom(i)){
System.out.println(i);
}
}
}
}
*/

//to calculate sum of diff


//x1,y1=(1,1) x2,y2=(2,4) x3,y3=(3,6)
//sqrt((x2-x1)^2 + (y2-y1)^2)
//sqrt((x3-x2)^2 + (y3-y2)^2)
//sqrt((x3-x1)^2 + (y3-y1)^2)
/*
public class Prepinsta{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
float x1=scan.nextFloat();
float y1=scan.nextFloat();
float x2=scan.nextFloat();
float y2=scan.nextFloat();
float x3=scan.nextFloat();
float y3=scan.nextFloat();
float firstdiff = (float)Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
float seconddiff = (float)Math.sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2));
float thirddiff = (float)Math.sqrt((x3-x1)*(x3-x1)+(y3-y1)*(y3-y1));
System.out.println(firstdiff+seconddiff+thirddiff);
}
}
*/

//find the maximum value and its index in the array


//void maxInArray(int arr[],int arrlength)
/*
public class Prepinsta{
public static void maxInArray(int arr[],int n){
int max=arr[0];
int index=0;
for(int i=0;i<n;i++){
if(arr[i]>max){
max=arr[i];
index=i;
}
}
System.out.println(max);
System.out.println(index);
}
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
System.out.println("enter the size of the array: ");
int n=scan.nextInt();
int arr[] = new int[n];
System.out.println("enter the elements: ");
for(int i=0;i<n;i++){
arr[i]=scan.nextInt();

}
maxInArray(arr,n);
}
}
*/

You might also like