Replace every element with next greatest element (in ascending order, not replacing with -1)
Currently working on an algorithm that replaces every element with the next greatest element, but unlike some other questions here, this one is not concerned with replacing values with -1 if no such number exists, and it must be in ascending order.
Given this input: {1, 5, -3, 2, 8, 4, 7, 10, 3, 11, 2 }
Have to get this output: 1 5 5 5 8 8 8 10 10 11 11
This is what I have so far:
class Playground {
static void nextGreatest(int arr) {
int size = arr.length;
// Initialize the next greatest element
int max_from_right = arr[size - 1];
// Replace all other elements with the next greatest
for (int i = 1; i < size; i++)
{
// Store the current element (needed later for
// updating the next greatest element)
int temp = arr[i];
// Replace current element with the next greatest
arr[i] = max_from_right;
// Update the greatest element, if needed
if(max_from_right < temp) {
max_from_right = temp;
}
}
}
// prints the array
static void printArray(int arr)
{
for (int i=0; i < arr.length; i++)
System.out.print(arr[i]+" ");
}
public static void main (String args) {
int arr = {1, 5, -3, 2, 8, 4, 7, 10, 3, 11, 2 };
nextGreatest (arr);
printArray (arr);
}
}
And I get the following right now:
1 2 5 5 5 8 8 8 10 10 11
Thoughts?
Thanks
java arrays algorithm dynamic-programming
|
show 8 more comments
Currently working on an algorithm that replaces every element with the next greatest element, but unlike some other questions here, this one is not concerned with replacing values with -1 if no such number exists, and it must be in ascending order.
Given this input: {1, 5, -3, 2, 8, 4, 7, 10, 3, 11, 2 }
Have to get this output: 1 5 5 5 8 8 8 10 10 11 11
This is what I have so far:
class Playground {
static void nextGreatest(int arr) {
int size = arr.length;
// Initialize the next greatest element
int max_from_right = arr[size - 1];
// Replace all other elements with the next greatest
for (int i = 1; i < size; i++)
{
// Store the current element (needed later for
// updating the next greatest element)
int temp = arr[i];
// Replace current element with the next greatest
arr[i] = max_from_right;
// Update the greatest element, if needed
if(max_from_right < temp) {
max_from_right = temp;
}
}
}
// prints the array
static void printArray(int arr)
{
for (int i=0; i < arr.length; i++)
System.out.print(arr[i]+" ");
}
public static void main (String args) {
int arr = {1, 5, -3, 2, 8, 4, 7, 10, 3, 11, 2 };
nextGreatest (arr);
printArray (arr);
}
}
And I get the following right now:
1 2 5 5 5 8 8 8 10 10 11
Thoughts?
Thanks
java arrays algorithm dynamic-programming
I don't think the DP tag is appropriate. This is greedy.
– ggorlen
Nov 28 '18 at 18:35
What does "next greatest element" mean, here?
– David Conrad
Nov 28 '18 at 18:36
1
What must be the output for1,2,100,6,7? Is it1,2,100,100,100?
– user7
Nov 28 '18 at 18:36
1
@David Contrad refers to the value of the next element in the array. If it's greater than the one before it, it replaces every value after that, until another higher value is reached.
– Mark T.
Nov 28 '18 at 18:40
1
I see. BTW, you might be able to replaceprintArraywithArrays.toString(fromjava.util) unless the exact output format is critical.
– David Conrad
Nov 28 '18 at 18:42
|
show 8 more comments
Currently working on an algorithm that replaces every element with the next greatest element, but unlike some other questions here, this one is not concerned with replacing values with -1 if no such number exists, and it must be in ascending order.
Given this input: {1, 5, -3, 2, 8, 4, 7, 10, 3, 11, 2 }
Have to get this output: 1 5 5 5 8 8 8 10 10 11 11
This is what I have so far:
class Playground {
static void nextGreatest(int arr) {
int size = arr.length;
// Initialize the next greatest element
int max_from_right = arr[size - 1];
// Replace all other elements with the next greatest
for (int i = 1; i < size; i++)
{
// Store the current element (needed later for
// updating the next greatest element)
int temp = arr[i];
// Replace current element with the next greatest
arr[i] = max_from_right;
// Update the greatest element, if needed
if(max_from_right < temp) {
max_from_right = temp;
}
}
}
// prints the array
static void printArray(int arr)
{
for (int i=0; i < arr.length; i++)
System.out.print(arr[i]+" ");
}
public static void main (String args) {
int arr = {1, 5, -3, 2, 8, 4, 7, 10, 3, 11, 2 };
nextGreatest (arr);
printArray (arr);
}
}
And I get the following right now:
1 2 5 5 5 8 8 8 10 10 11
Thoughts?
Thanks
java arrays algorithm dynamic-programming
Currently working on an algorithm that replaces every element with the next greatest element, but unlike some other questions here, this one is not concerned with replacing values with -1 if no such number exists, and it must be in ascending order.
Given this input: {1, 5, -3, 2, 8, 4, 7, 10, 3, 11, 2 }
Have to get this output: 1 5 5 5 8 8 8 10 10 11 11
This is what I have so far:
class Playground {
static void nextGreatest(int arr) {
int size = arr.length;
// Initialize the next greatest element
int max_from_right = arr[size - 1];
// Replace all other elements with the next greatest
for (int i = 1; i < size; i++)
{
// Store the current element (needed later for
// updating the next greatest element)
int temp = arr[i];
// Replace current element with the next greatest
arr[i] = max_from_right;
// Update the greatest element, if needed
if(max_from_right < temp) {
max_from_right = temp;
}
}
}
// prints the array
static void printArray(int arr)
{
for (int i=0; i < arr.length; i++)
System.out.print(arr[i]+" ");
}
public static void main (String args) {
int arr = {1, 5, -3, 2, 8, 4, 7, 10, 3, 11, 2 };
nextGreatest (arr);
printArray (arr);
}
}
And I get the following right now:
1 2 5 5 5 8 8 8 10 10 11
Thoughts?
Thanks
java arrays algorithm dynamic-programming
java arrays algorithm dynamic-programming
asked Nov 28 '18 at 18:32
Mark T.Mark T.
174
174
I don't think the DP tag is appropriate. This is greedy.
– ggorlen
Nov 28 '18 at 18:35
What does "next greatest element" mean, here?
– David Conrad
Nov 28 '18 at 18:36
1
What must be the output for1,2,100,6,7? Is it1,2,100,100,100?
– user7
Nov 28 '18 at 18:36
1
@David Contrad refers to the value of the next element in the array. If it's greater than the one before it, it replaces every value after that, until another higher value is reached.
– Mark T.
Nov 28 '18 at 18:40
1
I see. BTW, you might be able to replaceprintArraywithArrays.toString(fromjava.util) unless the exact output format is critical.
– David Conrad
Nov 28 '18 at 18:42
|
show 8 more comments
I don't think the DP tag is appropriate. This is greedy.
– ggorlen
Nov 28 '18 at 18:35
What does "next greatest element" mean, here?
– David Conrad
Nov 28 '18 at 18:36
1
What must be the output for1,2,100,6,7? Is it1,2,100,100,100?
– user7
Nov 28 '18 at 18:36
1
@David Contrad refers to the value of the next element in the array. If it's greater than the one before it, it replaces every value after that, until another higher value is reached.
– Mark T.
Nov 28 '18 at 18:40
1
I see. BTW, you might be able to replaceprintArraywithArrays.toString(fromjava.util) unless the exact output format is critical.
– David Conrad
Nov 28 '18 at 18:42
I don't think the DP tag is appropriate. This is greedy.
– ggorlen
Nov 28 '18 at 18:35
I don't think the DP tag is appropriate. This is greedy.
– ggorlen
Nov 28 '18 at 18:35
What does "next greatest element" mean, here?
– David Conrad
Nov 28 '18 at 18:36
What does "next greatest element" mean, here?
– David Conrad
Nov 28 '18 at 18:36
1
1
What must be the output for
1,2,100,6,7? Is it 1,2,100,100,100?– user7
Nov 28 '18 at 18:36
What must be the output for
1,2,100,6,7? Is it 1,2,100,100,100?– user7
Nov 28 '18 at 18:36
1
1
@David Contrad refers to the value of the next element in the array. If it's greater than the one before it, it replaces every value after that, until another higher value is reached.
– Mark T.
Nov 28 '18 at 18:40
@David Contrad refers to the value of the next element in the array. If it's greater than the one before it, it replaces every value after that, until another higher value is reached.
– Mark T.
Nov 28 '18 at 18:40
1
1
I see. BTW, you might be able to replace
printArray with Arrays.toString (from java.util) unless the exact output format is critical.– David Conrad
Nov 28 '18 at 18:42
I see. BTW, you might be able to replace
printArray with Arrays.toString (from java.util) unless the exact output format is critical.– David Conrad
Nov 28 '18 at 18:42
|
show 8 more comments
2 Answers
2
active
oldest
votes
(Not sure I understand your question exactly, but based on the clarification from the comments here is my answer...)
Seems you just need to change the initial max initialization to the first element rather than the last element.
int currentMax = arr[0];
for (int i = 1; i < size; i++) {
int temp = arr[i];
arr[i] = currentMax;
if(currentMax < temp) {
currentMax = temp;
}
}
The solution ends up as For each index i, what is the maximum element seen so far.
Yikes, I see it now. Thanks so much!
– Mark T.
Nov 28 '18 at 18:46
add a comment |
Consider the following:
int currMax = -1;
int input = {1, 5, -3, 2, 8, 4, 7, 10, 3, 11, 2};
for (int i = 0; i < input.length; i++){
if (input[i] > currMax){ // if we find a new max element
currMax = input[i];
}
else if (input[i] < currMax){ // if value is less then max we replace it
input[i] = currMax;
}
}
System.out.println(Arrays.toString(input));
> [1, 5, 5, 5, 8, 8, 8, 10, 10, 11, 11]
The question is tagged as Java
– user7
Nov 28 '18 at 18:38
Thanks for the help!
– Mark T.
Nov 28 '18 at 18:46
Sorry about the mix up! Edited to Java accordingly
– M.G
Nov 28 '18 at 18:47
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53525932%2freplace-every-element-with-next-greatest-element-in-ascending-order-not-replac%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
(Not sure I understand your question exactly, but based on the clarification from the comments here is my answer...)
Seems you just need to change the initial max initialization to the first element rather than the last element.
int currentMax = arr[0];
for (int i = 1; i < size; i++) {
int temp = arr[i];
arr[i] = currentMax;
if(currentMax < temp) {
currentMax = temp;
}
}
The solution ends up as For each index i, what is the maximum element seen so far.
Yikes, I see it now. Thanks so much!
– Mark T.
Nov 28 '18 at 18:46
add a comment |
(Not sure I understand your question exactly, but based on the clarification from the comments here is my answer...)
Seems you just need to change the initial max initialization to the first element rather than the last element.
int currentMax = arr[0];
for (int i = 1; i < size; i++) {
int temp = arr[i];
arr[i] = currentMax;
if(currentMax < temp) {
currentMax = temp;
}
}
The solution ends up as For each index i, what is the maximum element seen so far.
Yikes, I see it now. Thanks so much!
– Mark T.
Nov 28 '18 at 18:46
add a comment |
(Not sure I understand your question exactly, but based on the clarification from the comments here is my answer...)
Seems you just need to change the initial max initialization to the first element rather than the last element.
int currentMax = arr[0];
for (int i = 1; i < size; i++) {
int temp = arr[i];
arr[i] = currentMax;
if(currentMax < temp) {
currentMax = temp;
}
}
The solution ends up as For each index i, what is the maximum element seen so far.
(Not sure I understand your question exactly, but based on the clarification from the comments here is my answer...)
Seems you just need to change the initial max initialization to the first element rather than the last element.
int currentMax = arr[0];
for (int i = 1; i < size; i++) {
int temp = arr[i];
arr[i] = currentMax;
if(currentMax < temp) {
currentMax = temp;
}
}
The solution ends up as For each index i, what is the maximum element seen so far.
edited Nov 28 '18 at 18:46
answered Nov 28 '18 at 18:40
user7user7
9,70432546
9,70432546
Yikes, I see it now. Thanks so much!
– Mark T.
Nov 28 '18 at 18:46
add a comment |
Yikes, I see it now. Thanks so much!
– Mark T.
Nov 28 '18 at 18:46
Yikes, I see it now. Thanks so much!
– Mark T.
Nov 28 '18 at 18:46
Yikes, I see it now. Thanks so much!
– Mark T.
Nov 28 '18 at 18:46
add a comment |
Consider the following:
int currMax = -1;
int input = {1, 5, -3, 2, 8, 4, 7, 10, 3, 11, 2};
for (int i = 0; i < input.length; i++){
if (input[i] > currMax){ // if we find a new max element
currMax = input[i];
}
else if (input[i] < currMax){ // if value is less then max we replace it
input[i] = currMax;
}
}
System.out.println(Arrays.toString(input));
> [1, 5, 5, 5, 8, 8, 8, 10, 10, 11, 11]
The question is tagged as Java
– user7
Nov 28 '18 at 18:38
Thanks for the help!
– Mark T.
Nov 28 '18 at 18:46
Sorry about the mix up! Edited to Java accordingly
– M.G
Nov 28 '18 at 18:47
add a comment |
Consider the following:
int currMax = -1;
int input = {1, 5, -3, 2, 8, 4, 7, 10, 3, 11, 2};
for (int i = 0; i < input.length; i++){
if (input[i] > currMax){ // if we find a new max element
currMax = input[i];
}
else if (input[i] < currMax){ // if value is less then max we replace it
input[i] = currMax;
}
}
System.out.println(Arrays.toString(input));
> [1, 5, 5, 5, 8, 8, 8, 10, 10, 11, 11]
The question is tagged as Java
– user7
Nov 28 '18 at 18:38
Thanks for the help!
– Mark T.
Nov 28 '18 at 18:46
Sorry about the mix up! Edited to Java accordingly
– M.G
Nov 28 '18 at 18:47
add a comment |
Consider the following:
int currMax = -1;
int input = {1, 5, -3, 2, 8, 4, 7, 10, 3, 11, 2};
for (int i = 0; i < input.length; i++){
if (input[i] > currMax){ // if we find a new max element
currMax = input[i];
}
else if (input[i] < currMax){ // if value is less then max we replace it
input[i] = currMax;
}
}
System.out.println(Arrays.toString(input));
> [1, 5, 5, 5, 8, 8, 8, 10, 10, 11, 11]
Consider the following:
int currMax = -1;
int input = {1, 5, -3, 2, 8, 4, 7, 10, 3, 11, 2};
for (int i = 0; i < input.length; i++){
if (input[i] > currMax){ // if we find a new max element
currMax = input[i];
}
else if (input[i] < currMax){ // if value is less then max we replace it
input[i] = currMax;
}
}
System.out.println(Arrays.toString(input));
> [1, 5, 5, 5, 8, 8, 8, 10, 10, 11, 11]
edited Nov 28 '18 at 18:49
answered Nov 28 '18 at 18:37
M.GM.G
388310
388310
The question is tagged as Java
– user7
Nov 28 '18 at 18:38
Thanks for the help!
– Mark T.
Nov 28 '18 at 18:46
Sorry about the mix up! Edited to Java accordingly
– M.G
Nov 28 '18 at 18:47
add a comment |
The question is tagged as Java
– user7
Nov 28 '18 at 18:38
Thanks for the help!
– Mark T.
Nov 28 '18 at 18:46
Sorry about the mix up! Edited to Java accordingly
– M.G
Nov 28 '18 at 18:47
The question is tagged as Java
– user7
Nov 28 '18 at 18:38
The question is tagged as Java
– user7
Nov 28 '18 at 18:38
Thanks for the help!
– Mark T.
Nov 28 '18 at 18:46
Thanks for the help!
– Mark T.
Nov 28 '18 at 18:46
Sorry about the mix up! Edited to Java accordingly
– M.G
Nov 28 '18 at 18:47
Sorry about the mix up! Edited to Java accordingly
– M.G
Nov 28 '18 at 18:47
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53525932%2freplace-every-element-with-next-greatest-element-in-ascending-order-not-replac%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I don't think the DP tag is appropriate. This is greedy.
– ggorlen
Nov 28 '18 at 18:35
What does "next greatest element" mean, here?
– David Conrad
Nov 28 '18 at 18:36
1
What must be the output for
1,2,100,6,7? Is it1,2,100,100,100?– user7
Nov 28 '18 at 18:36
1
@David Contrad refers to the value of the next element in the array. If it's greater than the one before it, it replaces every value after that, until another higher value is reached.
– Mark T.
Nov 28 '18 at 18:40
1
I see. BTW, you might be able to replace
printArraywithArrays.toString(fromjava.util) unless the exact output format is critical.– David Conrad
Nov 28 '18 at 18:42