0% found this document useful (0 votes)
7 views

P2

Uploaded by

varshini
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

P2

Uploaded by

varshini
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

import java.io.

*;
import java.util.*;

//productSmallestPair(sum,arr)
//eg sum=9,n=7,arr=5,2,4,3,9,7,1 o/p=2
//Implement the function to find the pair, (arr[j], arr[k]) where j!=k, Such that
arr[j] and arr[k] are the least two elements of array (arr[j] + arr[k] <= sum) and
return the product of element of this pair
// (2, 1) 2 + 1 = 3 < 9, Product of (2, 1) 2*1 = 2. Thus, output is 2
/*
public class Prepinsta1{
public static int productSmallestPair(int sum,int arr[],int n){
if(n<2){
return -1;
}
int check;
// for(int i=0;i<n;i++){//5,2,4,3,9,7,1
// for(int j=i+1;j<n;j++){
// if(arr[i]>arr[j]){
// temp=arr[i];
// arr[i]=arr[j];
// arr[j]=temp;

// }
// }
// }
Arrays.sort(arr);

check=arr[0]+arr[1];
if(check<=sum)
return arr[0]*arr[1];
else
return 0;
}
public static void main(String[] args){
int sum=9;
int arr[]={8,5,7,3,6,4};
int n=arr.length;
System.out.println(productSmallestPair(sum,arr,n));
}
}*/

/*
//to move hypen in front of the string
//eg=> String-Compare 0/p=> -StringCompare
class Prepinsta1{
public static String moveHypenFront(String str,int n){
String result ="";
for(int i=0;i<n;i++){
if(str.charAt(i)=='-')
result=str.charAt(i)+result;
else
result=result+str.charAt(i);
// return str;
}
return result;
}
public static void main(String[] args){
String str="StringCompare";
int n=str.length();
System.out.println(moveHypenFront(str,n));
}
}*/

//int numberOfCarries(int num1,int num2)


//eg num1=451 num2=349 o/p=2
//explain=>9+1=10 carry=1
// carry+4+5=10 carry=1 => 1+4+3=!10 so o/p is 2
/*public class Prepinsta1{
public static int numberOfCarries(int num1,int num2){
int carry = 0;

// Counts the number of


// carry operations
int count = 0;

// Initialize len_a and len_b


// with the sizes of strings
String a=Integer.toString(num1);
String b=Integer.toString(num2);

int len_a = a.length(),len_b = b.length();

while (len_a != 0 || len_b != 0)


{
int x = 0, y = 0;
if (len_a > 0)
{
x = a.charAt(len_a-1)-'0';
System.out.println(" "+x);

len_a--;
}
if (len_b > 0)
{
y = b.charAt(len_b - 1)-'0' ;
System.out.println(" "+y);
len_b--;
}

// Add both numbers/digits


int sum = x + y + carry;

// If sum > 0, increment count


// and set carry to 1
if (sum >= 10)
{
carry = 1;
count++;
}

// Else, set carry to 0


else
carry = 0;
}

return count;
}
public static void main(String[] args){
int num1=451;
int num2=349;

System.out.println(numberOfCarries(num1,num2));
}
}
*/

//void replaceCharacter(String str,int n,char ch1,char ch2)


// i/p => apples o/p=> paales
/*
public class Prepinsta1{
public static void replaceCharacter(String str,int n,char ch1,char ch2){
String result ="";
for(int i=0;i<n;i++){
if(str.charAt(i)==ch1){//str.char = 'a'=> 'p'
result = result+ch2;
System.out.println("result "+result);
// System.out.println(ch2);
}
else if(str.charAt(i)==ch2){//str.char ='p'=>'a'
result=result+ch1;
System.out.println("result "+result);
// System.out.println(ch1);
}
else
result=result+str.charAt(i);
}
System.out.println(result);
}
public static void main(String[] args){
String str="apples";
int n=str.length();
char ch1='a';
char ch2='p';
replaceCharacter(str,n,ch1,ch2);
}
}
*/

//int operationChoices(int c,int n,int a,int b) return =>


// (a+b) if c=1
//(a-b) if c=2
//(a*b) if c=3
//(a/b) if c=4
//eg=> i/p c=1,a=12,b=16 o/p=>12+16=28
/*
public class Prepinsta1{
public static int operationChoices(int c,int a,int b){
int result=0;
switch(c){
case 1: result=a+b;
break;
case 2: result =a-b;
break;
case 3:result=a*b;
break;
case 4:result=a/b;
}
return result;
}
public static void main(String[] args){
int c=1;
int a=12;
int b=16;
System.out.println(operationChoices(c,a,b));
}
}*/

//to find max exponents


//i/p a=7 b=12 => 7,8,9,10,11,12
//8/2=>4/2=>2/1=1 //10/2=5 //12/2=6=>6/2=>3
/*
public class Prepinsta1{
public static int findCount(int i){
int count=0;
while((i%2==0) && (i!=0)){
count++;
i=i/2;
}
return count;
}
public static int maxExponents(int a,int b){
int max=0;
int num=0;
for(int i=a;i<=b;i++){
int temp=count(i);
if(max<temp){
max = temp;
num=i;
}
}
return num;
}
public static void main(String[] args){
int a=7;
int b=12;
System.out.println(maxExponents(a,b));
}
}*/

//int calculate(int m,int n)


//sum of the numbers divisible by both 3 and 5
//eg=> m=12 n=50 => 15,20,25,30,35,40,45,50 =>15+30+45=90
/*
public class Prepinsta1{
public static int calculate(int m,int n){
for(int i=m;i<=n;i++){
int sum=0;
if(i%3==0 && i%5==0){
sum=sum+i;
}
}
}
public static void main(String[] args){
int m=12,n=50;
System.out.println(calculate(m,n))
}
}*/

//enter the size of array : 5


//enter the elements : 3 4 1 7 9
//sorted even array:3,1,9=>1,3,9 sorted odd array:4,7
//second big num =>3+4 =7
/*
import java.util.ArrayList;
import java.util.Collections;
public class Prepinsta1{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
System.out.println("enter the size of array : ");
int arrsize = scan.nextInt();
System.out.println("enter the "+arrsize+" elements : ");
System.out.println();
int arr[] =new int[arrsize];
for(int i=0;i<arrsize;i++){
arr[i]=scan.nextInt();
}

ArrayList<Integer> even = new ArrayList<Integer>();


ArrayList<Integer> odd = new ArrayList<Integer>();
for(int i=0;i<arrsize;i++){
if(i%2==0)
even.add(arr[i]);
else
odd.add(arr[i]);
}

Collections.sort(even);
Collection.sort(odd);
System.out.println("sorted even elements ");
for(e:even){
System.out.println(e+" ");//1,3,9
}
for(o:odd){
System.out.println(o+" ");
}

System.out.println("addition of two sec large num");


int evensec=even.get(even.size()-2);
int oddsec =odd.get(odd.size()-2);
int ans = evensec+oddsec;
System.out.println(ans);
}
}*/

You might also like