Posts

Showing posts with the label Arrays

Kotlin vs Java

Recently, you might have come across a word Kotlin, so in this post, I am going to share about Kotlin and how it is different from JAVA? What is Kotlin? Kotlin is statically typed programming language developed by JetBrains that runs on Java Virtual Machine. It can also be compiled to JavaScript source code. Kotlin is expressive, concise and powerful as it combines object-oriented Programming and functional programming features. It is focused on interoperability, safety and tooling support. Lately, it is really popular within the Android community and most of the developers are switching towards Kotlin. The reason being that Kotlin reduces the lines of code and make it more convenient process for the developers. Let's look at the few important differences between Kotlin and Java Kotlin vs Java : Function Types: Kotlin has the ability to pass the function as a parameter to other functions without resorting to single abstract methods like callable, runnable etc, ...

Program to find the Equilibrium index of an array

Equilibrium index of an array is the index such that the sum of its lower index values and the sum of its higher index values are same. Suppose the array values are a[0] =1  a[1] =-5   a[2] =4   a[3] =4   a[4] =-6   a[5] =5   a[6] =1 then the index 3 will be at Equilibrium because a[0]+a[1]+a[2]=a[4]+a[5]+a[6]. In an array there can be more then one Equilibrium indexes provided the sum of its lower indexes and higher indexes are same. The logic of the program is pretty simple.First we will calculate the sum of the whole array elements and then initialize leftsum=0.Now we iterate through all the index of the arrays and substract all index values from the sum and add to leftsum and simultaneously check whether sum=leftsum.If it is found out equal then return i that is the index of the array. Program : #include <stdio.h> #include<conio.h> int equilibrium(int arr[], int len) {    int sum = 0;        in...

Find two element in an array whose sum is closest to zero

The array is having both +ve and -ve values.Lets take array values as  3 -7 8 9 5 -3 Then the two numbers with sum closest to zero will be 3 and -3. Program : # include <stdio.h> # include <stdlib.h> # include <math.h> void sumclosetozero(int arr[], int size) {   int count = 0;   int a, b, sum_min, sum, a_min, b_min;   a_min = 0;   b_min = 1;   sum_min = arr[0] + arr[1];   for(a = 0; a < size - 1; a++)   {     for(b = a+1; b < size; b++)     {       sum = arr[a] + arr[b];       if(abs(sum_min) > abs(sum))       {         sum_min = sum;         a_min = a;         b_min = b;       }     }   }   printf(" The two elements whose sum is minimum are %d and %d",           arr[a_min], arr[b_min]); } int ma...

Program to separate positive and negative numbers in an array

#include<iostream> #include<conio.h> using namespace std; void separateposneg(int arr[10],int a) {     int j=0,b[10],count=0;  for(int i=0;i<a;i++)  {   if(arr[i]>0)   {   count++;   b[j]=arr[i];   j++;   } }  for(int i=0;i<a;i++)  {   if(arr[i]<0)   {     b[count]=arr[i];   count++;   }      } cout<<"The separated numbers are\n";  for(int j=0;j<a;j++)  {  cout<<b[j]<<"\n";        }  } int main() {     int ar[10],n;     cout<<"enter no of values\n";     cin>>n; cout<<"Enter array\n"; for(int i=0;i<n;i++) { cin>>ar[i]; } separateposneg(ar,n); getch(); } Input : 6 3 90 -2 6 -90 5 Output : 3 90 6 5 -2 -90

Program to print the numbers with Odd Occurences

#include<iostream> #include<conio.h> using namespace std; int main() { int a[10],b[10]; int n,c; cout<<"Enter the total numbers to be checked\n"; cin>>n; cout<<"Enter the numbers\n"; for(int i=0;i<n;i++) { cin>>a[i]; } for(int j=0;j<n;j++) { int count=0; if(a[j]==0) { c++; continue; } for(int k=j+1;k<n;k++) { if(a[j]==a[k]) { count++; a[k]=0; } } b[j-c]=count+1; if(b[j-c]%2!=0) cout<<"The numbers with odd occurence are\n"<<a[j]<<"\t"; } getch(); } Input : 8 3 3 3 4 4 6 7 7 Output : 3 6

Program to search the element in Sorted Rotated Pivoted array

Sorted Rotated Pivoted array means the sorted array is rotated through the pivot element.The element in a sorted array can be find using binary search in O(logn) time.But if the sorted array is rotated through some pivot element then how to search for an element in that array.Suppose take sorted array 1 2 3 4 5 it might become 3 4 5 1 2 after rotating at Pivot element 5.Pivot element can be found by the idea that the only element whose next element is smaller then pivot element in an array.So here is the program to find the element in a sorted rotated pivoted array. Our first task is to find a pivot element in an array.The above idea of  pivot element is used to find the pivot element in an array. int findpivot(int a[],int low,int high) { int mid=(low+high)/2; if(a[mid]>a[mid+1])        // if middle element is greater then its next element then it is pivot element as it is the                     ...

Program to reverse the words of string

This program is little bit modification to the previous post program.After reversing the letters of each words in a string when we call again the reverse function the words get reversed of the string. #include<stdio.h> #include<conio.h> void reverse(char *start, char *end); void reversethewords(char *s) {   char *wordbegin = s;      //wordbegin and t are two pointer to the first character of string s   char *t = s;               while( *t)   {     t++;     if (*t == '\0')            // if reached at the end of the string reverse(swap) the first character and last and     {                                                                             ...

Program to reverse the letters of each words of the string

#include<stdio.h> #include<conio.h> void reverse(char *start, char *end); void reversethewords(char *s) {   char *wordbegin = s;      //wordbegin and temp are two pointer to the first character of string s   char *t = s;                 while( *t)   {     t++;     if (*t == '\0')            // if reached at the end of the string reverse(swap) the first character and last and     {                                                                                                                            so on.       ...

Program to find the element in an array with maximum occurrences

This is the program to find the element with maximum occurrences in an array.I extended the previous post program to get the element with maximum occurrences. #include<stdio.h> #include<conio.h> int main() { int k=0,count=0,z=0; int index; int countarr[16],nos[16]; int arr[20]={1,1,23,23,23,34,34,34,45,45,33,33,33,33,33,33,54,54,11,12}; for(int i=0;i<20;i++) { if(i>0) { i=i+count-1; } nos[z]=arr[i]; z=z+1; count=0; for(int j=0;j<20;j++) { if(arr[i]==arr[j]) count++; } countarr[k]=count; k=k+1;     }   int max=countarr[0]; for(int i=1;i<k;i++) { if(countarr[i]>max) { max=countarr[i]; index=i; } } printf("The element which have maximum frequency is :\n"); printf("%d",nos[index]); getch();   }

Count the occurrences of elements in an array

This is the program of counting the occurrences of each of the elements in an array asked in interview.I used a sample array for this program. #include<stdio.h> #include<conio.h> int main() { int k=0,count=0,z=0; int countarr[16],nos[16]; int arr[20]={1,1,23,23,23,34,34,34,45,45,33,33,33,33,33,33,54,54,11,12}; for(int i=0;i<20;i++) { if(i>0) { i=i+count-1; } nos[z]=arr[i]; z=z+1; count=0; for(int j=0;j<20;j++) { if(arr[i]==arr[j]) count++; } countarr[k]=count; k=k+1;     }   printf("The count of each element of the array is :\n"); for(int i=0;i<k;i++) printf("%d-%d\n",nos[i],countarr[i]); getch();   }

Program to uncompress a string ie a2b3c4 to aabbbcccc

Below is the program to uncompress a string #include<stdio.h> #include<conio.h> #include<stdlib.h> int main() { char str[100]="a2b3c4d8u7"; for(int i=0;str[i]!='\0';i++) { if(i%2!=0) { for(int j=0;j<atoi(&str[i]);j++) { printf("%c",str[i-1]); } } } getch(); } Want to become a Data Engineer? Check out below blog posts  1.  5 Key Skills Every Data Engineer needs in 2023 2.  How to prepare for Data Engineering Interviews 3.  Top 25 Data Engineer Questions

Program to find the 2nd largest number in an array

This is the program i written in my interview.There can be a better program or logic for the same #include<stdio.h> #include<conio.h> int secondlargest(int s[],int k) { int s1[5],index,ind; int max=s[0]; /* Find the max element in array and get its index*/ for(int i=1;i<k;i++) { if(s[i]>max) { max=s[i]; ind=i; } } /*Substract the array elements from the maximum element and store the new elements in new array,make again the max element index position as max element to avoid having zero in array.*/ for(int i=0;i<k;i++) { s1[i]=max-s[i]; s1[ind]=max; } /*Now find the minimum element in the new array and get its index*/ int min=s1[0]; for(int i=1;i<k;i++) { if(s1[i]<min) { min=s1[i]; index=i; } } /*Return the element having index of above step from the old array.You will have second max element of array*/ return s[index]; } int main() { int arr[5]={35,81,49,1,90}; int s=secondlargest(arr,5); printf("%d",s); getch(); }