You are on page 1of 7

Output:

Lab Assignment-1

Write a C++ Program to find a triplet such that sum of two equals to the third element.

Code:
#include <bits/stdc++.h>
#include <chrono>
using namespace std;
using namespace std::chrono;
void findtriplets(int arr[], int n)
{
sort(arr, arr + n);
for (int j = n - 1; j >= 0; j--)
{
int i = 0;
int k = j - 1;

while (i < k)
{

if (arr[j] == arr[i] + arr[k])


{
cout << "numbers are " << arr[i] << " " << arr[k] << " " << arr[j] << endl;
i++;
}
k--;
}
j--;
}
}
int main(){
int arr[] = {1, 2, 3, 4, 5, 6};
int n = sizeof(arr) / sizeof(arr[0]);
auto start = high_resolution_clock::now();
findtriplets(arr, n);
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);

cout << "Time taken by function: "


<< duration.count() << " microseconds" << endl;

return 0;
}
Output:
Lab Assignment-2

Write a program to reverse the elements of linked list in k groups.


Code:
#include <iostream>
using namespace std;
class node{
public:
int data;
node *next;
node(int data)
{
this->data = data;
this->next = NULL;
}
};
void insertion(node *&head, int pos, int d)
{
node *temp = new node(d);
if (pos == 1)
{
temp->next = head;
head = temp;
return;
}
int count = 1;
node *temp2 = head;
while (count < pos - 1)
{
temp2 = temp2->next;
count++;
}
temp->next = temp2->next;
temp2->next = temp;
}
void printlist(node *&head)
{
node *temp = head;
while (temp != NULL)
{
cout << temp->data << " ";
temp = temp->next;
}
}
node *reverseingroup(node *head, int n)
{
int count = 0;
node *prev = NULL;
node *curr = head;
node *forward = NULL;
while (curr != NULL && count < n)
{
forward = curr->next;
curr->next = prev;
prev = curr;
curr = forward;
count++;
}
if (forward != NULL)
{

head->next = reverseingroup(forward, n);


}
return prev;
}
int main(){
node *node1 = new node(1);
node *head = node1;
insertion(head, 1, 10);
insertion(head, 2, 3);
insertion(head, 3, 4);
insertion(head, 2, 5);
insertion(head, 5, 87);
insertion(head, 2, 14);
insertion(head, 4, 13);
insertion(head, 9, 12);
insertion(head, 3, 4);
printlist(head);
cout<<endl;
int k;
cout << "enter the element slot:" << endl;
cin >> k;
cout << endl;
node *newhead = reverseingroup(head, num);
printlist(newhead);

return 0;
}

You might also like