Problem:
Return a version of the given array where each zero value in the array is replaced by the largest odd value to the right of the zero in the array. If there is no odd value to the right of the zero, leave the zero as a zero.
zeroMax({0, 5, 0, 3}) → {5, 5, 3, 3}
zeroMax({0, 4, 0, 3}) → {3, 4, 3, 3}
zeroMax({0, 1, 0}) → {1, 1, 0}
Solution:
public int[] zeroMax(int[] nums) { int max = 0; for (int j =0; j < nums.length -1;j++) { if (nums[j] == 0) { for (int i = j + 1; i <=nums.length -1;i++) { if ( nums[i] > max && nums[i] % 2 == 1 ) max = nums[i]; } nums[j] = max; max = 0; } } return nums; }
u must do it using one loop
ReplyDeletepublic static int[] sameEnds(int[] nums) {
ReplyDeleteint max = 0;
for (int i = nums.length-1; i >= 0; i--) {
if (nums[i] % 2 != 0)
max = Math.max(max, nums[i]);
if (nums[i] == 0)
nums[i] = max;
}
return nums;
}
}
more easy logic---
ReplyDeletepublic int[] zeroMax(int[] nums) {
int max = 0;
for (int i = nums.length - 1; i >= 0; i--) {
if(nums[i]!=0 && nums[i]%2==1 && max<nums[i]){
max=nums[i];
}
if(nums[i]==0){
nums[i]=max;
}
}
return nums;
}
public int[] zeroMax(int[] nums) {
ReplyDeleteint index = 0;
for (int i = nums.length - 1; i >= 0; i--) {
if (nums[i] % 2 != 0) {
index = index < nums[i] ? nums[i] : index;
}
if (nums[i] == 0) {
nums[i] = index;
}
}
return nums;
public int[] zeroMax(int[] nums) {
ReplyDeleteint x = 0;
for (int i = 0; i < nums.length; i++){
if (nums[i] == 0){
for (int j = i; j < nums.length; j++){
if (nums[j]%2 == 1){
if (nums[j] > x){
x = nums[j];
}
}
}
nums[i] = x;
x = 0;
}
}
return nums;
}
public int[] zeroMax(int[] nums) {
ReplyDeleteint max =0;
for(int i=0 ; i < nums.length ; i++){
if(nums[i]==0){
for(int j=i+1 ; j < nums.length ; j++){
if(nums[j]%2!=0 &&nums[j]>=max )max=nums[j]; // لو فردي واكبر من اكبر قيمه فردية
}
nums[i]=max; // حط أكبر قيمة فردية مكان الصفر
max=0; // رجع اكبر قيمه فردية صفرتاني عشان انا معرفها بره اللوب فعشان متفضلش زي المرة القديمة
}}
return nums;
}
----------------
public int[] zeroMax(int[] nums) {
int max = 0;
for (int i = nums.length-1; i >= 0; i--) {
if (nums[i] % 2 != 0)
max = Math.max(max, nums[i]);
if (nums[i] == 0)
nums[i] = max;
}
return nums;
}
public int[] zeroMax(int[] nums)
ReplyDelete{
int counter = 0;
for(int i = 0; i < nums.length - 1; i++)
{
if(nums[i] == 0)
{
for(int j = i + 1; j < nums.length; j++)
{
if(nums[j] % 2 != 0 && nums[j] > counter)
{
counter = nums[j];
}
}
nums[i] = counter;
counter = 0;
}
}
return nums;
}
public static int[] zeroMax(int[] ar ){
ReplyDeleteint j=0,n=1;
for(int i=0;i<ar.length;i++)
{
if(ar[i]==0)
{
if((ar[i+n])%2==0)
{ while((ar[i+n])%2==0)
{
n++;
}
}
else
{
j=ar[i+n];
}
}
else
{
continue;
}
ar[i]=j;
}
return ar;
}
#saptharishee
Deletepublic int[] zeroMax(int[] nums) {
ReplyDeleteint temp;
for(int i=0;i<nums.length;i++){
temp=0;
if(nums[i]==0){
for(int j=i+1;j<nums.length;j++){
if(nums[j]%2==1){
temp=(temp<nums[j])?nums[j]:temp;
}
}
nums[i]=temp;
}
}
return nums;
}
public int[] zeroMax(int[] nums) {
ReplyDeleteint odd=0;
for(int i=nums.length-1;i>=0;i--){
if(nums[i]!=0){
if(nums[i]%2==1&&odd<nums[i]){
odd=nums[i];
}
}
if(nums[i]==0&&i!=nums.length-1){
nums[i]=odd;
}
}
return nums;
}