19bcd7246 Assignment2 L27+L28+L31+L32

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

NAME OF THE STUDENT : SRIHARSHITHA DEEPALA

REGISTRATION NUMBER : 19BCD7246


SLOT : L27 + L28 + L31 + L32
ASSIGNMENT NUMBER : 01

1. Sum of Duplicates in an Array

Sample Test Case: 10 20 30 40 50 60 40 20 10

O/P: 70 (10+20+40)

CODE

import java.util.*;

class DuplicateSum

public static void Duplicate_Sum(int arr[], int n)

int sum=0;

boolean v[] = new boolean[n];

Arrays.fill(v, false);

//traverse through elements of the array and count the frequencies

for (int i = 0; i < n; i++) {

//if the element is already visited then skip that

if (v[i] == true){

continue;

int count = 1;

for (int j = i + 1; j < n; j++) {


if (arr[i] == arr[j]) {

v[j] = true;

count++;

if(count>=2){

sum+=arr[i];

System.out.println(sum);

public static void main(String []args)

Scanner scan = new Scanner(System.in);

int n = scan.nextInt();

int arr[] = new int[n];

for(int i=0;i<n;i++){

arr[i]=scan.nextInt();

Duplicate_Sum(arr, n);

scan.close();

}
OUTPUT

2. Highest and lowest array sum and product with removing duplicates.

A [ ]= [a0, a1, a2, a3....an]

Range 2>=n

Sample I/P= [5,6,3,2,1,4]

Sample O/P:

Highest Product=720 Highest Sum=21


Lowest Product =2 Lowest Sum=3

Part-2

if m=3

Sample O/P:

Highest Product= 120 Highest Sum=15

Lowest Product = 6 Lowest Sum=6

CODE

import java.util.*;

class SumandProduct{

public static void main(String[]args){

Scanner scan = new Scanner(System.in);

int m = scan.nextInt();

int n = scan.nextInt();

int arr[] = new int[n];

int prod=1, sum=0;

int prod1=1,sum1=0;

int prod2=1,sum2=0;

for(int i=0;i<n;i++){

arr[i]=scan.nextInt();

Arrays.sort(arr);

int length = arr.length;

length = sop(arr,length);

for(int i=0;i<length;i++){

prod*=arr[i];
sum+=arr[i];

System.out.println("Highest Product="+prod+"\t"+"Highest Sum="+sum);

System.out.println("Lowest Product="+(arr[0]*arr[1])+"\t"+"Lowest Sum="+(arr[0]+arr[1]));

System.out.println();

for(int i=n-1;i>=m;i--){

prod1*=arr[i];

sum1+=arr[i];

for(int i=0;i<m;i++){

prod2*=arr[i];

sum2+=arr[i];

System.out.println("Highest Product="+prod1+"\t"+"Highest Sum="+sum1);

System.out.println("Lowest Product="+prod2+"\t"+"Lowest Sum="+sum2);

scan.close();

public static int sop(int arr[], int n){

if(n==0||n==1){

return n;

int j=0;

for(int i=0;i<n-1;i++){

if(arr[i]!=arr[i+1]){

arr[j++]=arr[i];

arr[j++]=arr[n-1];
return j;

}
OUTPUT

You might also like