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

Java

Uploaded by

Macharla Dhivija
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 views

Java

Uploaded by

Macharla Dhivija
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/ 6

count of fav char in string

-----------------------------
import java.util.*;
class HelloWorld {
public static void main(String[] args) {
Scanner S=new Scanner(System.in);
String s=S.nextLine();
int n=S.nextInt();
char c=S.next().charAt(0);
int count=0;
for(int i=0;i<n;i++){
if(s.charAt(i)==c){
count++;
}
}
System.out.println(count);
}
}
===================================================================================
==========
rebounced height
------------------
import java.util.*;
class HelloWorld {
public static void main(String[] args) {
Scanner S=new Scanner(System.in);
int H=S.nextInt();
int v=S.nextInt();
int vn=S.nextInt();
int en=v/vn;
int Hn=(int) (H*Math.pow(en,2));
System.out.println(Hn);
}
}
===================================================================================
====================
evenodd string
-------------------
import java.util.*;
class HelloWorld {
public static void main(String[] args) {
Scanner S=new Scanner(System.in);
int n=S.nextInt();
int[] a=new int[n];
for(int i=0;i<n;i++){
a[i]=S.nextInt();
}
String res="";
for(int i=0;i<n;i++){
if(a[i]%2==0){
res=res+"even";
}
else{
res=res+"odd";
}
}
System.out.println(res);
}
}
===================================================================================
=================
difference of sum and xor
-------------------------
import java.util.*;
class HelloWorld {
public static void main(String[] args) {
Scanner S=new Scanner(System.in);
int n=S.nextInt();
int[] a=new int[n];
for(int i=0;i<n;i++){
a[i]=S.nextInt();
}
int sum=0;
int xor=0;
for(int i=0;i<n;i++){
if(i%2!=0){
sum=sum+a[i];
}
else{
xor=xor^a[i];
}
}
System.out.println(sum-xor);
}
}
===================================================================================
===============
target sum
---------------------
import java.util.*;
class HelloWorld {
public static void main(String[] args) {
Scanner S=new Scanner(System.in);
int n=S.nextInt();
int[] a=new int[n];
for(int i=0;i<n;i++){
a[i]=S.nextInt();
}
int ans[]=new int[2];
int maxp=0;
for(int i=0;i<n;i++){
for(int j=0;j<n-1;j++)
{
if(a[i]+a[j]==18){
int t=a[i]*a[j];
if(t>maxp){
maxp=t;
ans[0]=a[i];
ans[1]=a[j];
}
}
}
}
System.out.println(Arrays.toString(ans));
}
}
import java.util.*;
class HelloWorld {
public static void main(String[] args) {
Scanner S = new Scanner(System.in);

// Input number of elements


int n = S.nextInt();
int[] a = new int[n];

// Input array elements


for (int i = 0; i < n; i++) {
a[i] = S.nextInt();
}

int[] ans = new int[2];


int maxp = 0;
Map<Integer, Integer> map = new HashMap<>();

// Single loop to find pairs that sum to 18 with the maximum product
for (int i = 0; i < n; i++) {
int complement = 18 - a[i];
if (map.containsKey(complement)) {
int product = a[i] * complement;
if (product > maxp) {
maxp = product;
ans[0] = a[i];
ans[1] = complement;
}
}
map.put(a[i], i); // Add current element to the map
}

// Output result
if (maxp > 0) {
System.out.println(Arrays.toString(ans));
} else {
System.out.println("No pair found with sum 18.");
}

S.close(); // Close scanner to avoid resource leaks


}
}

===================================================================================
=========================
fibinoce
--------
rt java.util.*;
class HelloWorld {
public static void main(String[] args) {
Scanner S=new Scanner(System.in);
int n=S.nextInt();
if(n==0|| n==1){
System.out.println("1");
}
int[] f =new int[n+1];
f[0]=1;
f[1]=1;
for(int i=2;i<=n;i++){
f[i]=(f[i-1]*(i-1)+(i-2)*(i-2))%47;
}
System.out.println(f[n]);
}
}
===================================================================================
============
even sum
--------
import java.util.*;
class HelloWorld {
public static void main(String[] args) {
Scanner S=new Scanner(System.in);
int n=S.nextInt();
int[] a=new int[n];
for(int i=0;i<n;i++){
a[i]=S.nextInt();
}
int sum=0;
for(int i=n-1;i>=0;i--){
if(i%2==0){
sum=sum+a[i];
}
}
System.out.println(sum);
}
}
===================================================================================
===============
max sweets
--------------
import java.util.*;
class HelloWorld {
public static void main(String[] args) {
Scanner S=new Scanner(System.in);
int n=S.nextInt();
int e=S.nextInt();
int d=S.nextInt();
int ts=e*d;
int s=d/7;
int ed=d-s;
int mx=ed*n;
if(ts>mx)
{
System.out.println(-1);
}
int b=(ts+n-1)/n;
System.out.println(b);
}
}
=======================================================
no of candies he can buy
------------------------
import java.util.*;
class HelloWorld {
public static void main(String[] args) {
Scanner S=new Scanner(System.in);
int n=S.nextInt();
int[] a=new int[n];
for(int i=0;i<n;i++){
a[i]=S.nextInt();
}
int m=S.nextInt();
int c=0;
for(int i=0;i<n;i++){
if(a[i]%5==0){
c=c+1;
}
else{
if(a[i]<=m){
m=m-a[i];
c=c+1;
}
}
}

System.out.println(c);
}
}
============================================================
factor of 3
----------------
import java.util.*;

class HelloWorld {
public static void main(String[] args) {
Scanner S = new Scanner(System.in);
int n=S.nextInt();
int[] a=new int[n];
for(int i=0;i<n;i++){
a[i]=S.nextInt();
}
int c=0;
for(int i=0;i<n;i++){
if(a[i]%3==0){
c++;
}
}
System.out.println(c);
}
}

using functions
----------------
import java.util.*;

class HelloWorld {
public static int mul(int[] a,int n){
int c=0;
for(int i=0;i<n;i++){
if(a[i]%3==0){
c++;
}
}
return c;
}
public static void main(String[] args) {
Scanner S = new Scanner(System.in);
int n=S.nextInt();
int[] a=new int[n];
for(int i=0;i<n;i++){
a[i]=S.nextInt();
}
int ans=mul(a,n);
System.out.println(ans);
}
}

==================================================================
import java.util.*;

class HelloWorld {
public static void main(String[] args) {
Scanner S = new Scanner(System.in);
int n = S.nextInt();
String s = "";
while (n > 0) {
int digit = n % 10;
s = digit + s;
n = n / 10;
}

StringBuilder Str = new StringBuilder();


for (int i = 0; i < s.length(); i++) {
char cu = s.charAt(i);
if (cu == '0') {
Str.append('5');
} else {
Str.append(cu);
}
}

int num = Integer.valueOf(Str.toString());


System.out.println(num);
}
}

You might also like