You are on page 1of 8

SUM OF ALL PRIME NOS IN THE ARRAY EXCEPT LARGEST/SMALLEST

Find sum of all prime numbers in the array, except the largest prime number: Madhav has been assigned the task of finding the sum of
all prime numbers in a given array, except the largest prime number in the array. Madhav approaches you to help him do this by
writing a program.Given an array of numbers, you are expected to find the sum of all prime numbers in the given array. You must
however exclude the largest prime number while performing this addition.For Example -If input1 (10,41,18,50,43,31,29,25,59,96,67)
representing the given array, and,input2 = 11 representing the number of elements in the array,then the expected output is 203, which
is the sum of all prime numbers in this array except the largest prime number 67.Explanation: The prime numbers in this array are 41,
43, 31, 29, 59 and 67. The largest prime number in this array is 67. So, let us leave out 67 and add all the other prime numbers to get
the output.Therefore, output = 41+43+31+29+59 = 203.Special conditions to be taken careNote: If the array does NOT contain any
prime number, the output should be the sum of all numbers in the array except the largest number. For example, If
input1=110,20,30,40) and input2 = 4 representing the number of elements in the array, then the expected output = 10+20+30= 60.

import java.util.*;
public class Main {
public static int sumOfPrime(int input1[],int input2)
{
List<Integer> al=new ArrayList<Integer>();
List<Integer> ls=new ArrayList<Integer>();
int sum=0,count=0,c=0;
for(int i=0;i<input2;i++)
{
count=0;
for(int j=1;j<=input1[i];j++)
{
if(input1[i]%j==0)
{
count++;
}
}
if(count==2)
{
al.add(input1[i]);
c++;
}

}
if(c==0)
{
for(int a=0;a<input1.length;a++)
ls.add(input1[a]);
for(int z:ls)
sum=sum+z;
//sum=sum-Collections.min(ls);sum=sum-Collections.max(ls);
}

else
{
for(int k:al)
sum=sum+k;
//sum=sum-Collections.min(al);sum=sum-Collections.max(al);
}

return sum;

public static void main(String[] args) {


// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int input2;input2=sc.nextInt();
int input1[]=new int[input2];
for(int i=0;i<input2;i++)
input1[i]=sc.nextInt();
System.out.println(Main.sumOfPrime(input1,input2));

}
import java.util.*;
public class FindKey2 {
public static int find(int input1,int input2,int input3,int input4)
{
int sume=0,sumo=0,c1=1,c2=1,c3=1;
List<Integer> l1=new ArrayList<Integer>();
List<Integer> l2=new ArrayList<Integer>();
List<Integer> l3=new ArrayList<Integer>();
while(input1>0 && input2>0 && input3>0)
{
if(c1%2==0 && c2%2==0 && c3%2==0)
{
sume=sume+input1%10+input2%10+input3%10;
c1++;c2++;c3++;
input1/=10;input2/=10;input3/=10;
}
else
{
sumo=sumo+input1%10+input2%10+input3%10;
input1/=10;input2/=10;input3/=10;
c1++;c2++;c3++;
}
}
if(input4%2==0)
{
return sume-sumo;
}
else
{
return sumo-sume;
}

}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int input1,input2,input3,input4;
input1=sc.nextInt();input2=sc.nextInt();input3=sc.nextInt();input4=sc.nextInt();
System.out.println(FindKey2.find(input1,input2,input3,input4));

}
}
Find key sum=smallest digit of input1* smallest digit of input2* smallest digit of input3+input4
import java.util.*;
public class findkey {
public static int find(int input1,int input2,int input3,int input4)
{
int sum;
List<Integer> l1=new ArrayList<Integer>();
List<Integer> l2=new ArrayList<Integer>();
List<Integer> l3=new ArrayList<Integer>();
while(input1>0)
{
l1.add(input1%10);input1=input1/10;
}
while(input2>0)
{
l2.add(input2%10);input2=input2/10;
}
while(input3>0)
{
l3.add(input3%10);input3=input3/10;
}
sum=Collections.min(l1)*Collections.min(l2)*Collections.min(l3)+input4;
return sum;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);int input1,input2,input3,input4;
input1=sc.nextInt();input2=sc.nextInt();input3=sc.nextInt();input4=sc.nextInt();
System.out.println(findkey.find(input1,input2,input3,input4));

}
}

Find Key
Key =max of inp1*min of inp1+max of inp2*min of inp2+max of inp3*min of inp3-input4

import java.util.*;
public class Main {
public static int find(int input1,int input2,int input3,int input4)
{
int sum;
List<Integer> l1=new ArrayList<Integer>();
List<Integer> l2=new ArrayList<Integer>();
List<Integer> l3=new ArrayList<Integer>();
while(input1>0)
{
l1.add(input1%10);input1=input1/10;
}
while(input2>0)
{
l2.add(input2%10);input2=input2/10;
}
while(input3>0)
{
l3.add(input3%10);input3=input3/10;
}
sum=(Collections.min(l1)*Collections.max(l1))+(Collections.min(l2)*Collections.max(l2))+
(Collections.min(l3)*Collections.max(l3))-input4;
return sum;

}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);int input1,input2,input3,input4;
input1=sc.nextInt();input2=sc.nextInt();input3=sc.nextInt();input4=sc.nextInt();
System.out.println(Main.find(input1,input2,input3,input4));
}
}
SUM OF EVEN/ODD DIGITS
import java.util.*;
public class Main {
public static int find(int input1,int input2,int input3,int input4)
{
int sum=0;
List<Integer> l1=new ArrayList<Integer>();
List<Integer> l2=new ArrayList<Integer>();
List<Integer> l3=new ArrayList<Integer>();
while(input1>0)
{
l1.add(input1%10);input1=input1/10;
}
while(input2>0)
{
l2.add(input2%10);input2=input2/10;
}
while(input3>0)
{
l3.add(input3%10);input3=input3/10;
}
if(input4%2==0)
{
for(int i:l1)
{
if(i%2==0)
sum=sum+i;
}
for(int i:l2)
{
if(i%2==0)
sum=sum+i;
}
for(int i:l3)
{
if(i%2==0)
sum=sum+i;
}
}
else
{
for(int i:l1)
{
if(i%2!=0)
sum=sum+i;
}
for(int i:l2)
{
if(i%2!=0)
sum=sum+i;
}
for(int i:l3)
{
if(i%2!=0)
sum=sum+i;
}
}
return sum;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int input1,input2,input3,input4;
input1=sc.nextInt();
input2=sc.nextInt();
input3=sc.nextInt();
input4=sc.nextInt();
System.out.println(Main.find(input1,input2,input3,input4));
}
}
SMALLEST DIGIT IN THOUSANDS, HUNDREDTHS, TENS, UNITS PLACE

import java.util.*;

public class Main {


public static String find(int input1,int input2,int input3,int input4)
{
int sum=0;
List<Integer> l1=new ArrayList<Integer>();
List<Integer> l2=new ArrayList<Integer>();
List<Integer> l3=new ArrayList<Integer>();
List<Integer> l4=new ArrayList<Integer>();
l1.add(input1%10);l1.add(input2%10);l1.add(input3%10);
l2.add((input1/10)%10);l2.add((input2/10)%10);l2.add((input3/10)%10);
l3.add((input1/100)%10);l3.add((input2/100)%10);l3.add((input3/100)%10);
l4.add(input1/1000);l4.add(input2/1000);l4.add(input3/1000);

return
Integer.toString(Collections.min(l4))+Integer.toString(Collections.min(l3))+Integer.toString(Collections.min(l2))+Integer.toString(Collection
s.min(l1));
}

public static void main(String[] args) {


// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int input1,input2,input3,input4;
input1=sc.nextInt();
input2=sc.nextInt();
input3=sc.nextInt();
System.out.println(Main.find(input1,input2,input3,input4));
}

}
FIRST REPEATED ELEMENT

import java.util.*;

class Main
{
public static int printFirstRepeating(int arr[],int n)
{
// Initialize index of first repeating element
int min = -1;

// Creates an empty hashset


HashSet<Integer> set = new HashSet<>();

// Traverse the input array from right to left


for (int i=arr.length-1; i>=0; i--)
{
// If element is already in hash set, update min
if (set.contains(arr[i]))
min = i;

else if(arr[i]>0) // Else add element to hash set


set.add(arr[i]);

// Print the result

if(set.size()==0)
return 0;
else
{
if (min != -1)
return arr[min];
else
return arr[0];
}

}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
Main obj = new Main();
int n=sc.nextInt();
int arr[] = new int[n];
for(int i=0;i<n;i++)
{
arr[i]=sc.nextInt();
}
System.out.println(Main.printFirstRepeating(arr, n));
}
}
First Repeated Element from Tail
Find first repeated element in the given array from the tail of the array:
Rajeev works in the data center lab of the survey department. He has been assigned the task of identifying "repeated numbers" in a given
set of numbers. He approaches you to help him achieve this. Given an array of numbers, your task is to return the first number that
repeats (appears again) starting from the last index.For example :If input1 (1.2.4.1.2.8) representing the given array, and
input2 = 6 representing the number of elements in the array. then the result should be 2 which is the first repeated number in the array
from the rear end.Special conditions to be taken.care.Note 1: You should ignore the negative numbers and zeros. The program should
consider only non-zero, non-negative numbers from the given array. Note 2: If no number is repeated then the output should be the last
element of the array.Note 3: If all elements in the array are negative or 0's the output should be 0.

import java.util.*;

class Main
{
public static int printFirstRepeating(int arr[],int n)
{
// Initialize index of first repeating element
int min = -1;

// Creates an empty hashset


HashSet<Integer> set = new HashSet<>();

// Traverse the input array from right to left


for (int i=0; i<n; i++)
{
// If element is already in hash set, update min
if (set.contains(arr[i]))
min = i;

else if(arr[i]>0) // Else add element to hash set


set.add(arr[i]);

// Print the result

if(set.size()==0)
return 0;
else
{
if (min != -1)
return arr[min];
else
return arr[n-1];
}

}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
Main obj = new Main();
int n=sc.nextInt();
int arr[] = new int[n];
for(int i=0;i<n;i++)
{
arr[i]=sc.nextInt();
}
System.out.println(Main.printFirstRepeating(arr, n));
}
}

You might also like