www.newleafins.
com
Problem Description
You are in charge of the cake for a child's birthday. You
have decided the cake will have one candle for each year of
their total age. They will only be able to blow out the tallest
of the candles. Count how many candles are tallest.
Example
The maximum height candles are units high. There are of
them, so return.
Function Description
Complete the function birthday Cake Candles in the editor
below.
Birthday Cake Candles has the following parameter(s):
int candles[n]: the candle heights
Returns
int: the number of candles that are tallest
Input Format
The first line contains a single integer, , the size of .
The second line contains space-separated integers,
where each integer describes the height of .
Sample Input 0
4
3213
Sample Output 0
2
www.newleafins.com
Explanation
Candle heights are. The tallest candles are units, and there
are of them.
Code
public class KRCT5 {
public static void main(String ag[])
{
Scanner s=new Scanner(System.in);
int n=s.nextInt();
int x[]=new int[n];
for(int i=0;i<n;i++)
{
x[i]=s.nextInt();
}
int m=0,c=0;
for (int i = 0; i < n; i++)
{
m=Math.max(m,x[i]);
www.newleafins.com
}
for(int a:x)
{
if(a==m)
c++;
}
System.out.println(c);
}
}
Problem Description
A doctor has a clinic where he serves his patients. The
doctor’s consultation fees are different for different
groups of patients depending on their age. The patients
age has read from each array values. If the patient’s age is
below 17, fees is 200 INR. If the patient’s age is between
17 and 40, fees is 400 INR. If patient’s age is above 40,
fees is 300 INR. Write a code to calculate earnings in a day
for which one array/List of values representing age of
patients visited on that day is passed as input.
Note:
Age should not be zero or
less than zero or above 120
Doctor consults a maximum of
20 patients a day
www.newleafins.com
Example 1: 18 21 45 60 87 22 10 12
Output: Rs.2500
Code:
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int n=s.nextInt();
int x[]=new int[n];
for(int i=0;i<n;i++){
x[i]=s.nextInt();
}
int c =0,d=0,e=0;
for(int i=0;i<n;i++)
{ if(x[i]<17)
c++;
else if(x[i]>=17&&x[i]<=40)
d++;
else if (x[i]>40)
e++;
else
System.out.println("Invalid");
}
int sum=(c*200)+(d*400)+(e*300);
System.out.println(sum);
}
}
www.newleafins.com
Problem Description
Given an array of integers ar = [1, 2, 3, 4, 5, 6] and a
positive integer K = 5, Avis must determine the number of
pairs (I, J) where I < J and ar[I] + ar[J] is divisible by K.
In this scenario, three pairs meet the criteria:
(1, 4) because 1 + 4 = 5, which is divisible by 5. (2, 3)
because 2 + 3 = 5, which is divisible by 5. (4, 6) because 4
+ 6 = 10, which is divisible by 5.
So, the number of valid pairs is 3.
To help Avis solve this challenge for any array of integers
and any positive integer K,
Input Format
The first line contains 2 space separated integers, N and K.
The second line contains N space-separated integers, each
a value of ar[I].
Constraints
2<=N<=100 1<=K<=100 1<=ar[I]<=100
Output Format
The number of pairs
Sample Input 0
10 4
765 345 22 123 89 765 345 793 456 239
Sample Output 0
12
Code
www.newleafins.com
import java.util.*;
public class KSRCT1 {
public static void main(String[] args) {
int count=0;
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int[] arr=new int[n];
for(int i=0;i<n;i++){
arr[i]=sc.nextInt(); }
int key=sc.nextInt();
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
if((arr[i]+arr[j])%key==0){
count++;
} } }
System.out.println(count); }
}
AMICABLE PAIRS
(220, 284), (1184, 1210), (2620, 2924), (5020, 5564),
(6232, 6368), (10744, 10856), (12285, 14595), (17296,
18416), (63020, 76084), and (66928, 66992).
www.newleafins.com
Code
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n1=sc.nextInt();
int n2=sc.nextInt();
int sum1=0;
int sum2=0;
for(int i=1;i<n1;i++)
{
if(n1%i==0)
sum1+=i;
}
for(int i=1;i<n2;i++)
{
if(n2%i==0)
sum2+=i;
}
if(sum1==n2 && sum2==n1)
System.out.println("Amicable Numbers");
else
System.out.println("Not Amicable Numbers");
}
}
DISARIUM NUMBER
www.newleafins.com
A number is said to be the Disarium number when the sum
of its digit raised to the power of their respective positions
is equal to the number itself.
For example, 175 is a Disarium number as follows
11 + 72 + 53 = 1 + 49 + 125 = 175
Some of the other examples of Disarium number are 89,
135, 518 etc.
*************
To Print only Deficient Number from List of Array Values
DEFICIENT NUMBER
A number is a positive integer n for which the sum of
divisors of n is less than 2n.
Divisors are 1, 3, 7 and 21. Sum of divisors is 32.
This sum is less than 2*21 or 42.
Input: 12 21 0 -1
Output: 21
Input: 17 0 13 14 -3
Output: 17
Write a function that returns the reverse of the input
array
Input Format
www.newleafins.com
First line contains size of array, n (n>0).Second
line contains elements of array.
Output Format
Print the array elements in reverse order.
Sample Input
3
123
Sample Output
321
Explanation
The reverse of array {1, 2, 3}
would be {3, 2, 1}.
Code:
import java.util.*;
class Main
{
static void reverse_array(int arr[],int n)
{
int temp ;
for(int i =0 ; i<n/2 ; i++)
{
temp = arr[i] ;
arr[i] = arr[n-i-1] ;
arr[n-i-1] = temp;
}
}
public static void main(String[] args)
www.newleafins.com
{
Scanner sc =new Scanner(System.in);
int n = sc.nextInt() ;
int arr[] =new int[n] ;
for(int i =0 ; i < n ; i++)
arr[i] = sc.nextInt() ;
reverse_array(arr, n) ;
for(int i =0 ; i < n ; i++)
System.out.print(arr[i]+" ") ;
}
}
To print First Max and First Min, do the same in length of
array
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
int[] a = new int[n];
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
Arrays.sort(a);
for(int i=0;i<n/2;i++)
{
www.newleafins.com
System.out.print(a[n-i-1]+" ");
System.out.print(a[i]+" ");
}
if(n%2!=0)
System.out.print(a[n/2]);}
}
To verify a given array in decreasing order format if yes
print first element and No print Last element
import java.util.*;
public class KEC2
{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
int N = s.nextInt();
s.nextLine(); // Consume newline
int nums[]=new int[100];
for(int i=0;i<N;i++)
{
nums[i] = s.nextInt();
}
System.out.println("Value from Array "+ check(nums));
}
www.newleafins.com
static int check(int nums[])
{
int first = 0, second = 1;
for(int i=1;i<nums.length;i++)
{
if(nums[first]<nums[second])
return nums[nums.length-1];
first++;
second++; }
return nums[0];
}
}}
Count of even and odd power pairs in an Array
Input: arr[] = {2, 3, 4, 5}
Output:
6
6
Explanation: (2, 3), (2, 4), (2, 5), (4, 2), (4, 3) and (4, 5) are
the pairs with even values
and (3, 2), (3, 4), (3, 5), (5, 2), (5, 3) and (5, 4) are the
pairs with odd values.
Input: arr[] = {10, 11, 20, 60, 70}
Output:
16
4
Explanation: (10, 11), (10, 20), (10, 60), (10, 70), (20, 10),
(20, 11), (20, 60), (20, 70), (60, 10), (60, 11), (60, 20), (60,
www.newleafins.com
70), (70, 10), (70, 11), (70, 20), (70, 60) are the pairs with
even values and (11, 10), (11, 20), (11, 60), (11, 70) are
the pairs with odd values.
Code
static void countPairs(int arr[], int n)
{
int even = 0, odd = 0;
for (int i = 0; i < n; i++)
{
if (arr[i] % 2 == 0)
even++;
else
odd++;
}
System.out.println((even) * (n - 1));
System.out.println((odd) * (n - 1));
}
Problem Description
You are a professional robber
planning to rob houses along a street. Each house has a
certain amount of money
stashed, the only constraint stopping you from robbing
each of them is that
adjacent houses have security systems connected and it
will automatically
contact the police if two adjacent houses were broken
www.newleafins.com
into on the same night.
Given an integer
array nums representing the amount of money of each
house,
return the maximum amount of money you can rob
tonight without
alerting the police.
Example 1:
Input: nums = [1,2,3,1]
Output: 4
Explanation: Rob house 1 (money =
1) and then rob house 3 (money = 3).
Total amount you can rob = 1 + 3 =
4.
Code
class Solution {
public static int rob(int[] nums) {
www.newleafins.com
if (nums.length == 0) return 0;
int prev1 = 0;
int prev2 = 0;
for (int num : nums)
{
int current = Math.max(prev2 + num, prev1);
prev2 = prev1;
prev1 = current;
}
return prev1;
}
public static void main(String[] args)
{
int nums[] = {1,2,3,1,5,6};
System.out.println(rob(nums));
}
}
Problem Description
To Find pair value to equal to K
int[] x = {12, 3, 4, 5, 66};
int K = 15;
Output
www.newleafins.com
12 3
Code
public class KRCE3 {
public static void main(String[] args) {
int[] x = {12, 3, 4, 5, 66};
int K = 15;
findPair(x, K);
}
public static void findPair(int[] arr, int sum) {
for (int i = 0; i < arr.length - 1; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[i] + arr[j] == sum) {
System.out.println(arr[i] + " " + arr[j]);
return;
}
}
}
System.out.println("No pair found");
}
}
www.newleafins.com
Problem Description
Best Time to Buy and Sell Stock
You are given an array prices where prices[i] is the price of
a given stock on the ith day.
You want to maximize your profit by choosing a single
day to buy one stock and choosing a different day in the
future to sell that stock.
Return the maximum profit you can achieve from this
transaction. If you cannot achieve any profit, return 0.
Example 1:
Input: prices = [7,1,5,3,6,4]
Output: 5
Explanation: Buy on day 2 (price = 1) and sell on day 5
(price = 6), profit = 6-1 = 5.
Note that buying on day 2 and selling on day 1 is not
allowed because you must buy before you sell.
Example 2:
Input: prices = [7,6,4,3,1]
Output: 0
Explanation: In this case, no transactions are done and the
max profit = 0.
Code
int max=0;
int x=-1,y=-1;
www.newleafins.com
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++)
{
if(arr[j]-arr[i]>max)
{
max=arr[j]-arr[i];
x=i;
y=j;
}
}
SOP("The maximum profit if bought on and sold on and
profit is "+x+1+” “ +y+1+” “ +max);
To print Repeated digit values only from Array
44,33,777,1111
public class KEC12
{
int a,i, t, d, flag;
void print(int arr[])
{
for(i = 0; i < arr.length; i++)
{
www.newleafins.com
t =arr[i];
int len=Math.log10(t)+1;
If(len>1){
d =t % 10;
t =t/10;
if(t> 0)
{
flag = 0;
while(t>0)
{
if (t % 10 != d)
{
flag = 1;
break;
}
t /= 10;
}
if (flag == 0)
System.out.println(" "+arr[i]);
}
}}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
www.newleafins.com
int n=sc.nextInt();
int[] a = new int[n];
int flag=1;
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
KEC12 k12=new KEC12();
k12.print(a);
}
}
To Restore all even values in Left slide and odd values in
Right Slide Array
public class Main {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
int[] a = new int[n];
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
for (i = 0; i < n; i++)
{ for (j = 0; j< n; j++)
{
if(x[i]%2==0)
www.newleafins.com
{
t=x[i];
x[i]=x[j];
x[j]=t;
} }}
for (int j :x)
{
System.out.println(“ “ +j);
}
To Find K value in Array Element at any Position
34,455,78,90,45
K=45
Count =2
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int arr[]= {0,112,14,144,1444};
int k=sc.nextInt();
www.newleafins.com
int c=0;
String k1=String.valueOf(k);
for(int i=0;i<arr.length;i++)
{
String s=String.valueOf(arr[i]);
if(s.contains(k1))
c++;
}
System.out.print(c);
}
}
To Find Maximum Profit in Stock Market
#include <stdio.h>
int main()
{
int n=7;
int arr[n];
for(int i=0;i<n;i++)
scanf("%d",&arr[i]);
int max=0;
int x=-1,y=-1;
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++)
{
if(arr[j]-arr[i]>max)
www.newleafins.com
{
max=arr[j]-arr[i];
x=i;
y=j;
}
}
printf("The maximum profit if bought on %d and sold
on %d and profit is %d",x+1,y+1,max);
return 0;
}
Problem Description
Given an integer array nums of size n, return the
minimum number of moves required to make all array
elements equal.
In one move, you can increment n - 1 elements of the
array by 1.
Example 1:
Input: nums = [1,2,3]
Output: 3
Explanation:
Only three moves are needed (remember each
www.newleafins.com
move increments two elements):
[1,2,3] =>
[2,3,3] => [3,4,3]
=> [4,4,4]
Example 2:
Input: nums = [1,1,1]
Output:
0
Code
int[]nums={4,3,4};
int sum = 0,n=nums.length;
int minm = Integer.MAX_VALUE;
System.out.println(minm);
for(int i=0;i<n;i++)
{
minm = Math.min(minm, nums[i]);
sum = sum + nums[i];
}
System.out.println(sum - n*minm);
Problem Description
To check 24 hrs Time format?
www.newleafins.com
Input: arr = [1,2,3,4]
Output: "23:41"
Explanation: The valid
24-hour times are "12:34", "12:43", "13:24",
"13:42", "14:23", "14:32", "21:34",
"21:43", "23:14", and "23:41". Of these times,"23:41" is
the latest.
Example 2:
Input: arr = [5,5,5,5]
Output: ""
Problem Description
To find max value from each row and Find Max value from
Anti-Diagonal Matrix.
public class KRCE5
{
public static void main(String args[]){
int a[][]={{1,3,6},{22,4,3},{3,4,95}};
for(int i=0;i<3;i++)
{
www.newleafins.com
for(int j=0;j<3;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
int n=3;
for(int i=0;i<3;i++)
{
System.out.println(a[i][n-i-1]+" ");
}
System.out.println();
int max=0;
for(int i=0;i<3;i++)
{ max=0;
for(int j=0;j<3;j++)
{
max=(int)Math.max(max,a[i][j]);
if(j==2)
www.newleafins.com
System.out.println("Max value from each row "+max);
}
}
}
}
Problem Description
To find max value from each row and col should not equal
and finally sum of all values and print it
Scanner sc=…..;
int r = sc.nextInt();
int c = sc.nextInt();
int[][] array = new int[r][c];
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
array[i][j]=sc.nextInt();
}
}
int max=0; int in,jn,pre=-1;
for(int i=0;i<r;i++)
www.newleafins.com
{
max=0;
in=0;
jn=0;
for(int j=0;j<c;j++)
{
if(j!=pre)
{
if(max<array[i][j])
{
max=array[i][j];
in=i;
jn=j;
}
}
} pre=jn;
System.out.println("The Max Element at "+(i+1)+"
row is : "+max+" at index : "+in+" "+jn); } }}
Count the Islands in the matrix:
int count=0,d1=0,d2=0;
for(int i=0;i<n;i++)
{ int cc=0,rc=0;
for(int j=0;j<n;j++)
www.newleafins.com
{
if(arr[i][j]==1)
cc+=1;
if(arr[j][i]==1)
rc+=1;
if(i==j)
{
if(arr[i][j]==1)
d1+=1;
if(arr[i][n-i-1]==1)
d2+=1;
}
}
if(rc==1)
{count+=1;}
if(cc==1)
{count+=1;}
}
if(d1==1){count+=1;}
if(d2==1){count+=1;}
sop("Total islands"+count);
return 0;
}
Binary Search in Iterative Method
www.newleafins.com
Class test
{
int binarySearch(int array[], int k, int low, int high)
{
while (low <= high)
{
int mid = low + (high - low) / 2;
if (array[mid] == k)
return mid;
if (array[mid] < k)
low = mid + 1;
else
high = mid - 1; } return -1;}
public static void main(String a[])
{
int array[] = {3, 4, 5, 6, 7, 8, 9};
int k = 4;
int n=array.length-1
www.newleafins.com
int result = binarySearch(array, k, 0, n);
if (result == -1)
sop("Not found");
else
sop("Element is found at index”+ result);
}