0% found this document useful (0 votes)
26 views

12 TH Mar Dynamic Programming

The document discusses various algorithms and data structures concepts including sorting an array in a single loop without a new array, finding the middle element of a linked list using slow and fast pointers, detecting and finding loops in linked lists, finding the odd occurring number in an array using XOR, how hashmaps store key-value pairs and can retrieve values in O(1) time, and finding pairs in an array that sum to a target value using a hashset.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

12 TH Mar Dynamic Programming

The document discusses various algorithms and data structures concepts including sorting an array in a single loop without a new array, finding the middle element of a linked list using slow and fast pointers, detecting and finding loops in linked lists, finding the odd occurring number in an array using XOR, how hashmaps store key-value pairs and can retrieve values in O(1) time, and finding pairs in an array that sum to a target value using a hashset.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

SwitchCase in C

#include <stdio.h>
#include<string.h>

int str_len(char str[])


{
int i=0;
for(i=0;str[i]!='\0';i++);
return i--;
}
int main()
{
char str[100];
gets(&str);
for(int i=0;i<str_len(str);i++)
{
if(str[i]>='a' && str[i]<='z')
{
str[i]-='a';
str[i]='A'+str[i];
}
else if(str[i]>='A' && str[i]<='Z')
{
str[i]-='A';
str[i]='a'+str[i];
}
}
printf("%s",str);

***********************************************************************************
*****************************************************
[0,2,1,2,1,0,1,2] - Sort using single loop without new array creation.

#include <stdio.h>
int main()
{
int arr[]={0,2,1,2,1,0,1,2};
int n=8;
int L=0,R=n-1,i=0;
int temp;
while(i<=R)
{
if(arr[i]==0)
{
temp=arr[L];
arr[L]=arr[i];
arr[i]=temp;
L++;
}
if(arr[i]==2)
{
temp=arr[R];
arr[R]=arr[i];
arr[i]=temp;
R--;
}
i++;
}
for(int i=0;i<n;i++)
{
printf("%d ",arr[i]);
}
}

***********************************************************************************
*****************************************************

Traverse linked list single time and find the middle element

Use both slow and fast pointer.........


when the fast pointer reaches last pointer slow will reach the middle element

***********************************************************************************
******************************************************

How to find loop in linked list

sol:1
traverse using slow and fast pointer ....... at some point both met at same pointer
.....
if it meets then it is circular linked list

sol:2
Using hashmap in Java

***********************************************************************************
******************************************************

find odd occurence number in array

int main()
{
int arr[]={1,0,2,1,0,0,2};
int sum=0,n=7;
for(int i=0;i<n;i++)
{
sum=sum^arr[i];
}
printf("%d",sum);
}

***********************************************************************************
******************************************************

Hashmap - using keys ... hashing the value and storing in keys

Advanced version of dictionary

//Java
Hashmap<key_dataType,Value_dataType> objName= new Hashmap();

Hashmap<Integer,String> hmap= new Hashmap();


Time Complexity : O(1)
a=1
b="Shree"
hmap.put(a,b);
hmap.get(1) // returns value

***********************************************************************************
******************************************************

[1,7,18,6,2,7,9] find the sum of pairs of the element just with one loop
sum=8

using hashset delete the element value with sum and save in hashset

now use contains() and check each element .... now the pair will return true and
none pair will return false

***********************************************************************************
******************************************************

You might also like