Showing posts with label Repetition problem. Show all posts
Showing posts with label Repetition problem. Show all posts

Saturday, 22 April 2023

Smaller Sum

 

PROBLEM 59: Smaller Sum

(For best view experience, view in windows version)

Problem Reference : GeeksForGeeks

Author : Ajay Zad
Date    : 22/04/2023


You are given an array arr of n integers. For each index i, you have to find the sum of all integers present in the array with a value less than arr[i].

Example 1:

Input:
n = 3
arr = {1, 2, 3}
Output:
0 1 3
Explanation:
For 1, there are no elements lesser than itself.
For 2, only 1 is lesser than 2.
And for 3, 1 and 2 are lesser than 3, so the sum is 3.

Example 2:

Input:
n = 2
arr = {4, 4}
Output:
0 0
Explanation:
For 4, there are no elements lesser than itself. 
For 4, there are no elements lesser than itself.
There are no smaller elements than 4.

SOLUTION:

from typing import List
class Solution:
    def smallerSum(self, n : int, arr : List[int]) -> List[int]:
        # code here
        ar = set(arr)
        arr1 = list(ar)
        arr1.reverse()
        l = []
        def sum1(i):
            j = arr1.index(i)
            s1 = sum(arr1[j+1:])
            l.append(s1)
        
            
        for i in arr:
            sum1(i)
        return l


class IntArray:
    def __init__(self) -> None:
        pass
    def Input(self,n):
        arr=[int(i) for i in input().strip().split()]#array input
        return arr
    def Print(self,arr):
        for i in arr:
            print(i,end=" ")
        print()


if __name__=="__main__":
    t = int(input())
    for _ in range(t):

        n = int(input())
        arr=IntArray().Input(n)
        obj = Solution()
        res = obj.smallerSum(n, arr)
        
        IntArray().Print(res)
        


Expected Time Complexity:O(n log n)
Expected Space Complexity:O(n)

Constraints:
1 <= n <= 105
0 <= arr[i] <= 109

Tuesday, 19 April 2022

Matrix Rotate Problem !!!

PROBLEM 49: Matrix Rotate Problem

A program to rotate a square matrix by 90° anti-clockwise direction. (Interview Question)

(For best view experience, view in windows version)


Author : Ajay Zad
Date    : 19/04/2022


#include<stdio.h>
void main()
{
        int i,j,r,c,m,n;
        printf("Enter the size for Row :");
        scanf("%d",&r);
        
        printf("Enter the size for Column :");
        scanf("%d",&c);
        
        int matrix1[r][c],matrix2[r][c];
        
        printf("Enter the elements in the matrix :\n");
        for(i = 0 ; i < r ; i++)
        {
            for(j = 0 ; j < c ; j++)
            {
                scanf("%d",&matrix1[i][j]);
            }
        }

        
        printf("\n\n");
        
        for(i = 0 ; i < r ; i++)
        {
            for(j = 0 ; j < c ; j++)
            {
                matrix2[i][j] = matrix1[j][i];
            }
        }

        
        m = 0;
        for(i = (r-1); i >= 0 ; i--)
        {
            n = 0;
            for( j = 0 ; j < c ; j++)
            {
                matrix1[m][n] = matrix2[i][j];
                n++;
            }
            m++;
        }

       printf("Matrix after 90 degree's rotation : \n"); 
        for(i = 0 ; i < r ; i++)
        {
            for(j = 0 ; j < c ; j++)
            {
                printf("%d\t",matrix1[i][j]);
            }
            printf("\n");
        }
        
}

The above code is in C language. 

Output :
Enter the size for Row : 3
Enter the size for Column : 3
Enter the elements in the matrix :
1
2
3
4
5
6
7
8
9

The original form of the matrix will be as follow :
1    2    3
4    5    6
7    8    9

Matrix after 90 degree's rotation :
3    6    9
2    5    8
1    4    7
 

Tuesday, 15 March 2022

Repetition Of All Numbers Problem !!!

 PROBLEM 46: Repetition Of All Numbers


A Program to find out number of times the elements are repeated in the array.

(Interview Question)


Author - Ajay Zad

Date   - 15-03-2022


n = int(input("Enter the size of the array :"))

l = []

for i in range(0,n):

    m = int(input("Enter the elements in the array (repeat the values) :"))

    l.append(m)

    

s = set(l)

l1 = list(s)

l1.sort()


for i in range(0,len(l1)):

    cnt = 0

    for j in range(0,n):

        if l1[i] == l[j]:

            cnt = cnt + 1

    print("Number ",l1[i]," is repeated for = ",cnt," times")

       

The above code is in python language.

Input : 

Enter the size of the array : 5 

Enter the elements in the array (repeat the values) :1

Enter the elements in the array (repeat the values) :2

Enter the elements in the array (repeat the values) :1

Enter the elements in the array (repeat the values) :3

Enter the elements in the array (repeat the values) :2


Output: 

Number  1  is repeated for =  2  times

Number  2  is repeated for =  1  times

Number  3  is repeated for =  2  times

Monday, 14 March 2022

Prime Problem !!!

 PROBLEM 45: Prime 


Program to find out all the prime numbers from the range of 0 to n. 


 Name - Ajay Zad

 Date - 14/03/2022 


n = int(input("Enter the value for n :"))

cnt = 0 

print("The Prime numbers are :")

for i in range(2,n+1):

    for j in range(2,i):

        if i % j == 0:   

            cnt = cnt + 1

        

    if cnt == 0:

        print(i)

    cnt = 0




Input:

Enter the value for n : 

10


Output :

The Prime numbers are :

2

3

5

7

Friday, 11 March 2022

Repetition Problem !!!

PROBLEM 44:  Repetition


A Program to find out the most repeated number in the array. (Without using built-in functions)

(Interview Question)


 Author - Ajay Zad 

 Date     - 11/03/2022 


n = int(input("Input the size of the array :"))

l = []

s = set()

for i in range(0,n):

    ele = int(input("Enter the elements in the array :"))

    l.append(ele)

    #Adding no. in set as it doesn't store duplicates

    s.add(ele)

    

cnt = 0

cnt1 = 0

l2 = list(s)

for i in range(0,len(l2)):

    cnt = 0

    for j in range(0,n):

        #Comparing for equality 

        if l2[i] == l[j]:

            cnt = cnt + 1

            #Condition for counter 

            if cnt > cnt1:

                cnt1 = cnt

                m_no = l2[i]

print("The number with highest repetition : ",m_no)


Input :

Input the size of the array : 5

Enter the elements in the array : 1

Enter the elements in the array : 2

Enter the elements in the array : 2

Enter the elements in the array : 3

Enter the elements in the array : 2


Output :

The number with highest repetition : 2


                


                

    

Rearrange an array with O(1) extra space

  PROBLEM 61:  Rearrange an array with O(1) extra space (For best view experience, view in windows version) Problem Reference : GeeksForGeek...