Win a copy of Java by Comparison (eBook) this week in the Java in General forum!
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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Simple Problem

Greenhorn
Posts: 2

Hi friends
I am new to Java and I am stuck up with two basic logic.

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.

Saloon Keeper
Posts: 1752
44
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
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.

Bartender
Posts: 11445
18

Sweet Sweetie wrote:

salvin francis
Saloon Keeper
Posts: 1752
44

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
Saloon Keeper
Posts: 1752
44
have you cracked it in code ?

Sheriff
Posts: 57797
178
Thank you for correcting the naming error. Please have a look at the title of this thread, which doesn't inspire answers.