Find nearest smallest element of left side in array
Here given code implementation process.
// C Program
// Find nearest smallest element of left side in array
#include <stdio.h>
// Find the smallest element of left side in given array
void leftSmallest(int arr[], int size)
{
// Loop controlling variables
int i = 0;
int j = 0;
// Define auxiliary variable
int location = -1;
// Outer loop (0...size)
for (i = 0; i < size; ++i)
{
location = -1;
for (j = i - 1; j > 0 && location == -1; --j)
{
if (arr[i] > arr[j])
{
// When get smaller element
location = j;
}
}
if (location == -1)
{
printf(" [%d] : None \n", arr[i]);
}
else
{
printf(" [%d] : %d \n", arr[i], arr[location]);
}
}
}
int main(int argc, char
const * argv[])
{
// Given array elements
int arr[] = {
6 , 3 , 7 , 5 , 12 , 10 , 6 , 2 , 4
};
// Get the size
int size = sizeof(arr) / sizeof(arr[0]);
leftSmallest(arr, size);
return 0;
}
Output
[6] : None
[3] : None
[7] : 3
[5] : 3
[12] : 5
[10] : 5
[6] : 5
[2] : None
[4] : 2
/*
Java program
Find nearest smallest element of left side in array
*/
public class Element
{
// Find the smallest element of left side in given array
public void leftSmallest(int[] arr, int size)
{
// Loop controlling variables
int i = 0;
int j = 0;
// Define auxiliary variable
int location = -1;
// Outer loop (0...size)
for (i = 0; i < size; ++i)
{
location = -1;
for (j = i - 1; j > 0 && location == -1; --j)
{
if (arr[i] > arr[j])
{
// When get smaller element
location = j;
}
}
if (location == -1)
{
System.out.print(" [" + arr[i] + "] : None \n");
}
else
{
System.out.print(" [" + arr[i] + "] : " + arr[location] + " \n");
}
}
}
public static void main(String[] args)
{
Element task = new Element();
// Given array elements
int[] arr = {
6 , 3 , 7 , 5 , 12 , 10 , 6 , 2 , 4
};
// Get the number of elements
int size = arr.length;
task.leftSmallest(arr, size);
}
}
Output
[6] : None
[3] : None
[7] : 3
[5] : 3
[12] : 5
[10] : 5
[6] : 5
[2] : None
[4] : 2
// Include header file
#include <iostream>
using namespace std;
/*
C++ program
Find nearest smallest element of left side in array
*/
class Element
{
public:
// Find the smallest element of left side in given array
void leftSmallest(int arr[], int size)
{
// Loop controlling variables
int i = 0;
int j = 0;
// Define auxiliary variable
int location = -1;
// Outer loop (0...size)
for (i = 0; i < size; ++i)
{
location = -1;
for (j = i - 1; j > 0 && location == -1; --j)
{
if (arr[i] > arr[j])
{
// When get smaller element
location = j;
}
}
if (location == -1)
{
cout << " [" << arr[i] << "] : None \n";
}
else
{
cout << " [" << arr[i] << "] : " << arr[location] << " \n";
}
}
}
};
int main()
{
Element task = Element();
// Given array elements
int arr[] = {
6 , 3 , 7 , 5 , 12 , 10 , 6 , 2 , 4
};
// Get the number of elements
int size = sizeof(arr) / sizeof(arr[0]);
task.leftSmallest(arr, size);
return 0;
}
Output
[6] : None
[3] : None
[7] : 3
[5] : 3
[12] : 5
[10] : 5
[6] : 5
[2] : None
[4] : 2
// Include namespace system
using System;
/*
C# program
Find nearest smallest element of left side in array
*/
public class Element
{
// Find the smallest element of left side in given array
public void leftSmallest(int[] arr, int size)
{
// Loop controlling variables
int i = 0;
int j = 0;
// Define auxiliary variable
int location = -1;
// Outer loop (0...size)
for (i = 0; i < size; ++i)
{
location = -1;
for (j = i - 1; j > 0 && location == -1; --j)
{
if (arr[i] > arr[j])
{
// When get smaller element
location = j;
}
}
if (location == -1)
{
Console.Write(" [" + arr[i] + "] : None \n");
}
else
{
Console.Write(" [" + arr[i] + "] : " + arr[location] + " \n");
}
}
}
public static void Main(String[] args)
{
Element task = new Element();
// Given array elements
int[] arr = {
6 , 3 , 7 , 5 , 12 , 10 , 6 , 2 , 4
};
// Get the number of elements
int size = arr.Length;
task.leftSmallest(arr, size);
}
}
Output
[6] : None
[3] : None
[7] : 3
[5] : 3
[12] : 5
[10] : 5
[6] : 5
[2] : None
[4] : 2
<?php
/*
Php program
Find nearest smallest element of left side in array
*/
class Element
{
// Find the smallest element of left side in given array
public function leftSmallest( & $arr, $size)
{
// Loop controlling variables
$i = 0;
$j = 0;
// Define auxiliary variable
$location = -1;
// Outer loop (0...size)
for ($i = 0; $i < $size; ++$i)
{
$location = -1;
for ($j = $i - 1; $j > 0 && $location == -1; --$j)
{
if ($arr[$i] > $arr[$j])
{
// When get smaller element
$location = $j;
}
}
if ($location == -1)
{
echo " [". $arr[$i] ."] : None \n";
}
else
{
echo " [". $arr[$i] ."] : ". $arr[$location] ." \n";
}
}
}
}
function main()
{
$task = new Element();
// Given array elements
$arr = array(6, 3, 7, 5, 12, 10, 6, 2, 4);
// Get the number of elements
$size = count($arr);
$task->leftSmallest($arr, $size);
}
main();
Output
[6] : None
[3] : None
[7] : 3
[5] : 3
[12] : 5
[10] : 5
[6] : 5
[2] : None
[4] : 2
/*
Node Js program
Find nearest smallest element of left side in array
*/
class Element
{
// Find the smallest element of left side in given array
leftSmallest(arr, size)
{
// Loop controlling variables
var i = 0;
var j = 0;
// Define auxiliary variable
var location = -1;
// Outer loop (0...size)
for (i = 0; i < size; ++i)
{
location = -1;
for (j = i - 1; j > 0 && location == -1; --j)
{
if (arr[i] > arr[j])
{
// When get smaller element
location = j;
}
}
if (location == -1)
{
process.stdout.write(" [" + arr[i] + "] : None \n");
}
else
{
process.stdout.write(" [" + arr[i] + "] : " + arr[location] + " \n");
}
}
}
}
function main()
{
var task = new Element();
// Given array elements
var arr = [6, 3, 7, 5, 12, 10, 6, 2, 4];
// Get the number of elements
var size = arr.length;
task.leftSmallest(arr, size);
}
main();
Output
[6] : None
[3] : None
[7] : 3
[5] : 3
[12] : 5
[10] : 5
[6] : 5
[2] : None
[4] : 2
# Python 3 program
# Find nearest smallest element of left side in array
class Element :
# Find the smallest element of left side in given array
def leftSmallest(self, arr, size) :
# Loop controlling variables
i = 0
j = 0
# Define auxiliary variable
location = -1
# Outer loop (0...size)
while (i < size) :
location = -1
j = i - 1
while (j > 0 and location == -1) :
if (arr[i] > arr[j]) :
# When get smaller element
location = j
j -= 1
if (location == -1) :
print(" [", arr[i] ,"] : None ")
else :
print(" [", arr[i] ,"] : ", arr[location] ," ")
i += 1
def main() :
task = Element()
# Given array elements
arr = [6, 3, 7, 5, 12, 10, 6, 2, 4]
# Get the number of elements
size = len(arr)
task.leftSmallest(arr, size)
if __name__ == "__main__": main()
Output
[ 6 ] : None
[ 3 ] : None
[ 7 ] : 3
[ 5 ] : 3
[ 12 ] : 5
[ 10 ] : 5
[ 6 ] : 5
[ 2 ] : None
[ 4 ] : 2
# Ruby program
# Find nearest smallest element of left side in array
class Element
# Find the smallest element of left side in given array
def leftSmallest(arr, size)
# Loop controlling variables
i = 0
j = 0
# Define auxiliary variable
location = -1
# Outer loop (0...size)
while (i < size)
location = -1
j = i - 1
while (j > 0 && location == -1)
if (arr[i] > arr[j])
# When get smaller element
location = j
end
j -= 1
end
if (location == -1)
print(" [", arr[i] ,"] : None \n")
else
print(" [", arr[i] ,"] : ", arr[location] ," \n")
end
i += 1
end
end
end
def main()
task = Element.new()
# Given array elements
arr = [6, 3, 7, 5, 12, 10, 6, 2, 4]
# Get the number of elements
size = arr.length
task.leftSmallest(arr, size)
end
main()
Output
[6] : None
[3] : None
[7] : 3
[5] : 3
[12] : 5
[10] : 5
[6] : 5
[2] : None
[4] : 2
/*
Scala program
Find nearest smallest element of left side in array
*/
class Element
{
// Find the smallest element of left side in given array
def leftSmallest(arr: Array[Int], size: Int): Unit = {
// Loop controlling variables
var i: Int = 0;
var j: Int = 0;
// Define auxiliary variable
var location: Int = -1;
// Outer loop (0...size)
while (i < size)
{
location = -1;
j = i - 1;
while (j > 0 && location == -1)
{
if (arr(i) > arr(j))
{
// When get smaller element
location = j;
}
j -= 1;
}
if (location == -1)
{
print(" [" + arr(i) + "] : None \n");
}
else
{
print(" [" + arr(i) + "] : " + arr(location) + " \n");
}
i += 1;
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Element = new Element();
// Given array elements
var arr: Array[Int] = Array(6, 3, 7, 5, 12, 10, 6, 2, 4);
// Get the number of elements
var size: Int = arr.length;
task.leftSmallest(arr, size);
}
}
Output
[6] : None
[3] : None
[7] : 3
[5] : 3
[12] : 5
[10] : 5
[6] : 5
[2] : None
[4] : 2
/*
Swift 4 program
Find nearest smallest element of left side in array
*/
class Element
{
// Find the smallest element of left side in given array
func leftSmallest(_ arr: [Int], _ size: Int)
{
// Loop controlling variables
var i: Int = 0;
var j: Int = 0;
// Define auxiliary variable
var location: Int = -1;
// Outer loop (0...size)
while (i < size)
{
location = -1;
j = i - 1;
while (j > 0 && location == -1)
{
if (arr[i] > arr[j])
{
// When get smaller element
location = j;
}
j -= 1;
}
if (location == -1)
{
print(" [", arr[i],"] : None ");
}
else
{
print(" [", arr[i],"] : ", arr[location]," ");
}
i += 1;
}
}
}
func main()
{
let task: Element = Element();
// Given array elements
let arr: [Int] = [6, 3, 7, 5, 12, 10, 6, 2, 4];
// Get the number of elements
let size: Int = arr.count;
task.leftSmallest(arr, size);
}
main();
Output
[ 6 ] : None
[ 3 ] : None
[ 7 ] : 3
[ 5 ] : 3
[ 12 ] : 5
[ 10 ] : 5
[ 6 ] : 5
[ 2 ] : None
[ 4 ] : 2
/*
Kotlin program
Find nearest smallest element of left side in array
*/
class Element
{
// Find the smallest element of left side in given array
fun leftSmallest(arr: Array < Int > , size: Int): Unit
{
// Loop controlling variables
var i: Int = 0;
var j: Int ;
// Define auxiliary variable
var location: Int ;
// Outer loop (0...size)
while (i < size)
{
location = -1;
j = i - 1;
while (j > 0 && location == -1)
{
if (arr[i] > arr[j])
{
// When get smaller element
location = j;
}
j -= 1;
}
if (location == -1)
{
print(" [" + arr[i] + "] : None \n");
}
else
{
print(" [" + arr[i] + "] : " + arr[location] + " \n");
}
i += 1;
}
}
}
fun main(args: Array < String > ): Unit
{
var task: Element = Element();
// Given array elements
var arr: Array < Int > = arrayOf(6, 3, 7, 5, 12, 10, 6, 2, 4);
// Get the number of elements
var size: Int = arr.count();
task.leftSmallest(arr, size);
}
Output
[6] : None
[3] : None
[7] : 3
[5] : 3
[12] : 5
[10] : 5
[6] : 5
[2] : None
[4] : 2
In above solution are need O(n*n) time to take all the left side smaller element. We can optimize this problem using stack. Stack operations are tack O(n) time to find all nearest smaller element in array. See this solution.
// C Program
// Find nearest smallest element of left side in array
// Using stack
#include <stdio.h>
#include <stdlib.h>
// Define stack node
struct StackNode
{
int element;
struct StackNode *next;
};
// Define a custom stack
struct MyStack
{
struct StackNode *top;
int size;
};
struct MyStack *newStack()
{
//Make a stack
struct MyStack *stack = (struct MyStack *) malloc(sizeof(struct MyStack));
if (stack != NULL)
{
//Set node values
stack->top = NULL;
stack->size = 0;
}
else
{
printf("\nMemory overflow when create new stack\n");
}
}
//Create a new node of stack
struct StackNode *newNode(int element, struct StackNode *next)
{
//Make a new node
struct StackNode *node = (struct StackNode *) malloc(sizeof(struct StackNode));
if (node == NULL)
{
printf("\nMemory overflow when create new stack Node \n");
}
else
{
node->element = element;
node->next = next;
}
return node;
}
// Returns the status of empty or non empty stacks
int isEmpty(struct MyStack *stack)
{
if (stack->size > 0 && stack->top != NULL)
{
return 0;
}
else
{
return 1;
}
}
// Add node at the top of stack
void push(struct MyStack *stack, int element)
{
// Add stack element
stack->top = newNode(element, stack->top);
stack->size++;
}
// return top element of stack
int peek(struct MyStack *stack)
{
return stack->top->element;
}
// Remove top element of stack
void pop(struct MyStack *stack)
{
if (isEmpty(stack) == 0)
{
struct StackNode *temp = stack->top;
// Change top element of stack
stack->top = temp->next;
// remove previous top
free(temp);
temp = NULL;
stack->size--;
}
}
// Find the smallest element of left side in given array
void leftSmallest(int arr[], int size)
{
// Loop controlling variables
int i = 0;
struct MyStack*stack = newStack();
// Outer loop (0...size)
for (i = 0; i < size; ++i)
{
while(isEmpty(stack) == 0 && peek(stack) >= arr[i])
{
pop(stack);
}
if(isEmpty(stack) == 0)
{
printf(" [%d] : %d \n", arr[i], peek(stack));
}
else
{
printf(" [%d] : None \n", arr[i]);
}
push(stack,arr[i]);
}
}
int main(int argc, char const *argv[])
{
// Given array elements
int arr[] = {6 , 3 , 7 , 5 , 12 , 10 , 6 , 2 , 4};
// Get the size
int size = sizeof(arr)/sizeof(arr[0]);
leftSmallest(arr,size);
return 0;
}
Output
[6] : None
[3] : None
[7] : 3
[5] : 3
[12] : 5
[10] : 5
[6] : 5
[2] : None
[4] : 2
/*
Java program
Find nearest smallest element of left side in array
by using stack
*/
// Define stack node
class StackNode
{
public int element;
public StackNode next;
public StackNode(int element, StackNode next)
{
this.element = element;
this.next = next;
}
}
// Define a custom stack
class MyStack
{
public StackNode top;
public int size;
public MyStack()
{
//Set node values
this.top = null;
this.size = 0;
}
// Add node at the top of stack
public void push(int element)
{
this.top = new StackNode(element, this.top);
this.size++;
}
public boolean isEmpty()
{
if (this.size > 0 && this.top != null)
{
return false;
}
else
{
return true;
}
}
// Remove top element of stack
public void pop()
{
if (this.size > 0 && this.top != null)
{
StackNode temp = this.top;
// Change top element of stack
this.top = temp.next;
// remove previous top
temp = null;
this.size--;
}
}
// return top element of stack
public int peek()
{
return this.top.element;
}
}
public class Element
{
// Find the smallest element of left side in given array
public void leftSmallest(int[] arr, int size)
{
// Loop controlling variables
int i = 0;
MyStack stack = new MyStack();
// Outer loop (0...size)
for (i = 0; i < size; ++i)
{
// Remove the stack element until its top is not less than current element of array
while (stack.isEmpty() == false && stack.peek() >= arr[i])
{
// Remove top element
stack.pop();
}
if (stack.isEmpty() == false)
{
System.out.print(" [" + arr[i] + "] : " + stack.peek() + " \n");
}
else
{
System.out.print(" [" + arr[i] + "] : None \n");
}
//Add element into stack
stack.push(arr[i]);
}
}
public static void main(String[] args)
{
Element task = new Element();
// Given array elements
int[] arr = {
6 , 3 , 7 , 5 , 12 , 10 , 6 , 2 , 4
};
// Get the number of elements
int size = arr.length;
task.leftSmallest(arr, size);
}
}
Output
[6] : None
[3] : None
[7] : 3
[5] : 3
[12] : 5
[10] : 5
[6] : 5
[2] : None
[4] : 2
// Include header file
#include <iostream>
using namespace std;
/*
C++ program
Find nearest smallest element of left side in array
by using stack
*/
// Define stack node
class StackNode
{
public: int element;
StackNode *next;
StackNode(int element, StackNode *next)
{
this->element = element;
this->next = next;
}
};
// Define a custom stack
class MyStack
{
public: StackNode *top;
int size;
MyStack()
{
//Set node values
this->top = NULL;
this->size = 0;
}
// Add node at the top of stack
void push(int element)
{
this->top = new StackNode(element, this->top);
this->size++;
}
bool isEmpty()
{
if (this->size > 0 && this->top != NULL)
{
return false;
}
else
{
return true;
}
}
// Remove top element of stack
void pop()
{
if (this->size > 0 && this->top != NULL)
{
StackNode *temp = this->top;
// Change top element of stack
this->top = temp->next;
// remove previous top
delete temp;
temp = NULL;
this->size--;
}
}
// return top element of stack
int peek()
{
return this->top->element;
}
};
class Element
{
public:
// Find the smallest element of left side in given array
void leftSmallest(int arr[], int size)
{
// Loop controlling variables
int i = 0;
MyStack stack = MyStack();
// Outer loop (0...size)
for (i = 0; i < size; ++i)
{
// Remove the stack element until its top is not less than current element of array
while (stack.isEmpty() == false && stack.peek() >= arr[i])
{
// Remove top element
stack.pop();
}
if (stack.isEmpty() == false)
{
cout << " [" << arr[i] << "] : " << stack.peek() << " \n";
}
else
{
cout << " [" << arr[i] << "] : None \n";
}
//Add element into stack
stack.push(arr[i]);
}
}
};
int main()
{
Element task = Element();
// Given array elements
int arr[] = {
6 , 3 , 7 , 5 , 12 , 10 , 6 , 2 , 4
};
// Get the number of elements
int size = sizeof(arr) / sizeof(arr[0]);
task.leftSmallest(arr, size);
return 0;
}
Output
[6] : None
[3] : None
[7] : 3
[5] : 3
[12] : 5
[10] : 5
[6] : 5
[2] : None
[4] : 2
// Include namespace system
using System;
/*
C# program
Find nearest smallest element of left side in array
by using stack
*/
// Define stack node
public class StackNode
{
public int element;
public StackNode next;
public StackNode(int element, StackNode next)
{
this.element = element;
this.next = next;
}
}
// Define a custom stack
public class MyStack
{
public StackNode top;
public int size;
public MyStack()
{
//Set node values
this.top = null;
this.size = 0;
}
// Add node at the top of stack
public void push(int element)
{
this.top = new StackNode(element, this.top);
this.size++;
}
public Boolean isEmpty()
{
if (this.size > 0 && this.top != null)
{
return false;
}
else
{
return true;
}
}
// Remove top element of stack
public void pop()
{
if (this.size > 0 && this.top != null)
{
StackNode temp = this.top;
// Change top element of stack
this.top = temp.next;
// remove previous top
temp = null;
this.size--;
}
}
// return top element of stack
public int peek()
{
return this.top.element;
}
}
public class Element
{
// Find the smallest element of left side in given array
public void leftSmallest(int[] arr, int size)
{
// Loop controlling variables
int i = 0;
MyStack stack = new MyStack();
// Outer loop (0...size)
for (i = 0; i < size; ++i)
{
// Remove the stack element until its top is not less than current element of array
while (stack.isEmpty() == false && stack.peek() >= arr[i])
{
// Remove top element
stack.pop();
}
if (stack.isEmpty() == false)
{
Console.Write(" [" + arr[i] + "] : " + stack.peek() + " \n");
}
else
{
Console.Write(" [" + arr[i] + "] : None \n");
}
//Add element into stack
stack.push(arr[i]);
}
}
public static void Main(String[] args)
{
Element task = new Element();
// Given array elements
int[] arr = {
6 , 3 , 7 , 5 , 12 , 10 , 6 , 2 , 4
};
// Get the number of elements
int size = arr.Length;
task.leftSmallest(arr, size);
}
}
Output
[6] : None
[3] : None
[7] : 3
[5] : 3
[12] : 5
[10] : 5
[6] : 5
[2] : None
[4] : 2
<?php
/*
Php program
Find nearest smallest element of left side in array
by using stack
*/
// Define stack node
class StackNode
{
public $element;
public $next;
function __construct($element, $next)
{
$this->element = $element;
$this->next = $next;
}
}
// Define a custom stack
class MyStack
{
public $top;
public $size;
function __construct()
{
//Set node values
$this->top = null;
$this->size = 0;
}
// Add node at the top of stack
public function push($element)
{
$this->top = new StackNode($element, $this->top);
$this->size++;
}
public function isEmpty()
{
if ($this->size > 0 && $this->top != null)
{
return false;
}
else
{
return true;
}
}
// Remove top element of stack
public function pop()
{
if ($this->size > 0 && $this->top != null)
{
$temp = $this->top;
// Change top element of stack
$this->top = $temp->next;
// remove previous top
$temp = null;
$this->size--;
}
}
// return top element of stack
public function peek()
{
return $this->top->element;
}
}
class Element
{
// Find the smallest element of left side in given array
public function leftSmallest( & $arr, $size)
{
// Loop controlling variables
$i = 0;
$stack = new MyStack();
// Outer loop (0...size)
for ($i = 0; $i < $size; ++$i)
{
// Remove the stack element until its top is not less than current element of array
while ($stack->isEmpty() == false && $stack->peek() >= $arr[$i])
{
// Remove top element
$stack->pop();
}
if ($stack->isEmpty() == false)
{
echo " [". $arr[$i] ."] : ". $stack->peek() ." \n";
}
else
{
echo " [". $arr[$i] ."] : None \n";
}
//Add element into stack
$stack->push($arr[$i]);
}
}
}
function main()
{
$task = new Element();
// Given array elements
$arr = array(6, 3, 7, 5, 12, 10, 6, 2, 4);
// Get the number of elements
$size = count($arr);
$task->leftSmallest($arr, $size);
}
main();
Output
[6] : None
[3] : None
[7] : 3
[5] : 3
[12] : 5
[10] : 5
[6] : 5
[2] : None
[4] : 2
/*
Node Js program
Find nearest smallest element of left side in array
by using stack
*/
// Define stack node
class StackNode
{
constructor(element, next)
{
this.element = element;
this.next = next;
}
}
// Define a custom stack
class MyStack
{
constructor()
{
//Set node values
this.top = null;
this.size = 0;
}
// Add node at the top of stack
push(element)
{
this.top = new StackNode(element, this.top);
this.size++;
}
isEmpty()
{
if (this.size > 0 && this.top != null)
{
return false;
}
else
{
return true;
}
}
// Remove top element of stack
pop()
{
if (this.size > 0 && this.top != null)
{
var temp = this.top;
// Change top element of stack
this.top = temp.next;
// remove previous top
temp = null;
this.size--;
}
}
// return top element of stack
peek()
{
return this.top.element;
}
}
class Element
{
// Find the smallest element of left side in given array
leftSmallest(arr, size)
{
// Loop controlling variables
var i = 0;
var stack = new MyStack();
// Outer loop (0...size)
for (i = 0; i < size; ++i)
{
// Remove the stack element until its top is not less than current element of array
while (stack.isEmpty() == false && stack.peek() >= arr[i])
{
// Remove top element
stack.pop();
}
if (stack.isEmpty() == false)
{
process.stdout.write(" [" + arr[i] + "] : " + stack.peek() + " \n");
}
else
{
process.stdout.write(" [" + arr[i] + "] : None \n");
}
//Add element into stack
stack.push(arr[i]);
}
}
}
function main()
{
var task = new Element();
// Given array elements
var arr = [6, 3, 7, 5, 12, 10, 6, 2, 4];
// Get the number of elements
var size = arr.length;
task.leftSmallest(arr, size);
}
main();
Output
[6] : None
[3] : None
[7] : 3
[5] : 3
[12] : 5
[10] : 5
[6] : 5
[2] : None
[4] : 2
# Python 3 program
# Find nearest smallest element of left side in array
# by using stack
# Define stack node
class StackNode :
def __init__(self, element, next) :
self.element = element
self.next = next
# Define a custom stack
class MyStack :
def __init__(self) :
# Set node values
self.top = None
self.size = 0
# Add node at the top of stack
def push(self, element) :
self.top = StackNode(element, self.top)
self.size += 1
def isEmpty(self) :
if (self.size > 0 and self.top != None) :
return False
else :
return True
# Remove top element of stack
def pop(self) :
if (self.size > 0 and self.top != None) :
temp = self.top
# Change top element of stack
self.top = temp.next
# remove previous top
temp = None
self.size -= 1
# return top element of stack
def peek(self) :
return self.top.element
class Element :
# Find the smallest element of left side in given array
def leftSmallest(self, arr, size) :
# Loop controlling variables
i = 0
stack = MyStack()
# Outer loop (0...size)
while (i < size) :
# Remove the stack element until its top is not less than current element of array
while (stack.isEmpty() == False and stack.peek() >= arr[i]) :
# Remove top element
stack.pop()
if (stack.isEmpty() == False) :
print(" [", arr[i] ,"] : ", stack.peek() ," ")
else :
print(" [", arr[i] ,"] : None ")
# Add element into stack
stack.push(arr[i])
i += 1
def main() :
task = Element()
# Given array elements
arr = [6, 3, 7, 5, 12, 10, 6, 2, 4]
# Get the number of elements
size = len(arr)
task.leftSmallest(arr, size)
if __name__ == "__main__": main()
Output
[ 6 ] : None
[ 3 ] : None
[ 7 ] : 3
[ 5 ] : 3
[ 12 ] : 5
[ 10 ] : 5
[ 6 ] : 5
[ 2 ] : None
[ 4 ] : 2
# Ruby program
# Find nearest smallest element of left side in array
# by using stack
# Define stack node
class StackNode
# Define the accessor and reader of class StackNode
attr_reader :element, :next
attr_accessor :element, :next
def initialize(element, top)
self.element = element
self.next = top
end
end
# Define a custom stack
class MyStack
# Define the accessor and reader of class MyStack
attr_reader :top, :size
attr_accessor :top, :size
def initialize()
# Set node values
self.top = nil
self.size = 0
end
# Add node at the top of stack
def push(element)
self.top = StackNode.new(element, self.top)
self.size += 1
end
def isEmpty()
if (self.size > 0 && self.top != nil)
return false
else
return true
end
end
# Remove top element of stack
def pop()
if (self.size > 0 && self.top != nil)
temp = self.top
# Change top element of stack
self.top = temp.next
# remove previous top
temp = nil
self.size -= 1
end
end
# return top element of stack
def peek()
return self.top.element
end
end
class Element
# Find the smallest element of left side in given array
def leftSmallest(arr, size)
# Loop controlling variables
i = 0
stack = MyStack.new()
# Outer loop (0...size)
while (i < size)
# Remove the stack element until its top is not less than current element of array
while (stack.isEmpty() == false && stack.peek() >= arr[i])
# Remove top element
stack.pop()
end
if (stack.isEmpty() == false)
print(" [", arr[i] ,"] : ", stack.peek() ," \n")
else
print(" [", arr[i] ,"] : None \n")
end
# Add element into stack
stack.push(arr[i])
i += 1
end
end
end
def main()
task = Element.new()
# Given array elements
arr = [6, 3, 7, 5, 12, 10, 6, 2, 4]
# Get the number of elements
size = arr.length
task.leftSmallest(arr, size)
end
main()
Output
[6] : None
[3] : None
[7] : 3
[5] : 3
[12] : 5
[10] : 5
[6] : 5
[2] : None
[4] : 2
/*
Scala program
Find nearest smallest element of left side in array
by using stack
*/
// Define stack node
class StackNode(var element: Int , var next: StackNode);
// Define a custom stack
class MyStack(var top: StackNode , var size: Int)
{
def this()
{
this(null, 0);
}
// Add node at the top of stack
def push(element: Int): Unit = {
this.top = new StackNode(element, this.top);
this.size += 1;
}
def isEmpty(): Boolean = {
if (this.size > 0 && this.top != null)
{
return false;
}
else
{
return true;
}
}
// Remove top element of stack
def pop(): Unit = {
if (this.size > 0 && this.top != null)
{
var temp: StackNode = this.top;
// Change top element of stack
this.top = temp.next;
// remove previous top
temp = null;
this.size -= 1;
}
}
// return top element of stack
def peek(): Int = {
return this.top.element;
}
}
class Element
{
// Find the smallest element of left side in given array
def leftSmallest(arr: Array[Int], size: Int): Unit = {
// Loop controlling variables
var i: Int = 0;
var stack: MyStack = new MyStack();
// Outer loop (0...size)
while (i < size)
{
// Remove the stack element until its top is not less than current element of array
while (stack.isEmpty() == false && stack.peek() >= arr(i))
{
// Remove top element
stack.pop();
}
if (stack.isEmpty() == false)
{
print(" [" + arr(i) + "] : " + stack.peek() + " \n");
}
else
{
print(" [" + arr(i) + "] : None \n");
}
//Add element into stack
stack.push(arr(i));
i += 1;
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Element = new Element();
// Given array elements
var arr: Array[Int] = Array(6, 3, 7, 5, 12, 10, 6, 2, 4);
// Get the number of elements
var size: Int = arr.length;
task.leftSmallest(arr, size);
}
}
Output
[6] : None
[3] : None
[7] : 3
[5] : 3
[12] : 5
[10] : 5
[6] : 5
[2] : None
[4] : 2
/*
Swift 4 program
Find nearest smallest element of left side in array
by using stack
*/
// Define stack node
class StackNode
{
var element: Int;
var next: StackNode? ;
init(_ element: Int, _ next: StackNode? )
{
self.element = element;
self.next = next;
}
}
// Define a custom stack
class MyStack
{
var top: StackNode? ;
var size: Int;
init()
{
//Set node values
self.top = nil;
self.size = 0;
}
// Add node at the top of stack
func push(_ element: Int)
{
self.top = StackNode(element, self.top);
self.size += 1;
}
func isEmpty()->Bool
{
if (self.size > 0 && self.top != nil)
{
return false;
}
else
{
return true;
}
}
// Remove top element of stack
func pop()
{
if (self.size > 0 && self.top != nil)
{
var temp: StackNode? = self.top;
// Change top element of stack
self.top = temp!.next;
// remove previous top
temp = nil;
self.size -= 1;
}
}
// return top element of stack
func peek()->Int
{
return self.top!.element;
}
}
class Element
{
// Find the smallest element of left side in given array
func leftSmallest(_ arr: [Int], _ size: Int)
{
// Loop controlling variables
var i: Int = 0;
let stack: MyStack = MyStack();
// Outer loop (0...size)
while (i < size)
{
// Remove the stack element until its top is not less than current element of array
while (stack.isEmpty() == false && stack.peek() >= arr[i])
{
// Remove top element
stack.pop();
}
if (stack.isEmpty() == false)
{
print(" [", arr[i],"] : ", stack.peek() ," ");
}
else
{
print(" [", arr[i],"] : None ");
}
//Add element into stack
stack.push(arr[i]);
i += 1;
}
}
}
func main()
{
let task: Element = Element();
// Given array elements
let arr: [Int] = [6, 3, 7, 5, 12, 10, 6, 2, 4];
// Get the number of elements
let size: Int = arr.count;
task.leftSmallest(arr, size);
}
main();
Output
[ 6 ] : None
[ 3 ] : None
[ 7 ] : 3
[ 5 ] : 3
[ 12 ] : 5
[ 10 ] : 5
[ 6 ] : 5
[ 2 ] : None
[ 4 ] : 2
/*
Kotlin program
Find nearest smallest element of left side in array
by using stack
*/
// Define stack node
class StackNode
{
var element: Int;
var next: StackNode ? ;
constructor(element: Int, next: StackNode ? )
{
this.element = element;
this.next = next;
}
}
// Define a custom stack
class MyStack
{
var top: StackNode ? ;
var size: Int;
constructor()
{
//Set node values
this.top = null;
this.size = 0;
}
// Add node at the top of stack
fun push(element: Int): Unit
{
this.top = StackNode(element, this.top);
this.size += 1;
}
fun isEmpty(): Boolean
{
if (this.size > 0 && this.top != null)
{
return false;
}
else
{
return true;
}
}
// Remove top element of stack
fun pop(): Unit
{
if (this.size > 0 && this.top != null)
{
var temp: StackNode? = this.top;
// Change top element of stack
this.top = temp?.next;
this.size -= 1;
}
}
// return top element of stack
fun peek(): Int
{
return this.top!!.element;
}
}
class Element
{
// Find the smallest element of left side in given array
fun leftSmallest(arr: Array < Int > , size: Int): Unit
{
// Loop controlling variables
var i: Int = 0;
var stack: MyStack = MyStack();
// Outer loop (0...size)
while (i < size)
{
// Remove the stack element until its top is not less than current element of array
while (stack.isEmpty() == false && stack.peek() >= arr[i])
{
// Remove top element
stack.pop();
}
if (stack.isEmpty() == false)
{
print(" [" + arr[i] + "] : " + stack.peek() + " \n");
}
else
{
print(" [" + arr[i] + "] : None \n");
}
//Add element into stack
stack.push(arr[i]);
i += 1;
}
}
}
fun main(args: Array < String > ): Unit
{
var task: Element = Element();
// Given array elements
var arr: Array < Int > = arrayOf(6, 3, 7, 5, 12, 10, 6, 2, 4);
// Get the number of elements
var size: Int = arr.count();
task.leftSmallest(arr, size);
}
Output
[6] : None
[3] : None
[7] : 3
[5] : 3
[12] : 5
[10] : 5
[6] : 5
[2] : None
[4] : 2
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment