anirudh3.kumar
BAN USER
Try this
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
int[] integerArray = {1,2,3,4,5,5,6};
System.out.println(getMinStones(integerArray, 3));
}
public static int getMinStones(final int a[], final int k) {
if ((a.length < k)) {
return -1;
}
Arrays.sort(a);
int i, last, stones1, stones2,stones, count;
stones1 = 0;
stones2 = 0;
// Traverse until last repeated value of a[i]
for (i = k; i < a.length && a[k - 1] == a[i]; i++);
last = a[i - 1];
// Count number of stones from end of array
for (i = 0; i < k; i++)
{
stones1 += a[a.length - 1 - i];
}
//
for (i = 0, count = 0; count < k; i++)
{
if ((a[a.length - i - 1] > last) )
{
stones2 += last;
}
else
{
stones2 += a[a.length - i - 1];
count++;
}
}
stones = Math.min(stones1, stones2);
return stones;
}
}
The answer to the question
"Why is answer 8 in your example, what if the crow put the 8 into the pot with overflow number of 11?"
is that the crow is intelligent enough to know that if he puts 2 stones in 4 pots, he would at least overflow 2 pots.
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
int[] integerArray = {1,2,3,4,5,5,6};
System.out.println(getMinStones(integerArray, 3));
}
public static int getMinStones(final int a[], final int k) {
if ((a.length < k)) {
return -1;
}
Arrays.sort(a);
int i, last, stones1, stones2,stones, count;
for (i = k; i < a.length && a[k - 1] == a[i]; i++);
last = a[i - 1];
stones1 = 0;
stones2 = 0;
for (i = 0; i < k; i++)
{
stones1 += a[a.length - 1 - i];
}
for (i = 0, count = 0; count < k; i++)
{
if ((a[a.length - i - 1] > last) )
{
stones2 += last;
}
else
{
stones2 += a[a.length - i - 1];
count++;
}
}
stones = Math.min(stones1, stones2);
return stones;
}
}
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
int[] integerArray = {1,2,3,4,5,5,6};
System.out.println(getMinStones(integerArray, 3));
}
public static int getMinStones(final int a[], final int k) {
if ((a.length < k)) {
return -1;
}
Arrays.sort(a);
int i, last, stones1, stones2,stones, count;
for (i = k; i < a.length && a[k - 1] == a[i]; i++);
last = a[i - 1];
stones1 = 0;
stones2 = 0;
for (i = 0; i < k; i++)
{
stones1 += a[a.length - 1 - i];
}
for (i = 0, count = 0; count < k; i++)
{
if ((a[a.length - i - 1] > last) )
{
stones2 += last;
}
else
{
stones2 += a[a.length - i - 1];
count++;
}
}
stones = Math.min(stones1, stones2);
return stones;
}
}
Algorithm fails for the below input
input: {1, 2, 2, 3, 3, 3, 3, 3, 5, 9, 9, 11}, K = 5, Actual Ans: 27. This algo ans: 32
Repnoradweisser, Backend Developer at ASU
I am Nora, I am a writer, I write many types blogs and articles. I collected many types and astrology ...
Repevelynleary0, Consultant at Arista Networks
Hi, I am Evelyn from Los Angeles, USA. I have been a Marketing Manager in Veramons Digital Company from last ...
RepI am randy, 29 year old and I am doing a job. I am doing a job as a plant ...
Reprickyjwerner, Area Sales Manager at Accolite software
I am Ricky, I live in Houston USA, I am working as a distribution manager in Total Serve company. I ...
Repjasonmcarrier, Analyst at Abs india pvt. ltd.
Hello, I live in Houston and I am working as a Convention planner in Roberd's company, I also part ...
Reptanyamayst, Android Engineer at ABC TECH SUPPORT
I am an experienced project support officer seeking a team focused on excellence and accelerated performance in a BMO company ...
Repjosiredhima, AT&T Customer service email at Aricent
I am an architect with three years experience planning and designing commercial buildings. I am working as principal architect on ...
Repjohndbutler0, Aghori Mahakal Tantrik at BMO Harris Bank
I consider myself to be driven, proactive, hard working, a team player, creative and absolutely passionate person! I am strong ...
Repsonjamramos45, Graphics Programmer at CGI-AMS
I am a strong writer with a passion for story-telling who has extensive experience of writing literary compositions, articles, reports ...
Repcharlesndouglass, Employee at VANS
I am Michael from Nantucket USA. I am working as a Power plant dispatcher in Matrix Design company. I am ...
Repshanitajjana, +27655765355 SSD MONEY CLEANING CHEMICAL +27655765355 BLACK MONEY IN JOHANNESBURG, OMAN, SAUDI ARABIA, SUDAN, Somalia ,Zimbabwe Botswana at 247quickbookshelp
Hi I am Shanita from San Bernardino USA. I am working as a manager in flore company. I am outgoing ...
Replonnacribbs, DIGITAL MARKETING at Athena Health
I am a Market Research Manager responsible for selecting the appropriate research methodology and supporting techniques to meet a defined ...
Repamysamson688, Accountant
Hi, I am an art teacher, good in all areas of art history, from ancient art through to contemporary art ...
Rephallieriddic, HR Executive Trainee at Barclays Capital
I am Hallie, Dedicated and experienced administrative secretary who excels at prioritizing , completing multiple tasks simultaneously and following through to ...
Repangelwilley8, xyz at Asiatic Solutions
I am Angel J. Willey from Gurnee. I am working as a truck driver in Asiatic Solutions company. I also ...
RepRuthMitchell, Applications Developer at ASAPInfosystemsPvtLtd
My name is Mitchell working as a technical sales support worker in the USA. I identify and establish new business ...
Repkathyrnunez, Area Sales Manager at Advent
I am manager in a Star Bright Investment Group company. I like to solve puzzles for Has excellent problem-solving and ...
Repcarmelalewis9, Area Sales Manager at AMD
I am fond of reading stories then reading articles which are all about a story of a beautiful nature and ...
Repcrystalblibby, Analyst at Achieve Internet
By professional i am teacher. Successfully supervised and assisted students, grade artwork, encouraged creativity and new technique.Familiar with art ...
Repalicegpopa, Apple Phone Number available 24/7 for our Customers at AMD
I am working as a U.S. marshal in Pro Property Maintenance. I want to write about I Want My ...
Repmonicaralvarado94, Korean Air Change Flight at Athena Health
I am fond of reading stories then reading articles which are all about a story of a beautiful nature and ...
Repmyershllims, abc at ASAPInfosystemsPvtLtd
Hiii, I am Myers and I am from Elizabeth. I am working as a Product designer designing most things I ...
RepRichardWParks, Accountant at ADP
Repclairetsmith49, AT&T Customer service email at ADP
Hello, I am Claire and work as a Human resources and I am responsible for recruiting, screening, interviewing and how ...
RepGinaSanchez, Computer Scientist at Autoportal.com
Ginna from New York.Successfully managing a high volume of work assignments without compromising quality to exceed client expectations.Apart ...
Try this
- anirudh3.kumar October 28, 2015