• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Same logic but different output

 
Ranch Hand
Posts: 94
1
Oracle Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was trying to solve a programming puzzle here, and i wrote to codes with same logic but different data structures. They are as follow.



and



The second code where i use ArrayList worked only for two test cases but the one where i used arrays worked for all cases.Here are the outupts of my codes output1 and output2 respectively. Can anyone please explain why this is happening even when i am using the same logic.
 
Saloon Keeper
Posts: 15490
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're not performing the same logic. p.get(i) returns an Integer, not an int. In your first program you perform int comparison, in your second program you perform object comparison. For values outside the range -128 to 127, == will return false for two different instances of Integer, even if they have the same value.

The solution is to use equals() instead of ==.
 
Stephan van Hulst
Saloon Keeper
Posts: 15490
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And here's my solution:
 
Rrohit rakesh upadhyay
Ranch Hand
Posts: 94
1
Oracle Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Stephan , can you explain the working of line 15 and 17 of your code.
 
Stephan van Hulst
Saloon Keeper
Posts: 15490
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The map occurrences keeps track how much each number occurs in the array. The loop containing line 15 fills this map. The merge function is a convenient way of setting the number of occurrences to 1 if the number hasn't been encountered before, and increments the count by 1 if it has been encountered before.

Regardless of where a number occurs in the array, the number of pairs of equal numbers for a specific number only depends on how many times that specific number occurs: It doesn't matter if the array holds [1, 1, 2] or [1, 2, 1]; the number of pairs you can make with a 1 is the same: 3.

1 occurrence means 1 pair.
2 occurrences mean 3 pairs.
3 occurrences mean 6 pairs.
4 occurrences mean 10 pairs.

1, 3, 6, 10... etc. are called Triangle Numbers. An easy way to calculate the nth triangle number is with the formula n(n+1)/2.

So what line 18 does is stream the values (the number of occurrences of the key) of the map, for each number of occurrence x, transform it to the number of pairs using the triangle number formula, and finally sum all the numbers of pairs.
 
Stephan van Hulst
Saloon Keeper
Posts: 15490
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I updated my code to make some of the names more clear.
 
Rrohit rakesh upadhyay
Ranch Hand
Posts: 94
1
Oracle Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much Stephan. I am sorry for my late reply
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic