├── 3rd largest element ├── Adding two array ├── After Element of Minimum Index ├── Array Sort 2 ├── COIN SIMPLE ├── Count Distinct Elements ├── Counting Even elements ├── Delete An Element 1 ├── Dutch national flag problem ├── Extra Element ├── Farmer Goat ├── Find Count ├── Find Sick Cow ├── Find minimum element 1 ├── Find next minimum element. ├── Find out the pairs of Bullocks ├── Find square numbers 1 ├── Greater than previous Elements ├── Half reverse ├── Half reverse and add ├── Highest Hill ├── John's pair ├── Least non prime number ├── Maximum Elements of Kth element ├── Maximum division of minimum ├── Merge Two Array ├── Merge Two Arrays 6 ├── Minimum at sliding Widow ├── Monica's Problem ├── Number of Occurrences 1 ├── Number of occurrences 1 ├── Occurrence of elements ├── Odd occurrences ├── Print After Rotate ├── Print Array ├── Print Given Range ├── Print after Finding the element ├── Print as given ├── Print without Duplication ├── Remove Duplicate Elements 2 ├── Replace Elements 1 ├── Reverse Array ├── Reverse Entire Array ├── Reverse first and second half ├── Rotate an array 2 ├── Rotate and print elements ├── Search and Print ├── Smaller then previous elements ├── Sort Array 8 frequent method ├── Sort and Sum alternatively ├── Sort array alternately ├── Sorting array elements by frequency ├── Sorting in max and min order ├── Square Numbers 1 ├── Sum of Two numbers 46 ├── Sum of all elements 1 ├── Swap array elements ├── Union of Two Array ├── first half in ascending order and second half in descending order ├── merge two array and sort ├── minimum of all the greater numbers ├── missing Number └── without n how to take array elements /3rd largest element: -------------------------------------------------------------------------------- 1 | Need to write a program to print the 3rd largest element. 2 | 3 | Input Format 4 | 5 | 7 6 | 7 23 45 2 9 56 8 7 | 8 | Constraints 9 | 10 | 1<=n<=20 11 | 12 | Output Format 13 | 14 | 23 15 | 16 | Sample Input 0 17 | 18 | 6 19 | 2 9 8 7 6 1 20 | Sample Output 0 21 | 22 | 7 23 | ---------------------------------------------------------------------------------------------------------------------------------- 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | int main() { 31 | int n,i,j,temp; 32 | scanf("%d",&n); 33 | int a[n]; 34 | for(i=0;ia[j]) 42 | { 43 | temp=a[i]; 44 | a[i]=a[j]; 45 | a[j]=temp; 46 | } 47 | } 48 | } 49 | 50 | printf("%d",a[n-3]); 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /Adding two array: -------------------------------------------------------------------------------- 1 | Given 2 huge numbers as separate digits, store them in array and process them and 2 | calculate the sum of 2 numbers and store the result in an array and print the sum. 3 | 4 | Input Format 5 | 6 | 12 7 | 9 8 | 9 2 8 1 3 5 6 7 3 1 1 6 9 | 7 8 4 6 2 1 9 9 7 10 | 11 | Constraints 12 | 13 | 4<=n<=15 14 | 15 | Output Format 16 | 17 | 9 2 8 9 2 0 2 9 5 1 1 3 18 | 19 | Sample Input 0 20 | 21 | 6 22 | 5 23 | 3 4 2 6 5 7 24 | 1 2 3 4 5 25 | Sample Output 0 26 | 27 | 3 5 5 0 0 2 28 | 29 | ---------------------------------------------------------------------------------------------------------------- 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | using namespace std; 36 | 37 | void rev(int a[],int n) 38 | { 39 | int l=0,r=n-1; 40 | while(l0) 66 | ans[size]=carry; 67 | return carry; 68 | } 69 | 70 | 71 | int main() { 72 | int n1,n2,i; 73 | cin>>n1>>n2; 74 | int a[n1],b[n2]; 75 | for(i=0;i>a[i]; 77 | for(i=0;i>b[i]; 79 | 80 | rev(a,n1); 81 | rev(b,n2); 82 | 83 | 84 | int size=n10) 91 | size++; 92 | 93 | rev(ans,size); 94 | 95 | for(i=0;i=0&&j>=0) 117 | { 118 | int t=a[i]+b[j]+c; 119 | s += t%10; 120 | c=t/10; 121 | i--; 122 | j--; 123 | } 124 | while(i>=0) 125 | { 126 | int t=a[i]+c; 127 | s += t%10; 128 | c=t/10; 129 | i--; 130 | } 131 | while(j>=0) 132 | { 133 | int t=a[j]+c; 134 | s += t%10; 135 | c=t/10; 136 | j--; 137 | } 138 | if(c>0) 139 | s+=c; 140 | System.out.println(s); 141 | 142 | int ans[]=new int[s.length()]; 143 | for(i=0;i 28 | #include 29 | #include 30 | #include 31 | #include 32 | 33 | int fun(int a[],int n) 34 | { 35 | int min=INT_MAX; 36 | int ind=0; 37 | for(int i=0;i 25 | int main() { 26 | int n,i,j; 27 | scanf("%d",&n); 28 | int a[n]; 29 | for(i=0;ia[j]) 36 | { 37 | int temp=a[i]; 38 | a[i]=a[j]; 39 | a[j]=temp; 40 | } 41 | } 42 | } 43 | for(i=0;i 28 | int main(){ 29 | int n,i,j,c=0,fc=0; 30 | scanf("%d",&n); 31 | int a[n]; 32 | for(i=0;i 25 | #include 26 | #include 27 | #include 28 | 29 | int main() { 30 | int n,e=0; 31 | scanf("%d",&n); 32 | int a[n]; 33 | for(int i=0;i 31 | #include 32 | #include 33 | #include 34 | 35 | void fun(int a[],int b[],int n,int m,int key) 36 | { 37 | int i=0,f=0,j=0,ind=-1; 38 | while(i 25 | #include 26 | #include 27 | #include 28 | #include 29 | int main() { 30 | int n,i,ans=INT_MAX; 31 | scanf("%d",&n); 32 | int a[n]; 33 | for(i=0;ia[i]) 41 | ans=a[i]; 42 | } 43 | printf("%d",ans); 44 | } 45 | 46 | -------------------------------------------------------------------------------- /Find Count: -------------------------------------------------------------------------------- 1 | Given an array and a threshold value find the output. 2 | 3 | Input Format 4 | 5 | 6 6 | 5 8 10 13 6 2 7 | 3 8 | 9 | Explanation: 10 | Number parts counts 11 | 5 {3,2} 3 12 | 8 {3,3,2} 4 13 | 10 {3,3,3,1} 4 14 | 13 {3,3,3,3,1} 5 15 | 6 {3,3} 2 16 | 2 {2} 2 17 | 18 | Constraints 19 | 20 | 4<=n<=10 21 | 22 | Output Format 23 | 24 | 20 25 | 26 | Sample Input 0 27 | 28 | 5 29 | 6 5 8 3 9 30 | 2 31 | Sample Output 0 32 | 33 | 17 34 | ------------------------------------------------------------------------------------------------------------------ 35 | #include 36 | #include 37 | #include 38 | #include 39 | #include 40 | using namespace std; 41 | 42 | int main() { 43 | 44 | int n,key,sum=0,i; 45 | scanf("%d",&n); 46 | int a[n]; 47 | for(i=0;i 28 | #include 29 | #include 30 | #include 31 | 32 | int main() { 33 | 34 | int n,i,key; 35 | scanf("%d",&n); 36 | int a[n]; 37 | for(i=0;i 25 | #include 26 | #include 27 | #include 28 | #include 29 | int main() { 30 | int n,i,ans=INT_MAX; 31 | scanf("%d",&n); 32 | int a[n]; 33 | for(i=0;ia[i]) 41 | ans=a[i]; 42 | } 43 | printf("%d",ans); 44 | } 45 | 46 | -------------------------------------------------------------------------------- /Find next minimum element.: -------------------------------------------------------------------------------- 1 | Given an array, find the next maximum element for each element in the array. 2 | 3 | Input Format 4 | 5 | 7 6 | 2 3 7 1 8 5 11 7 | 8 | Constraints 9 | 10 | 3<=n<=10 11 | 12 | Output Format 13 | 14 | 2>1,3>2,7>5,1>,8>7,5>3,11>8, 15 | 16 | Sample Input 0 17 | 18 | 5 19 | 3 2 6 7 1 20 | Sample Output 0 21 | 22 | 3>2,2>1,6>3,7>6,1>, 23 | 24 | ---------------------------------------------------------------------------------------------------- 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | using namespace std; 31 | 32 | 33 | int main() { 34 | int n,i; 35 | cin>>n; 36 | int a[n],b[n]; 37 | for(i=0;i>a[i]; 40 | b[i]=a[i]; 41 | } 42 | sort(b, b + n); 43 | for(i=0;i,"; 52 | else 53 | cout<"<m) 49 | m=a[i]; 50 | return m; 51 | } 52 | 53 | public static void main(String[] args) { 54 | 55 | Scanner z = new Scanner(System.in); 56 | int n = z.nextInt(); 57 | int a[] = new int[n]; 58 | for(int i=0;i1) 75 | { 76 | System.out.println(i+" - "+f[i]/2); 77 | } 78 | } 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /Find square numbers 1: -------------------------------------------------------------------------------- 1 | Given two numbers a and b both < 200 we have to find the square numbers which lie 2 | between a and b(inclusive) 3 | 4 | Input Format 5 | 6 | a = 25 7 | b = 100 8 | 9 | Constraints 10 | 11 | a and b should be <200 12 | 13 | Output Format 14 | 15 | 25 36 49 64 81 100 16 | 17 | Sample Input 0 18 | 19 | 100 20 | 200 21 | Sample Output 0 22 | 23 | 100 121 144 169 196 24 | ----------------------------------------------------------------------------------------------------------- 25 | #include 26 | #include 27 | #include 28 | #include 29 | void square(int a, int b) { 30 | int n = sqrt(a); 31 | while(n*n <= b) { 32 | printf("%d ", n*n); 33 | n++; 34 | } 35 | } 36 | 37 | int main() { 38 | int a, b; 39 | scanf("%d%d", &a,&b); 40 | square(a, b); 41 | 42 | } 43 | -------------------------------------------------------------------------------- /Greater than previous Elements: -------------------------------------------------------------------------------- 1 | You’re given an array. Print the elements of the array which are greater than its 2 | previous elements in the array. 3 | 4 | Input Format 5 | 6 | 7 7 | 2 -3 -4 5 9 7 8 8 | 9 | Constraints 10 | 11 | 4<=n<=10 12 | 13 | Output Format 14 | 15 | 2 5 9 16 | 17 | Sample Input 0 18 | 19 | 6 20 | 1 3 4 -5 2 6 21 | Sample Output 0 22 | 23 | 1 3 4 6 24 | ------------------------------------------------------------------------------------------------------------------------------------------- 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | int main() { 31 | int n,i,max=INT_MIN; 32 | scanf("%d",&n); 33 | int a[n]; 34 | for(i=0;i 28 | #include 29 | #include 30 | #include 31 | 32 | int main() { 33 | int n,L,r,temp; 34 | scanf("%d",&n); 35 | int a[n]; 36 | for(int i=0;i 31 | #include 32 | #include 33 | #include 34 | 35 | int main() { 36 | int n,L,r,temp,sum=0,i; 37 | scanf("%d",&n); 38 | int a[n]; 39 | for(i=0;i 24 | #include 25 | #include 26 | #include 27 | #include 28 | int main() { 29 | int n,i,ans=INT_MIN; 30 | scanf("%d",&n); 31 | int a[n]; 32 | for(i=0;i 26 | #include 27 | #include 28 | #include 29 | 30 | 31 | int sockMerchant(int n, int ar[]) { 32 | int pairs = 0; 33 | int colors[101] = {0}; 34 | for (int i = 0; i < n; i++) { 35 | colors[ar[i]]++; 36 | } 37 | for (int i = 1; i <= 100; i++) { 38 | pairs += colors[i] / 2; 39 | } 40 | return pairs; 41 | } 42 | 43 | int main() { 44 | int n; 45 | scanf("%d", &n); 46 | int ar[n]; 47 | for (int i = 0; i < n; i++) { 48 | scanf("%d", &ar[i]); 49 | } 50 | int result = sockMerchant(n, ar); 51 | printf("%d\n", result); 52 | return 0; 53 | } 54 | 55 | -------------------------------------------------------------------------------- /Least non prime number: -------------------------------------------------------------------------------- 1 | Find the least non-prime number that can be added with first array element that makes 2 | them divisible by second array elements at respective index (check for non-prime 3 | numbers under 1000, if doesn't exist return -1 as answer) & (Consider 1 as prime 4 | number) 5 | 6 | Input Format 7 | 8 | 2 9 | 2 10 | 20 16 11 | 7 5 12 | 13 | Constraints 14 | 15 | 4<=n<=10 16 | 17 | Output Format 18 | 19 | 8 20 | 4 21 | 22 | Sample Input 0 23 | 24 | 3 25 | 3 26 | 5 4 10 27 | 3 4 7 28 | Sample Output 0 29 | 30 | 4 31 | 4 32 | 4 33 | ---------------------------------------------------------------------------------------------------- 34 | #include 35 | #include 36 | #include 37 | #include 38 | 39 | int is_prime(int n) { 40 | 41 | if (n <= 1) { 42 | 43 | return 0; 44 | 45 | } 46 | 47 | for (int i = 2; i * i <= n; i++) { 48 | 49 | if (n % i == 0) { 50 | 51 | return 0; 52 | 53 | } 54 | 55 | } 56 | 57 | return 1; 58 | 59 | } 60 | 61 | int find_least_non_prime(int a, int b) { 62 | 63 | int i; 64 | 65 | for (i = 4; i < 1000; i++) { 66 | 67 | if (!is_prime(i) && (a + i) % b == 0) { 68 | 69 | return i; 70 | 71 | } 72 | 73 | } 74 | 75 | return -1; 76 | 77 | } 78 | int main () { 79 | 80 | int n1,n2; 81 | 82 | scanf("%d%d",&n1,&n2); 83 | 84 | int arr1[n1]; 85 | 86 | for(int i=0;i 41 | #include 42 | #include 43 | #include 44 | #include 45 | using namespace std; 46 | 47 | int fun(int m,int a[],int size) 48 | { 49 | int ind=0,i; 50 | for(i=0;im) 53 | { 54 | m=a[i]; 55 | ind=i; 56 | } 57 | } 58 | a[ind]=-1; 59 | return ind; 60 | } 61 | 62 | int main() { 63 | int n,i=0,key,ans=0,j,t=0; 64 | cin>>n; 65 | int size=ceil(n/2.0); 66 | int a[n],b[size]; 67 | for(i=0;i>a[i]; 69 | 70 | cin>>key; 71 | i=0; 72 | while(i 28 | #include 29 | #include 30 | #include 31 | #include 32 | int max(int a[],int n) 33 | { 34 | int max=INT_MIN; 35 | for(int i=0;imax) 37 | max=a[i]; 38 | return max; 39 | } 40 | int min(int a[],int n) 41 | { 42 | int min=INT_MAX; 43 | for(int i=0;i 30 | #include 31 | #include 32 | #include 33 | 34 | int main() { 35 | 36 | int n1,c=1,i,j; 37 | scanf("%d",&n1); 38 | int a[n1]; 39 | for(i=0;ifans[j]) 83 | { 84 | int temp=fans[i]; 85 | fans[i]=fans[j]; 86 | fans[j]=temp; 87 | } 88 | } 89 | } 90 | for(i=0;i 29 | #include 30 | #include 31 | #include 32 | 33 | int main() { 34 | 35 | int n1,c=1,i,j; 36 | scanf("%d",&n1); 37 | int a[n1]; 38 | for(i=0;ifans[j]) 82 | { 83 | int temp=fans[i]; 84 | fans[i]=fans[j]; 85 | fans[j]=temp; 86 | } 87 | } 88 | } 89 | for(i=0;i 94 | #include 95 | #include 96 | #include 97 | #define M 5 98 | #define N 4 99 | int main() { 100 | int arr1[100], arr2[100], merged[200], size1, size2, i, j, k; 101 | scanf("%d", &size1); 102 | for(i = 0; i < size1; i++) 103 | { 104 | scanf("%d", &arr1[i]); 105 | } 106 | scanf("%d", &size2); 107 | for(i = 0; i < size2; i++) 108 | { 109 | scanf("%d", &arr2[i]); 110 | } 111 | i = j = k = 0; 112 | while(i < size1 && j < size2) 113 | { 114 | if(arr1[i] < arr2[j]) 115 | { 116 | merged[k] = arr1[i]; 117 | i++; 118 | } 119 | else if(arr1[i] > arr2[j]) 120 | { 121 | merged[k] = arr2[j]; 122 | j++; 123 | } 124 | else 125 | { 126 | merged[k] = arr1[i]; 127 | i++; 128 | j++; 129 | } 130 | 131 | k++; 132 | } 133 | while(i < size1) 134 | { 135 | merged[k] = arr1[i]; 136 | i++; 137 | k++; 138 | } 139 | while(j < size2) 140 | { 141 | merged[k] = arr2[j]; 142 | j++; 143 | k++; 144 | } 145 | for(i = 0; i < k; i++) 146 | { 147 | if(i > 0 && merged[i] == merged[i - 1]) 148 | { 149 | continue; 150 | } 151 | 152 | printf("%d ", merged[i]); 153 | 154 | } 155 | 156 | return 0; 157 | } 158 | -------------------------------------------------------------------------------- /Minimum at sliding Widow: -------------------------------------------------------------------------------- 1 | Consider a sliding window of size k equals 2. Let the array be [3,4,2,7,6,8,2,7] the 2 | output should print 2 as the first output as first window contains {3,4,2} and second 3 | window contains {4,2,7} and so on 4 | 5 | Input Format 6 | 7 | 7 8 | 5 2 9 7 1 5 9 9 | 3 10 | 11 | Constraints 12 | 13 | 4<=n<=12 14 | 1<=E<=9 15 | 16 | Output Format 17 | 18 | 2 2 1 1 1 19 | 20 | Sample Input 0 21 | 22 | 9 23 | 1 2 3 4 5 6 7 8 9 24 | 3 25 | Sample Output 0 26 | 27 | 1 2 3 4 5 6 7 28 | --------------------------------------------------------------------------------------------------- 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | 35 | int main() { 36 | int n; 37 | scanf("%d", &n); 38 | int a[n]; 39 | for (int i = 0; i < n; i++) { 40 | scanf("%d", &a[i]); 41 | } 42 | int k; 43 | scanf("%d", &k); 44 | for (int i = 0; i <= n - k; i++) { 45 | int min= INT_MAX; 46 | 47 | for (int j = i; j < i + k; j++) { 48 | if (a[j] < min) { 49 | min= a[j]; 50 | } 51 | } 52 | printf("%d ", min); 53 | } 54 | return 0; 55 | } 56 | 57 | -------------------------------------------------------------------------------- /Monica's Problem: -------------------------------------------------------------------------------- 1 | Monica wants to buy a keyboard and a USB drive from her favorite electronics store. The store has several models of each. Monica wants to spend as much as possible for the 2 items, given her budget. 2 | Given the price lists for the store’s keyboards and USB drives, and Monica’s budget, find and print the amount of money Monica will spend. If she doesn’t have enough money to both a keyboard and a USB drive, print -1 instead. She will buy only the two required items. 3 | For example, suppose she has b=60 to spend. Three keyboards cost, keyboards=[40,50,70]. Three USB drives cost drives=[5,8,12]. She could purchase a 40 keyboards + 12 USB drives =52 or a 50 keyboards + 8 USB drives =58. She chooses the latter. She can’t buy more than 2 items so she can’t spend exactly 60. 4 | 5 | Input Format 6 | 7 | 3 8 | 3 9 | 40 50 70 10 | 5 8 12 11 | 60 12 | 13 | Constraints 14 | 15 | She should buy 2 items 16 | 17 | Output Format 18 | 19 | 58 20 | 21 | Sample Input 0 22 | 23 | 4 24 | 4 25 | 1 2 3 7 26 | 5 6 7 9 27 | 15 28 | Sample Output 0 29 | 30 | 14 31 | ================================================================================ 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | int main() { 38 | 39 | int n,m,max=INT_MIN,i,j; 40 | scanf("%d%d",&n,&m); 41 | int a[n]; 42 | int b[m]; 43 | for(i=0;imax&&a[i]+b[j] 35 | #include 36 | #include 37 | #include 38 | #include 39 | using namespace std; 40 | 41 | int fun(int a[],int n,int key) 42 | { 43 | int c=0; 44 | for(int i=0;i 36 | #include 37 | #include 38 | #include 39 | 40 | int main() { 41 | int n,i,j; 42 | scanf("%d",&n); 43 | int a[n]; 44 | int vis[n]; 45 | for(i=0;ia[j]) 56 | { 57 | int temp=a[i]; 58 | a[i]=a[j]; 59 | a[j]=temp; 60 | } 61 | } 62 | } 63 | 64 | for(i=0;i 33 | #include 34 | #include 35 | #include 36 | #include 37 | int max1(int a[],int n) 38 | { 39 | int max=INT_MIN; 40 | for(int i=0;imax) 43 | max=a[i]; 44 | } 45 | return max; 46 | } 47 | int main() { 48 | 49 | int n; 50 | scanf("%d",&n); 51 | int a[n]; 52 | for(int i=0;i 28 | int find_odd_occurrences(int a[], int n,int key) 29 | { 30 | int c=0; 31 | for(int i=0;i 31 | #include 32 | #include 33 | #include 34 | 35 | void fun(int a[],int n,int k,int s,int e){ 36 | for(int i =0;i=0;j--){ 39 | a[j+1] = a[j]; 40 | } 41 | a[0] = k; 42 | } 43 | for(int i=s;i<=e;i++){ 44 | printf("%d ",a[i]); 45 | } 46 | } 47 | 48 | int main() { 49 | 50 | int n; 51 | scanf("%d",&n); 52 | int a[n]; 53 | for(int i=0;i 26 | int main() { 27 | int n,i; 28 | scanf("%d",&n); 29 | int a[n]; 30 | for(i=0;i 27 | int main() { 28 | int n,i; 29 | scanf("%d",&n); 30 | int a[n]; 31 | for(i=0;i 28 | #include 29 | #include 30 | #include 31 | 32 | int main() { 33 | int n,i,key; 34 | scanf("%d",&n); 35 | int a[n]; 36 | for(i=0;i 54 | #include 55 | #include 56 | #include 57 | 58 | void fun(int n,int key,int a[]) 59 | { 60 | for(int i=0;i 23 | #include 24 | #include 25 | #include 26 | 27 | int main() { 28 | 29 | int n,i; 30 | scanf("%d",&n); 31 | int a[n]; 32 | for(i=0;i 25 | int main(){ 26 | int n,i,j; 27 | scanf("%d",&n); 28 | int a[n]; 29 | for(i=0;i 25 | int main(){ 26 | int n,i,j,c=0; 27 | scanf("%d",&n); 28 | int a[n]; 29 | for(i=0;i0;i--) 51 | { 52 | if(b[i]0;i--) 81 | { 82 | if(a[i]>a[i+1]) 83 | a[i]=a[i+1]; 84 | } 85 | for(int i=1;i<=n;i++) 86 | System.out.print(a[i]+" "); 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /Reverse Array: -------------------------------------------------------------------------------- 1 | void rev(int a[],int n) 2 | { 3 | int i=0; 4 | int j=n-1; 5 | while(i 28 | int main() { 29 | int n,i,j; 30 | scanf("%d",&n); 31 | int a[n],b[n]; 32 | for(i=0;i=0) 36 | { 37 | b[i]=a[j]; 38 | j--; 39 | i++; 40 | } 41 | for(i=0;i 48 | int main() { 49 | int n,temp,i,j; 50 | scanf("%d",&n); 51 | int a[n]; 52 | for(i=0;ie) 71 | return; 72 | int t=a[s]; 73 | a[s]=a[e]; 74 | a[e]=t; 75 | rev(a,s+1,e-1); 76 | } 77 | static void dis(int a[],int n) 78 | { 79 | for(int i=0;i 25 | #include 26 | #include 27 | #include 28 | void fun(int a[],int l,int r) 29 | { 30 | while(l 63 | #include 64 | #include 65 | #include 66 | 67 | int main() { 68 | 69 | int n,i; 70 | scanf("%d",&n); 71 | int a[n]; 72 | for(i=0;i 27 | #include 28 | #include 29 | #include 30 | 31 | int main() { 32 | 33 | int n,i,k; 34 | scanf("%d",&n); 35 | int a[n]; 36 | 37 | for(int i=0;i0) 43 | { 44 | int t1=a[n-1]; 45 | for(i=n-2;i>=0;i--) 46 | { 47 | a[i+1]=a[i]; 48 | } 49 | k--; 50 | a[0]=t1; 51 | } 52 | 53 | 54 | 55 | for(i=0;i 63 | #include 64 | #include 65 | #include 66 | void rotation(int a[],int n,int k){ 67 | int i,j,t; 68 | for(j=0;j=0;i--){ 71 | a[i+1]=a[i]; 72 | } 73 | a[0]=t; 74 | } 75 | } 76 | int main() { 77 | 78 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 79 | int n,i,k; 80 | scanf("%d",&n); 81 | int a[n]; 82 | for(int i=0;i 32 | #include 33 | #include 34 | #include 35 | 36 | int main() { 37 | int n,i,j,k,sr,er; 38 | scanf("%d",&n); 39 | int a[n]; 40 | for(i=0;ii-n1;j--) 49 | { 50 | int temp=a[j]; 51 | a[j]=a[j-1]; 52 | a[j-1]=temp; 53 | } 54 | } 55 | for(i=sr;i<=er;i++) 56 | printf("%d ",a[i]); 57 | } 58 | -------------------------------------------------------------------------------- /Search and Print: -------------------------------------------------------------------------------- 1 | Write a program to Search the elements and print elements between those indexes. 2 | 3 | Input Format 4 | 5 | 8 6 | 3 8 7 6 9 1 3 5 7 | 8 8 | 1 9 | 10 | Constraints 11 | 12 | Elements should be less than 10 13 | 14 | Output Format 15 | 16 | 7 6 9 17 | 18 | Sample Input 0 19 | 20 | 7 21 | 1 2 3 4 5 6 7 22 | 3 23 | 5 24 | Sample Output 0 25 | 26 | 4 27 | Explanation 0 28 | 29 | key 3 is at the index of 2 and the another key 5 is at the index of 4. So need to print between these indices. 30 | So here, we have only 4. So the output will be 4. 31 | ---------------------------------------------------------------------------------------------------------------------------------- 32 | #include 33 | #include 34 | #include 35 | #include 36 | 37 | int main() { 38 | int n,i1=0,i2=0; 39 | scanf("%d",&n); 40 | int a[n]; 41 | for(int i=0;i 63 | #include 64 | #include 65 | #include 66 | 67 | findIndex(int a[],int n,int start) 68 | 69 | { 70 | 71 | int index=-1; 72 | 73 | 74 | 75 | for(int i=0;i 26 | #include 27 | #include 28 | #include 29 | 30 | int main() { 31 | int n; 32 | scanf("%d",&n); 33 | int a[n]; 34 | for(int i=0;i 25 | int maximum(int A[],int n){ 26 | int max=A[0]; 27 | for(int i=1;imax){ 29 | max=A[i]; 30 | } 31 | } 32 | return max; 33 | } 34 | int minimum(int A[],int n){ 35 | int min=A[0]; 36 | for(int i=1;i 79 | #include 80 | #include 81 | #include 82 | int maximum(int A[],int n){ 83 | int max=A[0]; 84 | for(int i=1;imax){ 86 | max=A[i]; 87 | } 88 | } 89 | return max; 90 | } 91 | void hashing(int A[],int n){ 92 | int max=maximum(A,n); //1st 93 | int B[max+1]; 94 | for(int i=0;i<(max+1);i++){ 95 | B[i]=0; 96 | } 97 | for(int i=0;i mp = new HashMap<>(); 131 | for(int i=0;i> list = new ArrayList<>(mp.entrySet()); 138 | 139 | Collections.sort(list, new Comparator >(){ 140 | 141 | public int compare(Map.Entry o1, Map.Entry o2){ 142 | return o2.getValue().compareTo(o1.getValue()); 143 | } 144 | }); 145 | 146 | HashMap map = new HashMap<>(); 147 | 148 | for(Map.Entry lst : list){ 149 | map.put(lst.getKey(),lst.getValue()); 150 | for(int i=0;i map = new HashMap<>(); 180 | 181 | for(int i : arr){ 182 | map.put(i , map.getOrDefault(i,0)+1); 183 | } 184 | //System.out.println(Arrays.toString(arr)); 185 | PriorityQueue q = new PriorityQueue<>((a,b) -> a.count != b.count ? b.count - a.count : a.val-b.val); 186 | 187 | for(int i : map.keySet()){ 188 | // System.out.println(i + " " + map.get(i)); 189 | q.add(new Pair(i , map.get(i))); 190 | } 191 | //System.out.println(q); 192 | 193 | while(!q.isEmpty()){ 194 | Pair v = q.poll(); 195 | for(int i = 0 ; i < v.count ; i++){ 196 | System.out.print(v.val + " "); 197 | } 198 | } 199 | } 200 | } 201 | ========================================================================================================== 202 | public class Solution { 203 | public static void main(String args[] ) throws Exception { 204 | Scanner s = new Scanner(System.in); 205 | 206 | int n = s.nextInt(); 207 | int arr[] = new int[n]; 208 | 209 | Map map = new HashMap<>(); 210 | List res = new ArrayList<>(); 211 | 212 | for(int i=0; i=0; i--){ 230 | int ele = res.get(i); 231 | for(Map.Entry k : map.entrySet()){ 232 | if(k.getValue() == ele){ 233 | for(int z=0; z> n; 245 | vector arr(n); 246 | for(int i=0; i> arr[i]; 248 | map freq; 249 | for(int i: arr) 250 | freq[i]++; 251 | int m = freq.size(); 252 | vector num; 253 | for(auto i: freq) { 254 | num.push_back(i.first); 255 | } 256 | for(int i=0; i num[j]) { 264 | int temp = num[i]; 265 | num[i] = num[j]; 266 | num[j] = temp; 267 | } 268 | } 269 | } 270 | } 271 | for(int i: num) { 272 | for(int j=0; j map=new HashMap<>(); 290 | for(int i=0;i pq=new PriorityQueue<>((x,y)->x[1]==y[1]?x[0]-y[0]:y[1]-x[1]); 295 | for(Map.Entry hm:map.entrySet()){ 296 | pq.offer(new int[]{hm.getKey(),hm.getValue()}); 297 | } 298 | while(!pq.isEmpty()){ 299 | int ar[]=pq.poll(); 300 | for(int i=0;i map = new HashMap<>(); 316 | Scanner s = new Scanner(System.in); 317 | int n=s.nextInt(); 318 | int a[]=new int[n]; 319 | for(int i=0;i list = new ArrayList<>(); 330 | for(int num:a) 331 | { 332 | list.add(num); 333 | } 334 | 335 | Collections.sort(list,(b,c)->{ 336 | int freq=map.get(c)-map.get(b); 337 | if(freq==0) 338 | { 339 | return b-c; 340 | } 341 | return freq; 342 | }); 343 | for(int num:list) 344 | { 345 | System.out.print(num+" "); 346 | } 347 | } 348 | } 349 | ================================================================== 350 | import java.io.*; 351 | import java.util.*; 352 | import java.text.*; 353 | import java.math.*; 354 | import java.util.regex.*; 355 | 356 | public class Solution { 357 | public static void main(String args[] ) throws Exception { 358 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 359 | Scanner obj=new Scanner(System.in); 360 | int a=obj.nextInt(); 361 | int arr[]=new int[a]; 362 | for(int i=0;ihm=new HashMap<>(); 367 | for(int i=0;i 28 | #include 29 | #include 30 | #include 31 | 32 | int main() { 33 | 34 | int n,i,j,sum=0; 35 | scanf("%d",&n); 36 | int a[n]; 37 | for(i=0;ia[j]) 44 | { 45 | int temp=a[i]; 46 | a[i]=a[j]; 47 | a[j]=temp; 48 | } 49 | } 50 | } 51 | int l=0,r=n-1; 52 | while(l 67 | #include 68 | #include 69 | #include 70 | 71 | int main() { 72 | 73 | int n,i,j,sum=0; 74 | scanf("%d",&n); 75 | int a[n]; 76 | int b[n]; 77 | for(i=0;ia[j]) 84 | { 85 | int temp=a[i]; 86 | a[i]=a[j]; 87 | a[j]=temp; 88 | } 89 | } 90 | } 91 | int l=0,r=n-1,c=0; 92 | while(l 26 | #include 27 | #include 28 | #include 29 | 30 | int main() { 31 | 32 | int n,i,j; 33 | scanf("%d",&n); 34 | int a[n]; 35 | for(i=0;ia[j]) 42 | { 43 | int temp=a[i]; 44 | a[i]=a[j]; 45 | a[j]=temp; 46 | } 47 | } 48 | } 49 | int l=0,r=n-1; 50 | while(l 36 | #include 37 | #include 38 | #include 39 | 40 | int maximum(int A[],int n) 41 | { 42 | int max=A[0]; 43 | for(int i=0;i 25 | #include 26 | #include 27 | #include 28 | 29 | int main() { 30 | 31 | int n,i,j; 32 | scanf("%d",&n); 33 | int a[n]; 34 | for(i=0;ia[j]) 41 | { 42 | int temp=a[i]; 43 | a[i]=a[j]; 44 | a[j]=temp; 45 | } 46 | } 47 | } 48 | int l=0,r=n-1; 49 | while(l 27 | #include 28 | #include 29 | #include 30 | 31 | int main() { 32 | 33 | int n,m,ans; 34 | scanf("%d%d",&n,&m); 35 | for(int i=5;i<=14;i++) 36 | { 37 | ans=i*i; 38 | if(ans>=n&&ans<=m) 39 | printf("%d ",ans); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Sum of Two numbers 46: -------------------------------------------------------------------------------- 1 | Given 2 huge numbers as separate digits, store them in array and process them and 2 | calculate the sum of 2 numbers and store the result in an array and print the sum. 3 | 4 | Input Format 5 | 6 | 12 7 | 9 2 8 1 3 5 6 7 3 1 1 6 8 | 9 9 | 7 8 4 6 2 1 9 9 7 10 | 11 | Constraints 12 | 13 | 5<=n<=20 14 | 15 | Output Format 16 | 17 | 9 2 8 9 2 0 2 9 5 1 1 3 18 | 19 | Sample Input 0 20 | 21 | 5 22 | 1 2 3 4 5 23 | 4 24 | 6 7 8 9 25 | Sample Output 0 26 | 27 | 1 9 1 3 4 28 | Explanation 0 29 | 30 | You will have to add these two array as like below: 12345+ 6789= 19134 31 | 32 | ------------------------------------------------------------------------------------------------------ 33 | #include 34 | #include 35 | #include 36 | #include 37 | 38 | int main() { 39 | 40 | int n,m,i,j; 41 | 42 | long long int num1=0,num2=0,sum=0,s,c=0; 43 | 44 | scanf("%d",&n); 45 | 46 | int a[n]; 47 | 48 | for(i=0;i0) 81 | 82 | { 83 | 84 | c++; 85 | 86 | s/=10; 87 | 88 | } 89 | 90 | int r[c]; 91 | 92 | j=c-1; 93 | 94 | while(sum>0) 95 | 96 | { 97 | 98 | r[j]=sum%10; 99 | 100 | j--; 101 | 102 | sum/=10; 103 | 104 | } 105 | 106 | for(i=0;i 25 | int main() { 26 | int n,i,sum=0; 27 | scanf("%d",&n); 28 | int a[n]; 29 | for(i=0;i 33 | #include 34 | #include 35 | #include 36 | 37 | int main() { 38 | 39 | int n,i; 40 | scanf("%d",&n); 41 | int a[n]; 42 | for(i=0;i 59 | #include 60 | #include 61 | #include 62 | 63 | void swapElement(int a[],int n,int k){ 64 | 65 | for(int i=0,j=n-k;i 29 | #include 30 | #include 31 | #include 32 | 33 | int main() { 34 | int a,b,i,j,k; 35 | scanf("%d%d",&a,&b); 36 | int arr[a],arr1[b]; 37 | int n=a+b; 38 | int c[n]; 39 | for(i=0;ia[j]) 49 | { 50 | int temp=a[i]; 51 | a[i]=a[j]; 52 | a[j]=temp; 53 | } 54 | } 55 | } 56 | for(i=0;i 2 | #include 3 | 4 | int sort(int a[],int n) 5 | { 6 | int t,i,j; 7 | for(i=0;ia[j]) 12 | { 13 | t=a[i]; 14 | a[i]=a[j]; 15 | a[j]=t; 16 | } 17 | } 18 | } 19 | for(i=0;i3, 3->5, 7->8, -1->2, 8->11, 5->7, 11->, 16 | 17 | Sample Input 0 18 | 19 | 6 20 | 5 4 3 7 6 1 21 | Sample Output 0 22 | 23 | 5->6, 4->5, 3->4, 7->, 6->7, 1->3, 24 | 25 | ===================================================================================================================== 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | int maxi(int a[],int n,int key) 32 | { int min=INT_MAX; 33 | for(int i=0;ikey && a[i]%d, ",a[i],max); 52 | else 53 | printf("%d->, ",a[i]); 54 | } 55 | return 0; 56 | } 57 | -------------------------------------------------------------------------------- /missing Number: -------------------------------------------------------------------------------- 1 | Given an array nums containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array. 2 | 3 | 4 | 5 | Example 1: 6 | 7 | Input: nums = [3,0,1] 8 | Output: 2 9 | Explanation: n = 3 since there are 3 numbers, so all numbers are in the range [0,3]. 2 is the missing number in the range since it does not appear in nums. 10 | Example 2: 11 | 12 | Input: nums = [0,1] 13 | Output: 2 14 | Explanation: n = 2 since there are 2 numbers, so all numbers are in the range [0,2]. 2 is the missing number in the range since it does not appear in nums. 15 | Example 3: 16 | 17 | Input: nums = [9,6,4,2,3,5,7,0,1] 18 | Output: 8 19 | Explanation: n = 9 since there are 9 numbers, so all numbers are in the range [0,9]. 8 is the missing number in the range since it does not appear in nums. 20 | 21 | 22 | Constraints: 23 | 24 | n == nums.length 25 | 1 <= n <= 104 26 | 0 <= nums[i] <= n 27 | All the numbers of nums are unique. 28 | 29 | 30 | Follow up: Could you implement a solution using only O(1) extra space complexity and O(n) runtime complexity? 31 | ============================================================================================================================================== 32 | class Solution { 33 | public int missingNumber(int[] nums) { 34 | int n=nums.length; 35 | Arrays.sort(nums); 36 | int j; 37 | for(int i=0;i