tag:blogger.com,1999:blog-8712770457197348465.post7324108470151294961..comments2024-03-17T23:01:26.872-07:00Comments on Javarevisited: 3 ways to swap two Numbers without using Temp or Third Variable in Java?javin paulhttp://www.blogger.com/profile/15028902221295732276noreply@blogger.comBlogger51125tag:blogger.com,1999:blog-8712770457197348465.post-52317387376359199052018-09-01T03:33:31.285-07:002018-09-01T03:33:31.285-07:00Sir, then how is it that
a=a+b-(b=a) works?
Becau...Sir, then how is it that <br />a=a+b-(b=a) works?<br />Because, according to what you said, assignment happens first.<br /><br />a=2 b=3<br /><br />So, 1st execute (b=a); //b=2<br />Then a=a+b-(b=a);<br />a=2+2-2<br />a=2<br />Can you please explain?Snehahttps://www.blogger.com/profile/17036267606372454191noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-79332284665459099062017-11-02T07:18:09.681-07:002017-11-02T07:18:09.681-07:00in multiplication and division process if we give ...in multiplication and division process if we give any one of them value is zero(0) then it wont workAnonymoushttps://www.blogger.com/profile/04879083619743692640noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-62593246279479750572017-09-18T07:26:08.868-07:002017-09-18T07:26:08.868-07:00int a = 10;
int b = 20;
System.out.println(&qu...int a = 10;<br /> int b = 20;<br /> System.out.println("a = " + a);<br /> System.out.println("b = " + b);<br /> System.out.println("----------");<br /> b = b - a;<br /> a = a + b;<br /> <br /> System.out.println("a : " + a);<br /> System.out.println("b : " + b);Anonymoushttps://www.blogger.com/profile/18100986973104255789noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-6490303636940455792017-06-07T20:24:27.341-07:002017-06-07T20:24:27.341-07:00@Javin.. Can you please explain what is the proble...@Javin.. Can you please explain what is the problem of overflow in method 1 & 3 and how can it be prevented using method 2?Kewal Shahnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-81751443998505920852017-02-03T02:03:21.161-08:002017-02-03T02:03:21.161-08:00In bitwise swapping, if both values are equal, bot...In bitwise swapping, if both values are equal, both values will become zero. so it should be handled.DHARANI PATHY Rhttps://www.blogger.com/profile/03825414903461162744noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-26365953169586528402017-01-15T18:18:01.380-08:002017-01-15T18:18:01.380-08:00Will there be a overflow in 1?Will there be a overflow in 1?Anonymoushttps://www.blogger.com/profile/05315036637267871145noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-54869830180803906852016-09-08T04:57:28.726-07:002016-09-08T04:57:28.726-07:00There is no way to judge a person based on this qu...There is no way to judge a person based on this question. These type of questions are way too common and readily available on net. Its just chance and not skill. I got this swap integer question asked today. Had I looked in one of these interview guides, I would have easily answered it in a 3 to 4 mins spell. And the interviewer would have thought 'Oh this guy has good analytical skills'.Observerhttps://www.blogger.com/profile/14726548622592285184noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-65660282890978806862016-08-14T07:19:24.591-07:002016-08-14T07:19:24.591-07:00They only work for integers, and the one that uses...They only work for integers, and the one that uses division fails for b=0. As someone noted, it also produces more code than a regular swap via temp variable, so it's useless.<br />Ereshttps://www.blogger.com/profile/00785957637998003967noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-11974833961247382022016-06-19T15:26:23.495-07:002016-06-19T15:26:23.495-07:00I had this question asked in the first programming...I had this question asked in the first programming class I ever took - we weren't even learning a language, we just created flowcharts of a solution. The question as I heard it was output 2 values in ascending order without using a temp variable. The question is not about language skill, it is about thinking skill, so bitwise operators and multiplication and all the rest are not really the rpbarbatihttps://www.blogger.com/profile/04645764973907648320noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-27056785272786326892016-05-15T05:07:01.591-07:002016-05-15T05:07:01.591-07:00Toun, your logic will not work,
As you said: a =...Toun, your logic will not work,<br />As you said: a = b + 0 * (b = a); <br />Let : a=2, b=3<br />1st execute (b=a); //b=2<br />then a = b + 0 * (b = a); <br /> a = 2 + 0 * 2; <br /> a=2;<br /><br />so here, value of "b" will bi swap but value of "a" remain as it is.<br />Tech for Boosthttps://www.blogger.com/profile/16225366179456140319noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-58699801676240079282016-03-09T10:49:10.688-08:002016-03-09T10:49:10.688-08:00Using bitwise operator(^) is better in performance...Using bitwise operator(^) is better in performance wise. so go for bitwise operatorAnonymoushttps://www.blogger.com/profile/08290229502317274376noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-74754559715314893832016-02-15T05:28:41.006-08:002016-02-15T05:28:41.006-08:00Suppose I want to swap i and a[i] in an integer ar...Suppose I want to swap i and a[i] in an integer array<br />temp=i<br />i = a[i]<br />a[i] = temp<br />would work nicely.<br />Would the following give the effect that I want to?<br />=====<br />a[i] = a[i] + i<br />i = a[i] - i<br />a[i] = a[i] - i<br />=========<br /><br />Above All : I don't see any disadvantage in using temp that forms a universal solution to any primitive types.Dr S Mahesanhttps://www.blogger.com/profile/11152151129420066014noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-48405960452880473682016-02-15T05:12:46.457-08:002016-02-15T05:12:46.457-08:00I have a couple of questions on this:
First : Wha...I have a couple of questions on this:<br />First : What's wrong in using temp for swapping? What's the advantage do we have without using temp?<br />The question is about swapping two numbers; but the answers are given for integers. Except for the first one, would the others work for numbers of type float/double?<br />Dr S Mahesanhttps://www.blogger.com/profile/11152151129420066014noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-29451056281694552182016-01-29T15:21:39.889-08:002016-01-29T15:21:39.889-08:00Does this work too?
a^=b^=a^=b
Sorry, i'm new ...Does this work too?<br />a^=b^=a^=b<br />Sorry, i'm new to java.Anonymoushttps://www.blogger.com/profile/09988299231262015137noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-39981074866985945822015-11-07T06:29:09.418-08:002015-11-07T06:29:09.418-08:00how can we swap two characters without using 3rd v...how can we swap two characters without using 3rd variable?????Anonymoushttps://www.blogger.com/profile/08641381675386718463noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-25997288416910127142015-07-15T07:15:35.694-07:002015-07-15T07:15:35.694-07:00@Anonymous, you have summed it absolutely correct....@Anonymous, you have summed it absolutely correct. I like one or two such interview question everytime, something which is new to candidate and gives him an opportunity to apply his knowledge in totally new problem.javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-4991303327014129492015-07-13T16:23:26.223-07:002015-07-13T16:23:26.223-07:00The title of the article says NUMBERS not STRINGS....The title of the article says NUMBERS not STRINGS.aimhttps://www.blogger.com/profile/14487826827167121174noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-69654947409217043342015-06-25T08:36:37.444-07:002015-06-25T08:36:37.444-07:00The purpose of these type of questions is only to ...The purpose of these type of questions is only to test how you will perform when u r put in a situation that is outside your comfort zone !!! Will you panic? Will you revolt ? Will you remain indifferent ? or will you show leadership skills to address the situation ??? Thats about it dude ..Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-67247430141153287352015-04-25T17:23:54.102-07:002015-04-25T17:23:54.102-07:00IIRC, while it's probably pointless on modern ...IIRC, while it's probably pointless on modern hardware, this used to be a way to save space when memory or registers were at a premium (ex. embedded systems, early computers with a few hundred bytes of RAM, etc.). Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-29892164830084810232014-12-14T18:51:26.204-08:002014-12-14T18:51:26.204-08:00Method 2 is the best method. It uses very little ...Method 2 is the best method. It uses very little space & is very fast. Method 3 will give a runtime error if b=0.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-61028225681994939692014-04-12T10:29:52.442-07:002014-04-12T10:29:52.442-07:00As long as both String a & String b are not nu...As long as both String a & String b are not null. we can use the following to swap them:<br /><br />b = a + (a = b).substring(0, 0);<br /><br />Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-10690665055737454232014-02-06T04:25:17.860-08:002014-02-06T04:25:17.860-08:00If some interviewer asks me this question I will q...If some interviewer asks me this question I will quit that interview and leave. This is so unreal and so academic if you are not hiring for some very specialised position, not to mention all that problems with overflow, performance etc... <br /><br />My brain is activelly refusing to waste precious time with problems like this and from my > 10 years programming experience this type of questionbrkerezhttps://www.blogger.com/profile/16892731112543528696noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-33948921972311309112013-11-12T03:04:40.192-08:002013-11-12T03:04:40.192-08:00From a performance point of view, there is not muc...From a performance point of view, there is not much merit in doing a swap without using a third variable. If you study the bytecode that's generated for each of the methods, you will probably find that using a temporary variable generates the least number of bytecode instructions (6 as opposed to 12 or more). So, if you have something like this within a tight loop, use a third variable. WhileVicegriphttps://www.blogger.com/profile/14743140707827554921noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-62539936811067962862013-11-07T09:00:03.134-08:002013-11-07T09:00:03.134-08:00a = b + 0 * (b = a);a = b + 0 * (b = a);Tounhttps://www.blogger.com/profile/10824676117790501369noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-82042178019648859162013-09-21T13:21:49.967-07:002013-09-21T13:21:49.967-07:00@Ruks Shetty
String a = "abcdefghijkl...@Ruks Shetty<br /> String a = "abcdefghijklmno";<br /> String b = "123456787654321";<br /><br /> b = a + b; <br /> a = b.substring(a.length());<br /> b = b.substring(0, b.indexOf(a));ruksofthttps://www.blogger.com/profile/14397066768399079613noreply@blogger.com