Bitonic point in the given linked list
Given a linked list with distinct elements, the task is to find the bitonic point in the given linked list. If there is no such point then print -1.
Examples:
Input: 1 -> 2 -> 3 -> 4 -> 3 -> 2 -> 1 -> NULL
Output: 4
1 -> 2 -> 3 -> 4 is strictly increasing.
4 -> 3 -> 2 -> 1 -> NULL is strictly decreasing.
Input: 97 -> 98 -> 99 -> 91 -> NULL
Output: 99
Approach: A Bitonic Point is a point in bitonic sequence before which elements are strictly increasing and after which elements are strictly decreasing. A Bitonic point doesn’t exist if array is only decreasing or only increasing. So, find the first node such that the value of the node next to it is strictly smaller. Start traversing the linked list from that node onwards and if every other node is strictly smaller than its previous node then the found node was out bitonic sequence else the given linked list doesn’t contain a valid bitonic sequence.
Note that an empty list or a list with a single node doesn’t represent a valid bitonic sequence.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
class Node {
public :
int data;
Node* next;
};
Node* push(Node** head_ref, int data)
{
Node* new_node = new Node;
new_node->data = data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
int bitonic_point(Node* node)
{
if (node == NULL)
return -1;
if (node->next == NULL)
return -1;
if (node->data > node->next->data)
return -1;
while (node->next != NULL) {
if (node->data > node->next->data)
break ;
node = node->next;
}
int bitonicPoint = node->data;
while (node->next != NULL) {
if (node->data < node->next->data)
return -1;
node = node->next;
}
return bitonicPoint;
}
int main()
{
Node* head = NULL;
push(&head, 100);
push(&head, 201);
push(&head, 399);
push(&head, 490);
push(&head, 377);
push(&head, 291);
push(&head, 100);
cout << bitonic_point(head);
return 0;
}
|
Java
class GFG
{
static class Node
{
int data;
Node next;
};
static Node push(Node head_ref, int data)
{
Node new_node = new Node();
new_node.data = data;
new_node.next = (head_ref);
(head_ref) = new_node;
return head_ref;
}
static int bitonic_point(Node node)
{
if (node == null )
return - 1 ;
if (node.next == null )
return - 1 ;
if (node.data > node.next.data)
return - 1 ;
while (node.next != null )
{
if (node.data > node.next.data)
break ;
node = node.next;
}
int bitonicPoint = node.data;
while (node.next != null )
{
if (node.data < node.next.data)
return - 1 ;
node = node.next;
}
return bitonicPoint;
}
public static void main(String args[])
{
Node head = null ;
head=push(head, 100 );
head=push(head, 201 );
head=push(head, 399 );
head=push(head, 490 );
head=push(head, 377 );
head=push(head, 291 );
head=push(head, 100 );
System.out.println(bitonic_point(head));
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self . next = None
def push(head_ref, data):
new_node = Node(data)
new_node. next = head_ref
head_ref = new_node
return head_ref
def bitonic_point(node):
if (node = = None ):
return - 1 ;
if (node. next = = None ):
return - 1 ;
if (node.data > node. next .data):
return - 1 ;
while (node. next ! = None ):
if (node.data > node. next .data):
break ;
node = node. next ;
bitonicPoint = node.data;
while (node. next ! = None ):
if (node.data < node. next .data):
return - 1 ;
node = node. next ;
return bitonicPoint;
if __name__ = = '__main__' :
head = None ;
head = push(head, 100 );
head = push(head, 201 );
head = push(head, 399 );
head = push(head, 490 );
head = push(head, 377 );
head = push(head, 291 );
head = push(head, 100 );
print (bitonic_point(head))
|
C#
using System;
class GFG
{
public class Node
{
public int data;
public Node next;
};
static Node push(Node head_ref, int data)
{
Node new_node = new Node();
new_node.data = data;
new_node.next = (head_ref);
(head_ref) = new_node;
return head_ref;
}
static int bitonic_point(Node node)
{
if (node == null )
return -1;
if (node.next == null )
return -1;
if (node.data > node.next.data)
return -1;
while (node.next != null )
{
if (node.data > node.next.data)
break ;
node = node.next;
}
int bitonicPoint = node.data;
while (node.next != null )
{
if (node.data < node.next.data)
return -1;
node = node.next;
}
return bitonicPoint;
}
public static void Main(String []args)
{
Node head = null ;
head=push(head, 100);
head=push(head, 201);
head=push(head, 399);
head=push(head, 490);
head=push(head, 377);
head=push(head, 291);
head=push(head, 100);
Console.WriteLine(bitonic_point(head));
}
}
|
Javascript
<script>
class Node {
constructor() {
this .data = 0;
this .next = null ;
}
}
function push( head_ref, data)
{
var new_node = new Node();
new_node.data = data;
new_node.next = (head_ref);
(head_ref) = new_node;
return head_ref;
}
function bitonic_point( node)
{
if (node == null )
return -1;
if (node.next == null )
return -1;
if (node.data > node.next.data)
return -1;
while (node.next != null )
{
if (node.data > node.next.data)
break ;
node = node.next;
}
let bitonicPoint = node.data;
while (node.next != null )
{
if (node.data < node.next.data)
return -1;
node = node.next;
}
return bitonicPoint;
}
var head = null ;
head=push(head, 100);
head=push(head, 201);
head=push(head, 399);
head=push(head, 490);
head=push(head, 377);
head=push(head, 291);
head=push(head, 100);
document.write(bitonic_point(head));
</script>
|
Time complexity: O(N) where N is the size of the given linked list.
Auxiliary space: O(1)
Last Updated :
11 Oct, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...