Coding Question asked in Apple Inc Interview | Apple client | TCS

preview_player
Показать описание
This video contains arrays coding question that was asked as part of apple client round.
logic pinged in the comment.

One of the best book for Interview Questions

📌 Do Subscribe for all Updates.

Telegram : Drunken Engineer

You can follow us on Facebook :

#apple #codingquestions #leetcode #leetcodequestionandanswers
Рекомендации по теме
Комментарии
Автор

int size = arr.length;

// Initialize the next greatest element
int max_from_right = arr[size-1];

// The next greatest element for the rightmost
// element is always -1
arr[size-1] = -1;

// Replace all other elements with the next greatest
for (int i = size-2; i >= 0; i--)
{
// Store the current element (needed later for
// updating the next greatest element)
int temp = arr[i];

// Replace current element with the next greatest
if(max_from_right>arr[i]) {
arr[i] = max_from_right;
}else {
arr[i] = -1;
}


// Update the greatest element, if needed
if(max_from_right < temp)
max_from_right = temp;

//System.out.println();
}

DrunkenEngineer
Автор

Same question asked to me in one of the interview.

letstry
Автор

Thank you so much for your great and clear explanation. I request to please make more vedeos, Actually same questions asked in my previous interview,
i am unable to answer, after watching your vedeo i can able to learn,
Thanks a lot

rachamallidorasrivignesh
Автор

Another Approach:
Integer numArrays[] = { 8, 9, 5, 11, 6, 1, 7, 6 };
for (int i = 0; i < numArrays.length; i++) {
int max = numArrays[i] ;
for (int j = i + 1; j < numArrays.length; j++) {
if (numArrays[j] > max)
= numArrays[j];
}
numArrays[i] = numArrays[i] != max ? max ? -1 ;
}

jeckrazi
Автор

this code is incorrect, and you can check for input {2, 3, 4, 12};.. the output we are getting as per this code is {12, 12, 12, -1};... whereas the output should have been {3, 4, 12, -1};
basically you are just computing the largest element, and not the greater element present at RHS of an element !!

DurgaShiva
Автор

Simple alternative:

int arr1[] = { 8, 9, 5, 11, 6, 1, 7, 6 };
// output = 11, 11, 11, -1, 7, 7, -1, -1

int max1 = 0;

for (int i = arr1.length - 1; i >= 0; i--) {
if (arr1[i] > max1) {
max1 = arr1[i];
arr1[i] = -1;
} else {
arr1[i] = max1;
}
}

// check

hayden