Mr. Myers wants to edit the marks assigned to the questions

Pyramid Examples

Java Program for find duplicate element and increased duplicate element by 1

 A mathematics question paper has certain number of questions and each question is assigned some random maximum marks Mr. Myers wants to edit the marks assigned to the questions such that. 

(1) all questions in the paper should have distinct maximum marks.

(2) the total marks of all the questions should be as low as possible. Mr. Myers wants to achieve this by making minimal changes in the original format, assigning the question at least so much marks as it originally had. find the minimum total marks that can set the paper for. ?

All the inputs in array are minimum marks for a question and it can increase if the duplicates occurred  in the array.

Example Input
3
2 4 4

Example Output
11

Explanation
As two questions have the same marks, max marks for one of them needs to be incremented to 4. 
He can set the paper with 2+4+4=11 marks.

This program can be solved by two way, one is simple program and second is using collection technique.

 So let see we are solve this program by using 1st simple technique  but a problem using this technique, so we are not recommend use technique for a big project, also this technique is pass only 1-3 test cases, so we can use partially.

import java.util.*;
public class ArrayExample
{
public static void main(String args[] )
{
   @SuppressWarnings("resource")
   Scanner sc = new Scanner(System.in);
   int [] arr = new int[sc.nextInt()];
   int sum = 0;
   int count[] = new int[1];
   count[0] = 1;
   for(int i=0; <arr.length; i++)
    {
     arr[i]=sc.nextInt();
     }
   for(int k=0; k <arr.length; k++)
    {
     for(int l=k+1; l<arr.length; l++)
      {
       if(arr[k]==arr[l])
        {
        arr[k] = arr[k]+count[0];
        }
        }
      }
   for (int num: arr)
    {
    sum=sum+num;
    }
  System.out.print(sum);
}
}

first Input provide by user is 3

Second Input provide by user 1,2,2

Output is 6

Comments

  1. if you give a input 5 and 1,4,5,4,5 ans=23 but this program give output 22

    ReplyDelete

Post a Comment

Popular posts from this blog

Genral Ability

Object Orinted Programing