• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Simple Problem

 
Meena Singh
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi friends
I am new to Java and I am stuck up with two basic logic.
Can you please help me in explaining the logic behind them.

Problem No 1)

Return an array that contains the exact same numbers as the given array, but rearranged so that all the zeros are grouped at the start of the array. The order of the non-zero numbers does not matter. So {1, 0, 0, 1} becomes {0 ,0, 1, 1}. You may modify and return the given array or make a new array.

zero Front({1, 0, 0, 1}) → {0, 0, 1, 1}
zero Front({0, 1, 1, 0, 1}) → {0, 0, 1, 1, 1}
zero Front({1, 0}) → {0, 1}

Problem2)
Write a program to print the following on screen
1
1 1
1 1 1
1 1 1 1
1 1 1 1


I don't want anybody to tell the code only beacause that I can find it on net. But somebody please explain the main logic behind it.
 
salvin francis
Bartender
Posts: 1263
10
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I advise you to change your id to a real name.
Though you are not asking for code, it seems you want us to do your homework for you,

I promise to help you if you mention the though process in your mind (even mathematically) to solve this problem.

Welcome to code ranch.
 
Meena Singh
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Salvin
I am not asking you to do my homework but it is because I have never been able to think at this level that is why I wanted somebody to explain me. Though 2nd problem is the first level towards what I want to achieve which is
A B C D E F G F E D C B A
A B C D E F F E D C B A
A B C D E E D C B A
A B C D D C B A
A B C C B A
A B B A
A A

though I got this code through internet but could not figure out the logic behind it so I started with the basic one to develop 1 111...
I am stuck as to I could not figure out logic behind this also.
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sweet Sweetie wrote:

Please check your private messages for an important administrative matter
 
salvin francis
Bartender
Posts: 1263
10
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Logic 1:
{1, 0, 0, 1}

look at first element,
its a 0 ? no, do nothing,
look at second element,
its a 0 ? yes, move it to start ....

generalised form:

look at nth element, if its a 0 move it to start

iterations:
1. {1, 0, 0, 1}
2. {0, 1, 0, 1}
3. {0, 0, 1, 1}
4. {0, 0, 1, 1}


Logic 2:



first time i print "1" one time
second time i print "1" two times...
nth time, i print "1" n times


Meena Singh wrote:Hi Salvin
I am not asking you to do my homework but it is because I have never been able to think at this level that is why I wanted somebody to explain me. Though 2nd problem is the first level towards what I want to achieve which is


though I got this code through internet but could not figure out the logic behind it so I started with the basic one to develop 1 111...
I am stuck as to I could not figure out logic behind this also.
 
salvin francis
Bartender
Posts: 1263
10
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
have you cracked it in code ?
 
Campbell Ritchie
Sheriff
Posts: 48374
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for correcting the naming error. Please have a look at the title of this thread, which doesn't inspire answers.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic