tag:blogger.com,1999:blog-694855878384792308.post2681116367317634606..comments2024-03-21T06:26:49.387-07:00Comments on Java67: [Solved] How to Find 2 Largest Number from Integer Array in Java with Examplejavin paulhttp://www.blogger.com/profile/15028902221295732276noreply@blogger.comBlogger19125tag:blogger.com,1999:blog-694855878384792308.post-35890028329939986512022-11-02T00:29:55.946-07:002022-11-02T00:29:55.946-07:00very elegant div/conq algorithm.
both max's...very elegant div/conq algorithm. <br /><br />both max's are either in left or right halves, or first max in left, and second max in right half<br /><br />but of course, it is not linear complexity. it is O (n lgN)Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-10295280084107767602020-08-09T10:40:17.462-07:002020-08-09T10:40:17.462-07:00There is a condition: "You can not use any so...There is a condition: "You can not use any sorting functions and you should iterate the array only once"<br />Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-29770984966887504412020-03-05T09:48:43.858-08:002020-03-05T09:48:43.858-08:00this is right...this is right...DS WITH SATYENDRAhttps://www.blogger.com/profile/07042636895044466677noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-53596479410031183612019-10-02T10:03:57.670-07:002019-10-02T10:03:57.670-07:00#include
int main()
{
int n,arr[100],i;
scanf(&q...#include<br />int main()<br />{<br /> int n,arr[100],i;<br /> scanf("%d",&n);<br /> for(i=0;i=max1)<br /> max1=arr[i];<br /> }<br /> for(i=0;i=max2&&arr[i]<max1)<br /> max2=arr[i];<br /> }<br /> printf("%d %d",max1,max2);<br />}Anonymoushttps://www.blogger.com/profile/11209526015818673698noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-15810244740215790062019-05-15T17:37:05.657-07:002019-05-15T17:37:05.657-07:00This comment has been removed by the author.Charanya Chinnasamyhttps://www.blogger.com/profile/07770163141679521123noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-29233189476226305582019-05-15T17:36:37.522-07:002019-05-15T17:36:37.522-07:00def findToptwo(numlist):
max = numlist[0]
...def findToptwo(numlist):<br /> max = numlist[0]<br /> max1 = numlist[1]<br /> for i in range(2,len(numlist)):<br /> if numlist[i] > max:<br /> max1 = max<br /> max = numlist[i]<br /> elif numlist[i]>max1:<br /> max1 = numlist[i]<br /> return max,max1Charanya Chinnasamyhttps://www.blogger.com/profile/07770163141679521123noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-65705703975812133592018-08-11T09:44:44.552-07:002018-08-11T09:44:44.552-07:00Have to use else if statement..
public class Firs...Have to use else if statement..<br /><br />public class FirstTwoTopNumber {<br /><br /> static void findFirstTwoTopNumber(int[] arrayNum) {<br /><br /> int max1 = arrayNum[0];<br /> int max2 = 0;<br /><br /> for (int i = 1; i < arrayNum.length - 1; i++) {<br /> if (max1 < arrayNum[i]) {<br /><br /> max2 = max1;<br /> max1 = arrayNum[i];<br /> } else if (max2 < arrayNum[i]) {<br /> max2 = arrayNum[i];<br /> }<br /> }<br /> System.out.println("First Top :" + max1 + "\nSecond Top : " + max2);<br /> }<br /><br /> public static void main(String[] args) {<br /><br /> int myArray[] = { 1, 10, 3, 5, 1, 9, 4, 6 };<br /><br /> findFirstTwoTopNumber(myArray);<br /><br /> }<br /><br />}<br />StudyComputinghttps://www.blogger.com/profile/00904597386415290589noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-87424131154458727962018-04-08T01:48:45.522-07:002018-04-08T01:48:45.522-07:00/* Java program to find first repeating element in...<br />/* Java program to find first repeating element in arr[] */<br /><br />class Main {<br /> public static void main(String a[]) {<br /> int arr[] = { 10, 13, 15, 4, 34 };<br /> int max1 = arr[0];<br /> int max2 = 0;<br /> for (int i = 1; i <= arr.length - 1; i++) {<br /> if (max1 < arr[i]) {<br /> max1 = arr[i];<br /> max2 = max1;<br /> }<br /> }<br /> System.out.println(max1 + " " + max2);<br /> }<br />}<br /><br /><br /><br />It is giving wrong output : 34, 34Bhanu Prakash Guptahttps://www.blogger.com/profile/11139054694011994046noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-26823240694831656662017-12-13T15:49:26.859-08:002017-12-13T15:49:26.859-08:00class TwoMaxNumbers
{
public static void...class TwoMaxNumbers <br />{<br /><br /> public static void main(String... s)<br /> {<br /> int[] A = { 1, 2, 3, 5, 8, 9, 4, 6 };<br /> int top1 = 1;<br /> int top2 = 0;<br /> for (int i = 0; i < A.length; i++) <br /> {<br />if (top1 < A[i]) <br />{<br /> top2 = top1;<br /> top1 = A[i];<br /> }<br />}<br /> System.out.println(top1 + " and " + top2);<br /> }<br /><br />}Anonymoushttps://www.blogger.com/profile/14674824741446325475noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-6844374365978638042016-06-01T18:30:06.748-07:002016-06-01T18:30:06.748-07:00public class test17 {
public static void main(St...public class test17 {<br /><br /> public static void main(String[] args)<br /> {<br /> int[] numbers={45,23,56,23,56,4,89,4,3};<br /> int intTopElement=numbers[0];<br /> int intSecondTopElement=numbers[0];<br /> for(int i=0;iintTopElement)<br /> {<br /> intSecondTopElement=intTopElement;<br /> intTopElement=numbers[i];<br /> }<br /> }<br /> System.out.println("Top Element: "+ intTopElement);<br /> System.out.println("Second Top Element: "+intSecondTopElement);<br /> }<br /> <br />}<br />Raghuhttps://www.blogger.com/profile/09159966558650222609noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-66841622876942209782016-02-22T21:28:39.754-08:002016-02-22T21:28:39.754-08:00public static void getTopTwoMax(int[] arr){
int ... public static void getTopTwoMax(int[] arr){<br /> int firstMax , secondMax ;<br /> if(arr[0]>arr[1]){<br /> firstMax=arr[0];<br /> secondMax=arr[1];<br /> }<br /> else{<br /> firstMax=arr[1];<br /> secondMax=arr[0];<br /> }<br /><br /> for(int i=2;ifirstMax ){<br /> firstMax= arr[i];<br /> }<br /> if(arr[i]>secondMax&&arr[i]<firstMax){<br /> secondMax= arr[i];<br /> }<br /> }<br /> System.out.println("FirstMax="+firstMax+" SecondMax="+secondMax);<br /> <br /> }Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-91921259691346189712016-01-11T04:22:15.809-08:002016-01-11T04:22:15.809-08:00@Can, thanks for handling duplicates, good work.@Can, thanks for handling duplicates, good work.javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-46752973397819100592016-01-10T22:33:20.305-08:002016-01-10T22:33:20.305-08:00when I provide duplicate numbers, first and second...when I provide duplicate numbers, first and second maximum values become the same:<br />Given integer array : [1, 1, 2, 2, 4, 4, 5, 5, 3]<br />First maximum number is : 5<br />Second maximum number is : 5<br /><br />we can modify the else if like the following:<br /><br />public static void topTwo(int[] numbers) {<br /> int max1 = Integer.MIN_VALUE;<br /> int max2 = Integer.MIN_VALUE;<br /> for (int number : numbers) {<br /> if (number > max1) {<br /> max2 = max1;<br /> max1 = number;<br /> } else if (number > max2 && number!=max1) {<br /> max2 = number;<br /> }<br /> }<br /><br /> System.out.println("Given integer array : " + Arrays.toString(numbers));<br /> System.out.println("First maximum number is : " + max1);<br /> System.out.println("Second maximum number is : " + max2);<br /> }<br /><br /><br />Anonymoushttps://www.blogger.com/profile/16255195707119582170noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-79767595190381897072015-11-20T06:27:42.014-08:002015-11-20T06:27:42.014-08:00class maximum
{
public static void main(String a[]...class maximum<br />{<br />public static void main(String a[])<br />{<br />int arr[]={10,13,15,4,34};<br />int max1=arr[0];<br />int max2;<br />for(i=1;i<=arr.length();i++)<br />{<br />if(max1<arr[i])<br />{<br />max1=arr[i];<br />max2=max1;<br />}<br />}<br />System.out .println(max1+" "+max2);<br />}<br />}Anonymoushttps://www.blogger.com/profile/08416885498314772637noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-80018267163423450762015-09-10T11:13:38.919-07:002015-09-10T11:13:38.919-07:00Your program will fail when highest number is in 2...Your program will fail when highest number is in 2 place.<br />e.g int[] A = { 1, 10, 3, 5, 1, 9, 4, 6 }<br />your program will give output 10 and 1<br />But expected output is 10 and 9<br /><br />Correct program would be:<br />public static void main(String a[]){<br /> int[] A = { 1, 10, 3, 5, 1, 9, 4, 6 }; // answer should be 10 and 9<br /> int top1 = 1;<br /> int top2 = 0;<br /> for (int i = 0; i < A.length; i++) {<br /> if (top1 < A[i]) {<br /> top2 = top1;<br /> top1 = A[i];<br /> }<br /> else if (top2 < A[i]) {<br /> top2 = A[i];<br /> }<br /> }<br /> System.out.println(top1 + " and " + top2);<br /> }<br />Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-38548810403181373572015-09-05T08:34:16.395-07:002015-09-05T08:34:16.395-07:00That was my first option at first glance at the pr...That was my first option at first glance at the problem, but then I realized that there are at least 2 test-cases in which this method fails:<br /><br />1. Array contains only negative values -> In this case, the if condition will never be satisfied and the block inside the if block will never be executed, hence the top1 and top2 int will remain at their default values (1 and 0)<br />2. The second largest number is after the largest number. In this case, when the second largest number comes into the loop, it if block statements will not execute because the if condition will verify if the current number checked (second number) is larger than the current largest number. This will return false, so the top2 int will not change, even though is is supposed to.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-24441504246280156942015-07-27T02:30:43.487-07:002015-07-27T02:30:43.487-07:00public class TwoMaxNumbers {
public static v...public class TwoMaxNumbers {<br /> <br /> public static void main(String a[]){<br /> int[] A = { 1, 2, 3, 5, 8, 9, 4, 6 }; // answer should be 9 and8<br /> int top1 = 1;<br /> int top2 = 0;<br /> for (int i = 0; i < A.length; i++) {<br /> if (top1 < A[i]) {<br /> top2 = top1;<br /> top1 = A[i];<br /> }<br /> }<br /> System.out.println(top1 + " and " + top2);<br /> }<br /> <br />}Anujhttps://www.blogger.com/profile/06373990256344891336noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-54653406051265124722015-07-04T06:26:06.677-07:002015-07-04T06:26:06.677-07:00 public static void topTwo2(int[] numbers) {
... public static void topTwo2(int[] numbers) {<br /> Arrays.sort(numbers);<br /> <br /> System.out.println(numbers[numbers.length-2]);<br /> System.out.println(numbers[numbers.length-1]);<br /> <br /> }Anonymoushttps://www.blogger.com/profile/14847466046912323983noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-8770087164038362352015-05-27T05:16:01.792-07:002015-05-27T05:16:01.792-07:00thanxthanxAnonymousnoreply@blogger.com