Problem 3

7 comments on “Problem 3”


  • Sameer

    // O(log(n)) Solution in C++

    #include
    using namespace std;
    int main() {
    int arr[] = {1, 1, 2, 2, 2, 2, 3};
    int n = sizeof(arr)/sizeof(arr[0]);
    int x = 2, cnt = 0;
    for(int i=0;ix)
    break;
    }
    cout<<cnt<<endl;
    return 0;
    }


  • Nikkala

    import java.util.*;
    class Main
    {
    public static void main (String[] args)
    {
    Scanner n=new Scanner(System.in);
    int a=n.nextInt();
    int x=n.nextInt();
    int a1[]=new int[a];
    int count=0;
    System.out.println(“enter the elements”);
    for(int i=0;i<a;i++)
    {
    a1[i]=n.nextInt();
    }
    for(int i=0;i<a;i++)
    {
    if(x==a1[i])
    {
    count=count+1;

    }
    }
    System.out.println("the elemnt");
    System.out.println(count);
    }
    }


  • subhojit

    #Simple python Code whose expected Time Complexity is O(log n)
    arr=[int(x) for x in input().split(‘ ‘)]
    n=int(input())
    low=0
    ct=0
    high=len(arr)-1

    for i in range(low,high+1,1):
    mid=(low+high)//2
    if arr[low]==n:
    while True:
    if arr[low]==n:
    ct+=1
    low=low+1
    else:
    break
    break
    elif arr[high]==n:
    while True:
    if arr[high]==n:
    ct+=1
    high-=1
    else:
    break
    break
    elif arr[mid]>n:
    high=mid-1
    elif arr[mid]<n:
    low=mid+1
    else:
    p=mid-1
    while True:
    if arr[mid]==n:
    ct+=1
    mid+=1
    else:
    break
    while True:
    if arr[p]==n:
    ct+=1
    p-=1
    else:
    break
    break

    print(ct)


  • kandukuri

    #include
    void main()
    {
    int n;
    printf(“enter the length: “);
    scanf(“%d”, &n);
    int arr[n];
    for (int i = 0; i < n; i++)
    {
    scanf("%d", &arr[i]);
    }
    int freq, count = 0;
    printf("enter the freq: ");
    scanf("%d",&freq);
    for (int i = 0; i 0)
    {
    printf(“%d”, count);
    }
    else
    {
    printf(“-1 the number is not found!”);
    }
    }


  • Mohd Saif

    arr=[7,4,5,2]
    for i in range(0,len(arr)):
    for j in range(i):
    if(arr[j]>arr[j+1]):
    temp=arr[j]
    arr[j]=arr[j+1]
    arr[j+1]=temp
    print(arr)


  • Mohd Saif

    arr=[14,33,27,10,35,19,42,44]
    for i in range(0,len(arr)):
    for j in range(i):
    if(arr[j]>arr[j+1]):
    temp=arr[j]
    arr[j]=arr[j+1]
    arr[j+1]=temp
    print(arr)


  • Hritik

    #include
    void bubblesort(int [],int);
    int main(){
    int arr[] = {33,65,23,1,3};
    int i;
    bubblesort(arr,sizeof(arr)/sizeof(arr[0]));
    for(i=0;i<sizeof(arr)/sizeof(int);i++){
    printf("%d\t",arr[i]);
    }
    return 0;
    }
    void bubblesort(int arr[] , int n){
    int i,j,temp;
    for(i=1;i<=n-1;i++){
    for(j=0;jarr[j+1]){
    temp = arr[j];
    arr[j]=arr[j+1];
    arr[j+1]=temp;
    }
    }
    }
    }